3

What I am trying to achieve:

I want to make a dropwizard client that polls Amazon SQS. Whenever a message is found in the queue, it is processed and stored.

Some information about the processed messages will be available through an API.

Why I chose Dropwizard:

Seemed like a good choice to make a REST client. I need to have metrics, DB connections and integrate with some Java services.

What I need help with:

It is not very clear how and where the SQS polling will fit in a typical dropwizard application.
Should it be a managed resource? Or a console reporter console-reporter? Or something else.

2
  • What kind of polling do you need? At fixed interval or as long as messages are available in the queue? Commented Aug 31, 2018 at 8:38
  • I had to implement a somewhat "smart" polling (long polling + back-off strategies + message blocking). These are implementation level details for my use case though. The first answer was a good enough pointer in the right direction :) Commented Sep 1, 2018 at 12:30

2 Answers 2

3

You can use com.google.common.util.concurrent.AbstractScheduledService to create a consumer thread and add it to the dropwizard's environment lifecycle as ManagedTask. Following is the pseudocode -

public class YourSQSConsumer extends AbstractScheduledService { @Override protected void startUp() { // may be print something } @Override protected void shutDown() { // may be print something } @Override protected void runOneIteration() { // code to poll on SQS } @Override protected Scheduler scheduler() { return newFixedRateSchedule(5, 1, SECONDS); } } 

In Main do this -

YourSQSConsumer consumer = new YourSQSConsumer(); Managed managedTask = new ManagedTask(consumer); environment.lifecycle().manage(managedTask); 
Sign up to request clarification or add additional context in comments.

Comments

3

As an alternative to RishikeshDhokare's answer, one can also go ahead with the following code which does not need to include additional jar as a dependency in your project to keep the uber jar as much lightweight as possible.

public class SQSPoller implements Managed, Runnable { private ScheduledExecutorService mainRunner; @Override public void start() throws Exception { mainRunner = Executors.newSingleThreadScheduledExecutor() mainRunner.scheduleWithFixedDelay(this, 0, 100, TimeUnit.MILLISECONDS); } @Override public void run() { // poll SQS here } @Override public void stop() throws Exception { mainRunner.shutdown(); } } 

And in the run() of your Application class, you can register the above class as follows.

environment.lifecycle().manage(new SQSPoller()); 

You can use either scheduleWithFixedDelay() or scheduleAtFixedRate() depending upon your use case.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.