Accessing Executor in Managed

118 views
Skip to first unread message

Kode Tester

unread,
Aug 24, 2021, 6:04:12 PM8/24/21
to dropwizard-user

In DW Core (https://www.dropwizard.io/en/latest/manual/core.html#managed-objects), the sample code below talks about configuring Executor Service in Application run method.

If I want to access executorService in my class, what is the right way to get a reference?


<<<<<<snip>>>>>>

It should be noted that Environment has built-in factory methods for ExecutorService and ScheduledExecutorService instances which are managed. These managed instances use InstrumentedThreadFactory that monitors the number of threads created, running and terminated


public class MyApplication extends Application<MyConfiguration> { 
  @Override 
public void run(MyConfiguration configuration, Environment environment) {

ExecutorService executorService = environment.lifecycle()
.executorService(nameFormat)
.maxThreads(maxThreads) .build(); 

 ScheduledExecutorService scheduledExecutorService = environment.lifecycle() .scheduledExecutorService(nameFormat) 
  .build(); 
  } 
}



Ryan Kennedy

unread,
Aug 24, 2021, 8:02:16 PM8/24/21
to dropwiz...@googlegroups.com
The most straightforward way is to pass it via a constructor to (for example) your Jersey resource class. For example:

environment.jersey().register(new HelloWorldResource(managedExecutorService));

Ryan

--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dropwizard-user/e19b94f3-7627-431b-a9a8-6ef015777b39n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages