简体   繁体   中英

Polling SQS using dropwizard

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.

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM