为什么spring-amqp的spring amqp 消息确认认失败会一直发原来的消息

1541人阅读
org.springframework.amqp
spring-rabbit
1.2.0.RELEASE
just Java...
public static void main(final String... args) throws Exception {
ConnectionFactory cf = new CachingConnectionFactory();
// set up the queue, exchange, binding on the broker
RabbitAdmin admin = new RabbitAdmin(cf);
Queue queue = new Queue(&myQueue&);
admin.declareQueue(queue);
TopicExchange exchange = new TopicExchange(&myExchange&);
admin.declareExchange(exchange);
admin.declareBinding(
BindingBuilder.bind(queue).to(exchange).with(&foo.*&));
// set up the listener and container
SimpleMessageListenerContainer container =
new SimpleMessageListenerContainer(cf);
Object listener = new Object() {
public void handleMessage(String foo) {
System.out.println(foo);
MessageListenerAdapter adapter = new MessageListenerAdapter(listener);
container.setMessageListener(adapter);
container.setQueueNames(&myQueue&);
container.start();
// send something
RabbitTemplate template = new RabbitTemplate(cf);
template.convertAndSend(&myExchange&, &foo.bar&, &Hello, world!&);
Thread.sleep(1000);
container.stop();
Or, the Spring way...
public static void main(final String... args) throws Exception {
AbstractApplicationContext ctx =
new ClassPathXmlApplicationContext(&context.xml&);
RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
template.convertAndSend(&Hello, world!&);
Thread.sleep(1000);
ctx.destroy();
public class Foo {
public void listen(String foo) {
System.out.println(foo);
id=&connectionFactory&
id=&amqpTemplate& connection-factory=&connectionFactory&
exchange=&myExchange& routing-key=&foo.bar&
connection-factory=&connectionFactory&
name=&myQueue&
name=&myExchange&
queue=&myQueue& pattern=&foo.*&
connection-factory=&connectionFactory&
ref=&foo& method=&listen& queue-names=&myQueue&
id=&foo& class=&foo.Foo&
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:532514次
积分:5106
积分:5106
排名:第3820名
原创:132篇
转载:54篇
评论:53条
(2)(1)(1)(1)(4)(4)(6)(4)(1)(8)(4)(5)(12)(5)(2)(10)(12)(4)(1)(9)(7)(20)(10)(8)(3)(4)(3)(4)(4)(6)(3)(18)rabbitTemplate消息DeliveryMode设置 - 博客频道 - CSDN.NET
每天积累一点,一年后你会发现,自己变化很大
静下心来,一步一步,学习开源项目。
分类:nosqlmq
在用spring和rabbitmq整合 对message的DeliveryMode的设置有两种方式
一,在发送消息的时候设置DeliveryMode
1,生产者调用rabbimqTemplate发送消息的时候,调用接口
public Message sendAndReceive(final String exchange, final String routingKey, final Message message)
throws AmqpException {
return this.doSendAndReceive(exchange, routingKey, message);
2,对其中的参数Message设置MessageProperties属性
public class MessageProperties implements Serializable {
static final String DEFAULT_CONTENT_TYPE = CONTENT_TYPE_BYTES;
static final MessageDeliveryMode DEFAULT_DELIVERY_MODE = MessageDeliveryMode.PERSISTENT;//默认消息是持久化
二,在spring和rabbitmq的配置文件中配置自定义MessageConverter
public interface MessageConverter {
* Convert a Java object to a Message.
* @param object the object to convert
* @param messageProperties The message properties.
* @return the Message
* @throws MessageConversionException in case of conversion failure
Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionE
* Convert from a Message to a Java object.
* @param message the message to convert
* @return the converted Java object
* @throws MessageConversionException in case of conversion failure
Object fromMessage(Message message) throws MessageConversionE
配置其默认实现
&bean class=&org.springframework.amqp.rabbit.core.RabbitTemplate&&
&property name=&connectionFactory& ref=&rabbitConnectionFactory&/&
&property name=&messageConverter& ref=&&&
&bean class=&org.springframework.amqp.support.converter.SimpleMessageConverter&&
排名:第602名
(57)(391)(71)(100)(63)(7)(194)(1)(38)(21)(105)(194)(58)(15)(57)(47)(1)(18)(9)(24)(42)(15)(12)(14)(45)(11)(1)(1)(11)(26)(27)(13)(8)(4)(3)(3)(31)(226)(79)(19)(24)(53)(25)(9)(37)(9)(126)(4)(3)(14)(8)(9)(10)(3)(2)(1)(20)(0)
http://www.vpser.net/2486人阅读
java(114)
在官方例子/spring-projects/spring-amqp-samples/tree/master/helloworld进行整理
一.Producer端:
1.定义一个队列名,配置连接工厂,发送工具类.
public static final String helloWorldQueueName = &hello.world.queue&;
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(&192.168.1.118&);
return connectionF
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
public RabbitTemplate rabbitTemplate() {
//没有指定默认routingKey,由发送时指定
return new RabbitTemplate(connectionFactory());
2.设置一个定时计划,每3秒发送一次消息(模拟发送消息)
//配置一个定时计划,每隔3秒发一次消息
public BeanPostProcessor postProcessor() {
return new ScheduledAnnotationBeanPostProcessor();
public ScheduledProducer scheduledProducer() {
return new ScheduledProducer();
static class ScheduledProducer {
@Autowired
private volatile RabbitTemplate rabbitT
private final AtomicInteger counter = new AtomicInteger();
@Scheduled(fixedRate = 3000)
public void sendMessage() {
String msg=&Hello World & + counter.incrementAndGet();
System.out.println(msg);
rabbitTemplate.convertAndSend(ProducerConfig.helloWorldQueueName,msg);
二.Consumer端
1.与Producer端连接rabbitmq服务器配置类似,使用相同的队列名,配置连接工厂,发送工具类.
public static final String helloWorldQueueName = &hello.world.queue&;
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(&192.168.1.118&);
return connectionF
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
public RabbitTemplate rabbitTemplate() {
//没有指定默认queueName,在接收的监听器指定
return new RabbitTemplate(connectionFactory());
2.设置一个接收消息的监听器.
public SimpleMessageListenerContainer listenerContainer() {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory());
container.setQueueNames(ConsumerConfig.helloWorldQueueName);
MessageListenerAdapter adapter= new MessageListenerAdapter(new HelloWorldHandler());
//没有指定默认的监听器方法,则使用默认的监听器方法名handleMessage
container.setMessageListener(adapter);
其中HelloWorldHandler.java如下:
public class HelloWorldHandler {
public void handleMessage(String text) {
System.out.println(&Received: &+ text);
三.测试.启动Producer端和Consumer端顺序没有限制.
1.Producer端
public static void main(String[] args) {
new AnnotationConfigApplicationContext(ProducerConfig.class);
2.Consumer端
public static void main(String[] args) {
new AnnotationConfigApplicationContext(ConsumerConfig.class);
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:542508次
积分:6737
积分:6737
排名:第2414名
原创:160篇
转载:53篇
评论:112条
(1)(4)(4)(4)(15)(5)(4)(7)(8)(4)(3)(1)(3)(5)(10)(10)(9)(14)(2)(12)(11)(5)(8)(8)(9)(12)(1)(3)(14)(1)(4)(11)spring amqp ssl 及 消息确认该如何配置 - 开源中国社区
当前访客身份:游客 [
当前位置:
spring amqp 如何配置SSL?
消息应答该如何配置?这个是java代码的设置(channel.basicAck(delivery.getEnvelope().getDeliveryTag(),&false)),在配置文件中该配置哪个属性达到这个效果?
共有0个答案
更多开发者职位上
有什么技术问题吗?
娃娃菜的其它问题
类似的话题

我要回帖

更多关于 spring boot amqp 的文章

 

随机推荐