Aws集成弹簧:仅保证平方

时间:2016-07-28 18:57:44

标签: spring spring-integration

我试图只允许在给定时间处理sqs队列中的一个项目。目前它只会提取队列中的一条消息,但它会在每次轮询时保持这样做。

ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
      executor.setCorePoolSize(2);
      executor.setMaxPoolSize(2);
      executor.setQueueCapacity(10);
      executor.setThreadNamePrefix("test-");
      executor.initialize();
      return executor;

      new SqsMessageDrivenChannelAdapter(amazon));
      adapter.setMaxNumberOfMessages(1);
      adapter.setSendTimeout(2000);
      adapter.setVisibilityTimeout(1200);
      adapter.setWaitTimeOut(20);
      adapter.setTaskExecutor(this.asyncTaskExecutor());

问题似乎在ThreadPoolTask​​Executor中,我对此有所了解。由于队列大小为10,它每次都会提升,直到它满了吗?

将maxPoolSize设置为1.导致

Caused by: java.util.concurrent.RejectedExecutionException: Task org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer$SignalExecutingRunnable@406354e5 rejected from java.util.concurrent.ThreadPoolExecutor@30d53b7[Running, pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 0]
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
    at org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor.execute(ThreadPoolTaskExecutor.java:293)
    ... 6 common frames omitted

1 个答案:

答案 0 :(得分:1)

问题是你的ThreadPoolExecutor设置为BlockingQueue大小为10,使用2个线程来消耗此队列中的消息。因此,在任何给定时间,您可以有2个线程同时处理消息。如果将PoolSize设置为1,则可以保证在给定时间只处理一条消息。

来自源代码:

/*
 * Proceed in 3 steps:
 *
 * 1. If fewer than corePoolSize threads are running, try to
 * start a new thread with the given command as its first
 * task.  The call to addWorker atomically checks runState and
 * workerCount, and so prevents false alarms that would add
 * threads when it shouldn't, by returning false.
 *
 * 2. If a task can be successfully queued, then we still need
 * to double-check whether we should have added a thread
 * (because existing ones died since last checking) or that
 * the pool shut down since entry into this method. So we
 * recheck state and if necessary roll back the enqueuing if
 * stopped, or start a new thread if there are none.
 *
 * 3. If we cannot queue task, then we try to add a new
 * thread.  If it fails, we know we are shut down or saturated
 * and so reject the task.

你正在打第三种情况。