spring boot spring rabbit:如何创建一个兔子模板来在监听器中发送消息

时间:2016-10-19 00:43:35

标签: spring-amqp

我有一个从Rabbitmq接收消息的简单侦听器。我想修改消息并将其发布到新的交换/队列。我无法让兔子模板在监听器中工作。任何帮助,将不胜感激。我的代码在下面,当我试图自动装载我得到的模板时

Caused by: java.lang.NullPointerException: null
    at com.example.Consumer.onMessage(Consumer.java:27) ~[classes!/:0.0.1-SNAPSHOT]

类:

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class RabbittestApplication {

    public static void main(String[] args) {
        SpringApplication.run(RabbittestApplication.class, args);
    }
}


package com.example;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
public class RabbitConfig {

    private static final String SIMPLE_MESSAGE_QUEUE = "qDLX1.dlx";

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory("RabbitErl19");
        connectionFactory.setUsername("gregg");
        connectionFactory.setPassword("gregg");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("dlxtest");
        return connectionFactory;
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(connectionFactory());
        return template;
    }



    @Bean
    public SimpleMessageListenerContainer listenerContainer() {
        SimpleMessageListenerContainer listenerContainer = new SimpleMessageListenerContainer();
        listenerContainer.setConnectionFactory(connectionFactory());
        listenerContainer.setQueueNames(SIMPLE_MESSAGE_QUEUE);
        listenerContainer.setMessageListener(new Consumer());
        listenerContainer.setAcknowledgeMode(AcknowledgeMode.AUTO);
        return listenerContainer;
    }



}

package com.example;


import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class Consumer implements MessageListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void onMessage(Message message) {


        System.out.println("Body: "+new String(message.getBody()));


        System.out.println();


        message.getMessageProperties().setExpiration("5000");        
        rabbitTemplate.send("xDLX1.delay", "xq1.retry", message);



    }
}

2 个答案:

答案 0 :(得分:2)

这是你的代码:

listenerContainer.setMessageListener(new Consumer());

如您所见@Autowired无法正常工作,这是绝对正常的。仅仅因为你绕过了依赖注入。

由于Consumer标有@Component,您只需将其注入SimpleMessageListenerContainer bean定义:

@Bean
public SimpleMessageListenerContainer listenerContainer(Consumer consumer) 

答案 1 :(得分:0)

谢谢你做到了,我将我的代码更改为以下内容并且它有效。非常感谢您的帮助!

@Autowired
private Consumer consumer;


@Bean
public SimpleMessageListenerContainer listenerContainer() {
    SimpleMessageListenerContainer listenerContainer = new SimpleMessageListenerContainer();        
    listenerContainer.setConnectionFactory(connectionFactory());
    listenerContainer.setQueueNames(SIMPLE_MESSAGE_QUEUE);
    listenerContainer.setMessageListener(consumer);
    listenerContainer.setAcknowledgeMode(AcknowledgeMode.AUTO);
    return listenerContainer;
}