Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive what was sent by convertAndSend?

Tags:

java

spring

jms

I'm reading Spring Framework reference, chapter about JMS integration. There are some examples for sending text messages and asynchronously receiving them (by listeners). And there is also an example for JmsTemplate function convertAndSend which converts given object to a message. The reference says:

By using the converter, you and your application code can focus on the business object that is being sent or received via JMS and not be concerned with the details of how it is represented as a JMS message.

But there is no example for receiving such messages. They mention function receiveAndConvert but, unfortunately, it receives synchronously.
So how am I to receive it asynchronously? Must I be aware that when I convertAndSend a Map, the resulting message will be a MapMessage, and just check in my listener for this type of message and handle it? But they promised I'm not to be concerned with the details of how it is represented as a JMS message.
So is there a better way?

like image 628
Tadeusz Kopec for Ukraine Avatar asked Sep 03 '09 13:09

Tadeusz Kopec for Ukraine


1 Answers

I know it's been a while since this was asked, but I had the same problem, solved it and wanted to give an explicit code example here.

Here's my MessageListener. This implements the onMessage(Message) method to intercept messages asynchronously.

package com.package.amqp;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.support.converter.JsonMessageConverter;

import com.package.model.User;

public class TestListener implements MessageListener {

        public void onMessage(Message message) {
            JsonMessageConverter jmc = new JsonMessageConverter();
            User u = (User)jmc.fromMessage(message);
            System.out.println("received: " + u.getFirstName());
        }
}

The messages are then converted using the standard JsonMessageConvertor in my case as this is the messageConvertor I plugged into my rabbitTemplate bean.

<bean id="rabbitConnectionFactory" class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory">
    <constructor-arg value="10.10.1.2"/>
    <property name="username" value="guest"/>
    <property name="password" value="guest"/>
</bean>

<bean class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
    <property name="connectionFactory" ref="rabbitConnectionFactory"/>
    <property name="queueName" value="queue.helloWorld"/>
    <property name="messageListener" ref="someListener"/>
</bean>

<bean id="someListener" class="com.package.amqp.TestListener"></bean>

<bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
    <property name="connectionFactory" ref="rabbitConnectionFactory"/>
    <property name="messageConverter">
        <bean class="org.springframework.amqp.support.converter.JsonMessageConverter"/>
    </property>
</bean>

Hope this helps someone! Owen

like image 128
Owen O Byrne Avatar answered Oct 20 '22 21:10

Owen O Byrne