简体   繁体   中英

Spring 3 and Rabbit MQ integration (not Spring Boot)

I'm having difficulty getting a Spring 3 application to integrate with RabbitMQ, in order to receive messages from a queue (I do not need to send messages).

Part of the challenge is much of the documentation now relates to Spring Boot. The related Spring guide is helpful, but following the steps does not seem to work in my case. For instance, the guide includes the text:

The message listener container and receiver beans are all you need to listen for messages.

So I have setup the listener container and receiver beans with the following code.

Setting up message handler

@Component
public class CustomMessageHandler {

    public void handleMessage(String text) {
        System.out.println("Received: " + text);
    }
}

Setting up configuration

@Configuration
public class RabbitConfig {

    @Bean
    public RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory){
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setRoutingKey("queue-name");
        return rabbitTemplate;
    }

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost("...host...");
        connectionFactory.setPort(5671);
        connectionFactory.setVirtualHost("...virtual host..");
        connectionFactory.setUsername("...username...");
        connectionFactory.setPassword("...password...");
        return connectionFactory;
    }

    @Bean
    public MessageListenerAdapter messageListenerAdapter(CustomMessageHandler messageHandler) {
        return new MessageListenerAdapter(messageHandler, "handleMessage");
    }

    @Bean
    public SimpleMessageListenerContainer listenerContainer(ConnectionFactory connectionFactory,
                                                            MessageListenerAdapter messageListenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setQueueNames("queue-name");
        container.setConnectionFactory(connectionFactory);
        container.setMessageListener(messageListenerAdapter);
        return container;
    }
}

Unfortunately with this setup, the application will start up, but it never triggers the message handler. The queue it is trying to read from also has one message sitting in it, waiting to be consumed.

Any ideas on something that is missing, or appears misconfigured?

Thanks to some dependency management assistance from @GaryRussell, I was able to see that the version of spring-rabbit and spring-amqp were too recent. Using the older 1.3.9.RELEASE unfortunately proved to add additional challenges.

Some other assistance came in the form of using an actual RabbitMQ Java client. This option was much simpler to implement, and avoided the dependency problems. Ultimately I needed to include the following dependency:

<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>5.7.3</version>
</dependency>

And then I simply followed their documentation on creating a connection , and consuming messages .

Voila, it works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM