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-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to dropwizard-dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to