Home > Java > javaTutorial > body text

Using Spring and AMQP to send and receive messages in java (Part 2)

怪我咯
Release: 2017-06-25 10:02:27
Original
2102 people have browsed it

The previous article talked about using RabbitMQ to send messages, and this article will talk about receiving messages.
In traditional JMS, there are two ways to obtain information from the queue, the synchronous method using JmsTemplate and the asynchronous method using message-driven pojo. Spring AMQP also provides a similar way to obtain messages sent via AMQP.

Use RabbitTemplate to receive messages

The simplest method of receiving information provided by RabbitTemplate is the receive() method, through which you can obtain a Message object from the queue:

Message message = rabbit.receive("spittle.test.queue");
Copy after login

Or you can also obtain the default queue of messages through configuration. This is achieved by setting the queue attribute when configuring the template:

<template id="rabbitTemplate" 
    connection-factory="connectionFactory"exchange="spittle.test.exchange" 
    routing-key="spittle.test" 
    queue="spittle.test.queue" />
Copy after login

This way If so, when calling the receive() method, the message can be obtained from the default queue without setting any parameters:

Message message = rabbit.receive( );
Copy after login

After obtaining the Message object, you generally need to set its body attribute Convert the byte array in to the desired object. Just like converting the domain object to the Message when sending, converting the received Message to the domain object is also tedious. Here you can consider using RabbitTemplate's receiveAndConvert() method as an alternative:

Spittle spittle = (Spittle) rabbit.receiveAndConvert("spittle.test.queue");
Copy after login

The receiveAndConvert() method will use the same message converter as the sendAndConvert() method to convert the Message object into the original type.
Calling the receive() and receiveAndConvert() methods will return immediately. If there are no waiting messages in the queue, you will get null. At this time, programmers generally need to manage polling and necessary threads themselves to implement queue monitoring. If you don't want to poll synchronously every time to wait for the message to arrive, you can use the message-driven pojo provided by Spring AMQP. Let's take a look at using the message-driven pojo to receive messages.

Use message-driven pojo to receive messages

If you want to consume Spittle objects asynchronously in message-driven pojo, you must first solve the pojo itself, as shown below SpittleTestHandler Playing this role:

public class SpittleTestHandler {public void handleSpittleTest (Spittle spittle) {
        ...
    }
}
Copy after login

In fact, this class does not depend on AMQP. No matter what mechanism is passed to the Spittle object, it can handle it.
Here you also need to declare SpittleTestHandler as a bean in the Spring application context:

<bean id="spittleListener"class="com.***.spittr.test.SpittleTestHandler">
Copy after login

Finally, declare a listener container and listener, which can be called when the message arrives SpittleTestHandler, the configuration is as follows:

<listener-container connection-factory="connectionFactory"><listener ref="spittleListener" 
          method="handleSpittleTest"  queue-names="spittle.test.queue" /></listener-container>
Copy after login

The and elements above are both from the rabbit namespace. And specify the queue to be monitored through the queue-names attribute. Here, only one queue to be monitored is set. If multiple queues are to be set, separate them with commas. At this point, the message reception is completed. After getting the message, you can perform the corresponding processing in the corresponding method.

The above is the detailed content of Using Spring and AMQP to send and receive messages in java (Part 2). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template