Your approach seems like a sound one, and Dropwizard is essentially silent 
on the how/why of background threads, scheduling, etc.  A possible 
consideration is that you might prefer DW to fail fast and exit on service 
non-start rather than "run unhealthy" in connection with startAsync.  
Likewise, stopAsync might not be entirely harmonious with DW's synchronous 
stop().


On Friday, November 2, 2018 at 2:55:36 PM UTC-4, [email protected] wrote:
>
> I'm writing my first dropwizard service. Typically I've used guava, 
> jetty/grizzly, jersey, etc. on my own and created Guava Services for each 
> and then a ServiceManager. In the dropwizard world, what is the best way to 
> create background processing services each with a health check? 
>
> Normally I would do this with something like a guava 
> AbstractExecutionThreadService for each. Maybe one reads an SQS queue and 
> puts things into a Database Batching blocking queue (that might be two 
> services). Then I'd take my Jetty Service, and two background services and 
> put them into a ServiceManager that I start in my main.
>
> What's the best way to write these background services with dropwizard? 
> Should I do them the same way and then wrap them in Managed register each 
> managed and its healthcheck in main? Or does dropwizard have something else 
> I should use instead of the Guava AbstractExecutionThreadService? 
>
> That might look like this. But I feel like if that was the standard way to 
> do it I wouldn't have to write ManagedGuavaService and 
> GuavaServiceHealthCheck. So I'm thinking perhaps there is a better way to 
> do this, perhaps not even using Guava Services?
>
>     @Override
>     public void run(final MyConfiguration config, final Environment env) {
>
>         SqsQueue queue = new SqsQueue(config.getRegion(), 
> config.getQueueFullName());
>         AbstractExecutionThreadService queueConsumer = 
> queue.createConsumerService(System.out::println);
>
>         // Rest resource writes messages to the queue
>         ProviderResource resource = new ProviderResource(queue::send);
>         env.jersey().register(resource);
>         env.healthChecks().register("providers-api", new 
> ProviderResourceHealthCheck());
>
>         // Queue consumer thread to process the SQS queue
>         env.lifecycle().manage(new ManagedGuavaService(queueConsumer));
>         env.healthChecks().register("queue-consumer", new 
> GuavaServiceHealthCheck(queueConsumer));
>     }
>     
>     static class ManagedGuavaService implements Managed {
>         private final Service service;
>
>         public ManagedGuavaService(Service service) {
>             this.service = service;
>         }
>
>         @Override
>         public void start() throws Exception {
>             service.startAsync();
>         }
>
>         @Override
>         public void stop() throws Exception {
>             service.stopAsync();
>         }
>     }
>
>     private static class GuavaServiceHealthCheck extends HealthCheck {
>         private final Service service;
>
>         public GuavaServiceHealthCheck(Service service) {
>             this.service = service;
>         }
>
>         @Override
>         protected Result check() throws Exception {
>             return service.isRunning()
>                 ? Result.healthy()
>                 : Result.unhealthy(service.state().toString());
>         }
>     }
>

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to