SpringBoot:手动确认多条AMQP消息

时间:2017-02-27 12:43:19

标签: multithreading spring-boot rabbitmq spring-amqp spring-rabbitmq

只有在成功处理和存储后,我才需要在兔子监听器中手动识别多条消息。使用的Spring启动配置如下

listener:
  concurrency: 2
  max-concurrency: 20
  acknowledge-mode: manual
  prefetch: 30

消息应一次分批存储20个。只有在成功存储它们时,才应发送多个确认。还存在与存储机制相关联的超时,即使没有20个消息,它也应该在20秒后存储消息。目前,我有以下代码

@Slf4j
@Component    
class EventListener {

  @Autowired
  private EventsStorage eventsStorage
  private ConcurrentMap<Integer, ChannelData> channelEvents = new ConcurrentHashMap<>()

  @RabbitListener(queues = 'event-queue')
  void processEvent(@Payload Event event, Channel channel, @Header(DELIVERY_TAG) long tag) {
    log.debug("Event received for channel $channel.channelNumber")
    channelEvents.compute(channel.channelNumber, { k, channelData -> addEventAndStoreIfNeeded(channel, event, tag, channelData) })
  }

  private ChannelData addEventAndStoreIfNeeded(Channel channel, Event event, long tag, ChannelData channelData) {
    if (channelData) {
      channelData.addEvent(tag, event)
      if (channelData.getDeliveredEvents().size() >=   batchSize) {
        storeAndAckChannelEvents(channel.channelNumber)
      }
      return channelData
    } else {
      ChannelData newChannelData = new ChannelData(channel)
      newChannelData.addEvent(tag, event)
      return newChannelData
    }
  }

  void storeAndAckChannelEvents(Integer channelNumber) {
    channelEvents.compute(channelNumber, { k, channelData ->
      List<DeliveredEvent> deliveredEvents = channelData.deliveredEvents
      if (!deliveredEvents.isEmpty()) {
        def events = deliveredEvents.stream()
          .map({ DeliveredEvent deliveredEvent -> deliveredEvent.event })
          .collect(Collectors.toList())

        eventsStorage.store(events)
        long lastDeliveryTag = deliveredEvents.get(deliveredEvents.size() - 1).deliveryTag
        channelData.channel.basicAck(lastDeliveryTag, true)
        deliveredEvents.clear()
      }
    })
  }

  @Scheduled(fixedRate = 20000L)
  void storeMessagingEvents() {
    channelEvents.forEach({ k, channelData -> storeAndAckChannelEvents(channelData) })
  }

}

其中ChannelDataDeliveredEvent如下

class DeliveredMesssagingEvent {
  int deliveryTag
  Event event
}

class ChannelData {
  Channel channel
  List<DeliveredEvent> deliveredEvents = new ArrayList<>()

  ChannelData(Channel channel) {
    this.channel = channel
  }

  void addEvent(long tag, Event event) {
    deliveredEvents.add(new DeliveredEvent(deliveryTag: tag, event: event))
  }
}

使用的Channelcom.rabbitmq.client.ChannelThe docs关于此接口状态:

  

不得在线程之间共享通道实例。应用程序应该更喜欢每个线程使用一个Channel,而不是跨多个线程共享相同的Channel。

所以,我正在做相反的事情,在SchedulerSimpleMessageListenerContainer工作线程之间共享Channel。我的应用程序的输出是这样的:

[SimpleAsyncTaskExecutor-3] DEBUG EventListener - Event received for channel 2 
[SimpleAsyncTaskExecutor-4] DEBUG EventListener - Event received for channel 3 
[SimpleAsyncTaskExecutor-5] DEBUG EventListener - Event received for channel 1 
[SimpleAsyncTaskExecutor-1] DEBUG EventListener - Event received for channel 5 
[SimpleAsyncTaskExecutor-2] DEBUG EventListener - Event received for channel 4 
[SimpleAsyncTaskExecutor-3] DEBUG EventListener - Event received for channel 2 
[SimpleAsyncTaskExecutor-1] DEBUG EventListener - Event received for channel 5 
[SimpleAsyncTaskExecutor-2] DEBUG EventListener - Event received for channel 4 
[SimpleAsyncTaskExecutor-3] DEBUG EventListener - Event received for channel 2
[pool-4-thread-1] DEBUG EventListener - Storing channel 5 events 
[pool-4-thread-1] DEBUG EventListener - Storing channel 2 events 
...

SimpleMessageListenerContainer工作线程有自己的频道,不会随时间变化。

考虑到我同步了SchedulerSimpleMessageListenerContainer工作线程,有没有人看到为什么这段代码不是线程安全的?

我是否应该尝试在Spring启动时手动执行多条消息?

1 个答案:

答案 0 :(得分:1)

只要你同步线程就可以了。

请记住,如果连接丢失,您将获得一个新的消费者,并且您的同步线程将具有过时的数据(未重新发送的消息将被重新传递)。

但是,您也可以使用container idle events

当消费者线程在那段时间闲置时,该事件将在同一个侦听器线程上发布,因此您可以在那里执行定时确认,而不必担心同步。

如果连接丢失,您还可以检测到consumer failed events