gpt4 book ai didi

java - Spring AMQP : would like to put message to queue and send ACK immediately

转载 作者:搜寻专家 更新时间:2023-11-01 02:50:39 24 4
gpt4 key购买 nike

我编写了 Java 应用程序,它向 RabbitMQ 发送消息。然后 Flume 从 RabbitMQ 队列中获取消息。我感兴趣的是没有人从队列中提取消息,除了 flume。

我的应用程序使用 Spring AMQP Java 插件。

问题:

使用下面的代码,消息进入 RabbitMQ 队列并永远保持“Unknowledges”状态。据我了解,RabbitMQ 正在等待来自 MessageListener 的 ACK,但 MessageListener 永远不会 ACK。有人知道如何解决吗?

代码:

public class MyAmqpConfiguration {

@Autowired
ConnectionFactory connectionFactory;

@Bean
public SimpleMessageListenerContainer messageListenerContainer() {

SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
container.setQueues(activityLogsQueue());
container.setMessageListener(MyMessageListener());
container.setConcurrentConsumers(3);

return container;
}

@Bean(name="myTemplate")
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory);
template.setMessageConverter(MyMessageConverter());
return template;
}
}


public class MyMessageListener implements MessageListener {


public MyMessageListener(MessageConverter converter, MyMessageHandler<MyObject> messageHandler) {
this.converter = converter;
this.messageHandler = messageHandler;
}

@Override
public void onMessage(Message message) {
this.messageHandler.doThings();
}

}

public class MyMessageHandler {

@Autowired
@Qualifier("myTemplate")
RabbitTemplate template;

@Override
public void handleMessage(MyObject thing) {
template.convertAndSend(exchange, routingKey, thing);
}

}


public class MyMessageConverter extends JsonMessageConverter {

@Override
protected Message createMessage(Object object, MessageProperties messageProperties) {
//do things
}

@Override
public Object fromMessage(Message message) throws MessageConversionException {
throw new UnsupportedOperationException("fromMessage is not supported in "+this.getClass().getName());
}


}

最佳答案

如果您不想确认每条消息,那么您可以通过以下方式在 SimpleMessageListenerContainer 上设置 AcknowledgeMode

container.setAcknowledgeMode(AcknowledgeMode.NONE);

看看API reference了解更多信息。

更新:应该是AcknowledgeMode.NONE

Set to AcknowledgeMode.NONE to tell the broker not to expect any acknowledgements, and it will assume all messages are acknowledged as soon as they are sent (this is "autoack" in native Rabbit broker terms). If AcknowledgeMode.NONE then the channel cannot be transactional (so the container will fail on start up if that flag is accidentally set).

关于java - Spring AMQP : would like to put message to queue and send ACK immediately,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11951508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com