如何使用Spring将JMS消息转换为对象

时间:2018-12-04 19:36:49

标签: spring jms jackson2

我是Spring的新手。我尝试将对象(Temperature.class)作为JMS消息发送,将其转换为JSON并再次接收,然后自动将其再次转换为对象。 发送和转换可以工作,也可以接收,但不会将其转换回正确的类型。

也许我需要设置typeIdMappings。但是首先我不确定这是否是原因,其次我现在不知道如何在Spring XML配置中对其进行配置。也许有人可以给我提示如何用XML做到这一点。

发件人Spring配置:

    <jee:jndi-lookup id="connectionFactory" jndi-name="ConnectionFactory" />
    <jee:jndi-lookup id="temperatureQueueJndi" jndi-name="jms/queue/TemperaturesQ" />

<bean id="temperatureSender" class="jms.sender.TemperatureSender">
    <property name="queue" ref="temperatureQueueJndi" />
</bean>

<bean id="jmsMsgConverter"
    class="org.springframework.jms.support.converter.MappingJackson2MessageConverter">
    <property name="targetType" value="TEXT" />
    <property name="typeIdPropertyName" value="_type" />

</bean>

<bean id="jmsMsgTemplate"
    class="org.springframework.jms.core.JmsMessagingTemplate">
    <property name="connectionFactory" ref="connectionFactory" />
    <property name="jmsMessageConverter" ref="jmsMsgConverter" />
</bean>

<context:component-scan
    base-package="jms.sender" />

发件人代码:

public class TemperatureSender {

@Autowired 
private ClassPathXmlApplicationContext applicationContext;

@Autowired 
@Qualifier("jmsMsgTemplate")
private JmsMessagingTemplate jmsTemplate;

private Queue queue;

public TemperatureSender() {
}

public void setQueue(Queue queue) {
    this.queue = queue;
}

public void sendMessage(final Temperature message) {
    this.jmsTemplate.convertAndSend(this.queue, message);
}

}

接收器弹簧配置:

    <jee:jndi-lookup id="connectionFactory" jndi-name="ConnectionFactory" />
<jee:jndi-lookup id="temperatureQueueJndi" jndi-name="jms/queue/TemperaturesQ" />

<bean id="temperatureReceiver" class="jms.receiver.TemperatureReceiver" />

<bean id="jmsContainer"
    class="org.springframework.jms.listener.DefaultMessageListenerContainer"
    lazy-init="false">

    <property name="connectionFactory" ref="connectionFactory" />
    <property name="destination" ref="temperatureQueueJndi" />
    <property name="messageListener" ref="temperatureReceiver" />
    <property name="clientId" value="1234" />
    <property name="sessionAcknowledgeMode" value="1" /> <!-- 1 = AUTO_ACKNOWLEDGE -->
    <property name="messageConverter" ref="jmsMsgConverter"/>
</bean>

<bean id="jmsMsgConverter" class="org.springframework.jms.support.converter.MappingJackson2MessageConverter">
    <property name="targetType" value="TEXT" />
    <property name="typeIdPropertyName" value ="_type" />
</bean>

接收方代码:

public class TemperatureReceiver implements MessageListener {

@Autowired 
private ClassPathXmlApplicationContext applicationContext;

@Autowired 
private DefaultMessageListenerContainer msgListenerContainer;

@Override public void onMessage(final Message message) {

    if (message instanceof TextMessage) {
        try {
            System.out.println(((TextMessage) message).getText());
        } // try
        catch (JMSException e) {
            throw new RuntimeException(e);
        } // catch
    }
    else {
        throw new IllegalArgumentException("This receiver only processes text messages!");
    } // if
}

}

0 个答案:

没有答案