Spring整合RabbitMQ 一直收到重复消息怎么处理?

汉唐 发布于 2015/06/04 09:57
阅读 10K+
收藏 1

使用Spring的AqmpTemplate整合rabbitMQ,然后指定了一个listener,但是现在一直收到重复的消息,请问怎么破?

配置信息

<!-- rabbit MQ -->
	<!-- 连接服务配置 -->
	<rabbit:connection-factory id="connectionFactory"
		host="${mq.host}" username="${mq.username}" password="${mq.password}"
		port="${mq.port}" />

	<rabbit:admin connection-factory="connectionFactory" />

	<!-- queue 队列声明 -->
	<rabbit:queue id="spr_queue" durable="true" auto-delete="false"
		exclusive="false" name="spr_queue" />

	<!-- exchange queue binging key 绑定 -->
	<rabbit:direct-exchange name="spr_exchange"
		durable="true" auto-delete="false" id="spr_exchange">
		<rabbit:bindings>
			<rabbit:binding queue="spr_queue" key="queue_one_key" />
		</rabbit:bindings>
	</rabbit:direct-exchange>

	<rabbit:template exchange="spr_exchange" id="amqpTemplate"
		connection-factory="connectionFactory" />

	<rabbit:listener-container
		connection-factory="connectionFactory" acknowledge="auto">
		<rabbit:listener queues="spr_queue" ref="queueOneLitener" />
	</rabbit:listener-container>

	<bean id="queueOneLitener" class="com.my.base.RabbitMqListener" />



二、发送代码
amqpTemplate.convertAndSend("queue_one_key", "1111111111Hello queue!");



三、监听器
public class RabbitMqListener implements ChannelAwareMessageListener {


	@Override
	public void onMessage(Message message, Channel channel) throws Exception {
		String str = new String(message.getBody());
		// TODO 接受消息
		System.out.println(message.getMessageProperties().getMessageId());
		System.out.println("Recived msg:" + str);
	}
}



现在一直收到同一条重复消息,控制台一直打印
Recived msg:Hello queue!
null
Recived msg:Hello queue!
null
Recived msg:Hello queue!
null
Recived msg:Hello queue!
null
Recived msg:Hello queue!
null
Recived msg:Hello queue!
null
Recived msg:Hello queue!
null
Recived msg:Hello queue!
null
Recived msg:Hello queue!
null
Recived msg:Hello queue!
null
Recived msg:Hello queue!


请问是哪里的配置有什么问题吗?我是参考这位大侠的文章进行配置的

http://my.oschina.net/yilian/blog/174244

加载中
1
夜色霓虹灯
夜色霓虹灯

监听的代码改成这样试试

public class RabbitMqListener implements MessageListener {

	@Override
	public void onMessage(Message message) {
		try {
			String str = new String(message.getBody());
			// TODO 接受消息
			System.out.println(message.getMessageProperties().getMessageId());
			System.out.println("Recived msg:" + str);
		} catch (Exception e) {
			// TODO: handle exception
		}

	}
}




0
秀川
秀川
抛异常了
0
貓小樂
貓小樂
同问呐,这些框架的重复消费如何解决
汉唐
汉唐
你把ack设为自动的,然后再看下处理消息的时候有没有抛异常
0
19viw2
19viw2

onMessage最后来一句

channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

返回顶部
顶部