简体   繁体   English

使用dropwizard轮询SQS

[英]Polling SQS using dropwizard

What I am trying to achieve: 我想要实现的目标:

I want to make a dropwizard client that polls Amazon SQS. 我想制作一个投票亚马逊SQS的dropwizard客户端。 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. 有关已处理消息的一些信息将通过API提供。

Why I chose Dropwizard: 我为什么选择Dropwizard:

Seemed like a good choice to make a REST client. 看起来像是制作REST客户端的好选择。 I need to have metrics, DB connections and integrate with some Java services. 我需要有指标,数据库连接并与一些Java服务集成。

What I need help with: 我需要帮助的是:

It is not very clear how and where the SQS polling will fit in a typical dropwizard application. 目前尚不清楚SQS轮询在典型的dropwizard应用程序中的适用方式和位置。
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 . 您可以使用com.google.common.util.concurrent.AbstractScheduledService创建使用者线程,并将其作为ManagedTask添加到dropwizard的环境生命周期中。 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 - Main做这个 -

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. 作为RishikeshDhokare的答案的替代方案,人们还可以继续使用以下代码,这些代码不需要在项目中包含额外的jar作为依赖项,以保持uber jar尽可能轻量级。

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. 在Application类的run()中,您可以按如下方式注册上述类。

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

You can use either scheduleWithFixedDelay() or scheduleAtFixedRate() depending upon your use case. 您可以使用scheduleWithFixedDelay()scheduleAtFixedRate(),具体取决于您的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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