Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method to get the underlying ThreadFactory for use with external libraries #1117

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions api/src/main/java/net/md_5/bungee/api/plugin/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Plugin
private File file;
@Getter
private Logger logger;
private final Unsafe unsafe = new Unsafe();

/**
* Called when the plugin has just been loaded. Most of the proxy will not
Expand Down Expand Up @@ -88,18 +89,42 @@ final void init(ProxyServer proxy, PluginDescription description)
this.logger = new PluginLogger( this );
}

//
private ExecutorService service;
public Unsafe unsafe()
{
return unsafe;
}

//
@Deprecated
public ExecutorService getExecutorService()
{
if ( service == null )
return unsafe().getExecutorService();
}
//

public final class Unsafe {
private GroupedThreadFactory groupedThreadFactory;
private ExecutorService service;

private Unsafe(){}

public ExecutorService getExecutorService()
{
if ( service == null )
{
service = Executors.newCachedThreadPool( new ThreadFactoryBuilder().setNameFormat( getDescription().getName() + " Pool Thread #%1$d" )
.setThreadFactory( getGroupedThreadFactory() ).build() );
}
return service;
}

public GroupedThreadFactory getGroupedThreadFactory()
{
service = Executors.newCachedThreadPool( new ThreadFactoryBuilder().setNameFormat( getDescription().getName() + " Pool Thread #%1$d" )
.setThreadFactory( new GroupedThreadFactory( this ) ).build() );
if ( groupedThreadFactory == null )
{
groupedThreadFactory = new GroupedThreadFactory( Plugin.this );
}
return groupedThreadFactory;
}
return service;
}
//
}
2 changes: 1 addition & 1 deletion proxy/src/main/java/net/md_5/bungee/BungeeCord.java
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public void run()
getLogger().log( Level.SEVERE, "Exception disabling plugin " + plugin.getDescription().getName(), t );
}
getScheduler().cancel( plugin );
plugin.getExecutorService().shutdownNow();
plugin.unsafe().getExecutorService().shutdownNow();
}

getLogger().info( "Thank you and goodbye" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class BungeeScheduler implements TaskScheduler
@Override
public ExecutorService getExecutorService(Plugin plugin)
{
return plugin.getExecutorService();
return plugin.unsafe().getExecutorService();
}
};

Expand Down