3535import java .util .concurrent .ForkJoinPool .ForkJoinWorkerThreadFactory ;
3636import java .util .concurrent .ForkJoinWorkerThread ;
3737import java .util .concurrent .ThreadFactory ;
38+ import java .util .concurrent .atomic .AtomicLong ;
39+ import java .util .function .BiConsumer ;
3840import java .util .function .Supplier ;
3941
4042import org .jooby .Env ;
169171 */
170172public class Exec implements Module {
171173
174+ private static final BiConsumer <String , Executor > NOOP = (n , e ) -> {
175+ };
176+
172177 /** The logging system. */
173178 private final Logger log = LoggerFactory .getLogger (getClass ());
174179
@@ -189,6 +194,16 @@ public class Exec implements Module {
189194 return new ForkJoinPool (n , fjwtf (name ), null , asyncMode );
190195 });
191196
197+ private String namespace ;
198+
199+ protected Exec (final String namespace ) {
200+ this .namespace = namespace ;
201+ }
202+
203+ public Exec () {
204+ this ("executors" );
205+ }
206+
192207 /**
193208 * Defined the default value for daemon. This value is used when a executor spec doesn't define a
194209 * value for daemon. Default is: <code>true</code>
@@ -216,14 +231,21 @@ public Exec priority(final int priority) {
216231
217232 @ Override
218233 public Config config () {
219- return ConfigFactory .empty ("exec.conf" ).withValue ("executors" ,
234+ return ConfigFactory .empty ("exec.conf" ).withValue (namespace ,
220235 ConfigValueFactory .fromAnyRef ("fixed" ));
221236 }
222237
223238 @ Override
224239 public void configure (final Env env , final Config conf , final Binder binder ) {
225- List <Map <String , Object >> executors = executors (conf .getValue ("executors" ), daemon , priority ,
226- Runtime .getRuntime ().availableProcessors ());
240+ configure (env , conf , binder , NOOP );
241+ }
242+
243+ protected void configure (final Env env , final Config conf , final Binder binder ,
244+ final BiConsumer <String , Executor > callback ) {
245+ List <Map <String , Object >> executors = conf .hasPath (namespace )
246+ ? executors (conf .getValue (namespace ), daemon , priority ,
247+ Runtime .getRuntime ().availableProcessors ())
248+ : Collections .emptyList ();
227249 List <Entry <String , ExecutorService >> services = new ArrayList <>(executors .size ());
228250 for (Map <String , Object > options : executors ) {
229251 // thread factory options
@@ -246,6 +268,7 @@ public void configure(final Env env, final Config conf, final Binder binder) {
246268 options );
247269
248270 bind (binder , name , executor );
271+ callback .accept (name , executor );
249272
250273 services .add (Maps .immutableEntry (name , executor ));
251274 }
@@ -292,18 +315,20 @@ private static Set<Class> collector(final Class type) {
292315
293316 private static ThreadFactory factory (final String name , final boolean daemon ,
294317 final int priority ) {
318+ AtomicLong id = new AtomicLong (0 );
295319 return r -> {
296- Thread thread = new Thread (r , name );
320+ Thread thread = new Thread (r , name + "-" + id . incrementAndGet () );
297321 thread .setDaemon (daemon );
298322 thread .setPriority (priority );
299323 return thread ;
300324 };
301325 }
302326
303327 private static ForkJoinWorkerThreadFactory fjwtf (final String name ) {
328+ AtomicLong id = new AtomicLong ();
304329 return pool -> {
305330 ForkJoinWorkerThread thread = ForkJoinPool .defaultForkJoinWorkerThreadFactory .newThread (pool );
306- thread .setName (name );
331+ thread .setName (name + "-" + id . incrementAndGet () );
307332 return thread ;
308333 };
309334 }
0 commit comments