Spring Boot RabbitMQ空指针异常错误

时间:2018-08-11 22:01:38

标签: spring-rabbitmq

我正在将RabbitMQ与Spring Boot结合使用来代理两个服务之间的消息。我能够接收消息并对其进行格式化,但是当我在onMessage方法中调用服务类时,出现空指针异常错误。这是我的接收消息的消息监听器类

public class QueueListener implements MessageListener{
    @Autowired
    private QueueProcessor queueProcessor;

    @Override
    public void onMessage(Message message) {
        String msg = new String(message.getBody());
        String output = msg.replaceAll("\\\\", "");
        String jsonified = output.substring(1, output.length()-1);

        JSONArray obj = new JSONArray(jsonified);

        queueProcessor.processMessage(obj);
    }
}

调用方法processMessage会引发空指针异常 enter image description here

有人可以指出我做错了什么吗?

2 个答案:

答案 0 :(得分:0)

确保QueueListenercomponent类或service类,可以由Spring IoC管理。否则,配置类不能将其设置为bean,因为这只是一个普通的Java类,需要放在容器@runtime中。

因此,当您在yr config类中编写new QueueListener()时,则在实例化config类时,Java类不在SpringContext中,因此是null

希望这有助于解决此问题!

答案 1 :(得分:-1)

我发现问题出在RabbitMqConfig类中。这是导致错误的代码:

@Configuration
public class RabbitMqConfig {
private static final String QUEUE_NAME = "my.queue.name";

    @Bean
    public ConnectionFactory connectionFactory() {
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory("<url.to.rabbit>");
    connectionFactory.setUsername("<username>");
    connectionFactory.setPassword("<password>");
    return connectionFactory;;
    }

    @Bean
    public Queue simpleQueue() {
        return new Queue(QUEUE_NAME);
    }

    @Bean
    public MessageConverter jsonMessageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(connectionFactory());
        template.setRoutingKey(QUEUE_NAME);
        template.setMessageConverter(jsonMessageConverter());
        return template;
    }

    @Bean
    public SimpleMessageListenerContainer userListenerContainer() {
        SimpleMessageListenerContainer listenerContainer = new SimpleMessageListenerContainer();
        listenerContainer.setConnectionFactory(connectionFactory());
        listenerContainer.setQueues(simpleQueue());
        listenerContainer.setMessageConverter(jsonMessageConverter());
        listenerContainer.setMessageListener(new QueueListener());
        listenerContainer.setAcknowledgeMode(AcknowledgeMode.AUTO);
        return listenerContainer;
    }
}

listenerContainer.setMessageListener(new QueueListener());行是错误的来源。我通过自动装配类而不是使用new解决了它。这是工作代码

@Configuration
public class RabbitMqConfig {
private static final String QUEUE_NAME = "my.queue.name";

    @Autowired
    private QueueListener queueListener;

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory("<url.to.rabbit>");
        connectionFactory.setUsername("<username>");
        connectionFactory.setPassword("<password>");
        return connectionFactory;
    }

    @Bean
    public Queue simpleQueue() {
        return new Queue(QUEUE_NAME);
    }

    @Bean
    public MessageConverter jsonMessageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(connectionFactory());
        template.setRoutingKey(QUEUE_NAME);
        template.setMessageConverter(jsonMessageConverter());
        return template;
    }

    /*@Bean
    public SimpleMessageListenerContainer userListenerContainer() {
        SimpleMessageListenerContainer listenerContainer = new SimpleMessageListenerContainer();
        listenerContainer.setConnectionFactory(connectionFactory());
        listenerContainer.setQueues(simpleQueue());
        listenerContainer.setMessageConverter(jsonMessageConverter());
        listenerContainer.setMessageListener(queueListener);
        listenerContainer.setAcknowledgeMode(AcknowledgeMode.AUTO);
        return listenerContainer;
    }
}

希望这对其他人有帮助