MessageConverter的作用主要有两方面,一方面它可以把我们的非标准化Message对象转换成我们的目标Message对象,这主要是用在发送消息的时候;另一方面它又可以把我们的Message对象转换成对应的目标对象,这主要是用在接收消息的时候。


下面我们就拿发送一个对象消息来举例, 传输USer对象:

package com.xinwei.order.entity;

import java.io.Serializable;
import java.util.Date; public class User implements Serializable{ private static final long serialVersionUID = 1L; private String id;
private String name;
private int age;
private Date birthDate; public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
}

定义转换器

import java.io.Serializable;  

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session; import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter; /**
* spring 消息转换器
* @author slimina
*
*/
public class UserMessageConverter implements MessageConverter { public Message toMessage(Object object, Session session)
throws JMSException, MessageConversionException {
return session.createObjectMessage((Serializable) object);
} public Object fromMessage(Message message) throws JMSException,
MessageConversionException {
ObjectMessage objMessage = (ObjectMessage) message;
return objMessage.getObject();
}
}

消息接收

import com.google.gson.Gson;  

public class UserMessageListener {  

    public void receiveMessage(User user) {
System.out.println(new Gson().toJson(user));
} public void receiveMessage(String message) {
System.out.println("接收到一个纯文本消息,消息内容是:" + message);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd"> <!-- 配置JMS连接工厂 -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- Session缓存数量 -->
<property name="sessionCacheSize" value="" />
<property name="targetConnectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<!-- MQ地址 -->
<property name="brokerURL" value="tcp://192.168.233.128:61616" />
<property name="trustAllPackages" value="true"/>
<!-- 是否异步发送 -->
<property name="useAsyncSend" value="false" />
</bean>
</property>
</bean> <!-- 定义队列 -->
<bean id="testQueue" class="org.apache.activemq.command.ActiveMQQueue">
<property name="physicalName" value="spring.queue.converte" />
</bean> <!-- jmsTemplate,用于向任意地址发送消息 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="testQueue" />
<property name="sessionTransacted" value="false" />
<!-- receiveTimeout表示接收消息时的超时时间 -->
<property name="receiveTimeout" value="" />
<!-- 消息转换器 -->
<property name="messageConverter" ref="userMessageConverter"/>
</bean>
<!-- 类型转换器 -->
<bean id="userMessageConverter" class="com.xinwei.order.controller.UserMessageConverter"/>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd"> <!-- 配置JMS连接工厂 -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- Session缓存数量 -->
<property name="sessionCacheSize" value="" />
<!-- 接收者ID 持久化订阅 -->
<property name="targetConnectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<!-- MQ地址 -->
<property name="brokerURL" value="tcp://192.168.233.128:61616" />
<property name="trustAllPackages" value="true"/>
</bean>
</property>
</bean> <!-- <bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration">
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="configuration" ref="jmsConfig"/>
</bean> --> <!-- 定义队列 -->
<bean id="testQueue" class="org.apache.activemq.command.ActiveMQQueue">
<property name="physicalName" value="spring.queue.converte" />
</bean> <!-- 异步接收Queue消息Container -->
<bean id="queueContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="testQueue" />
<!-- 使用MessageListenerAdapter来作为消息监听器 -->
<property name="messageListener" ref="messageListenerAdapter" />
<property name="receiveTimeout" value="" />
</bean> <!-- 消息监听适配器 -->
<bean id="messageListenerAdapter"
class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<property name="delegate" ref="userMessageListener" />
<property name="defaultListenerMethod" value="receiveMessage" />
<property name="messageConverter" ref="userMessageConverter" />
</bean> <bean id="userMessageListener"
class="com.xinwei.order.controller.UserMessageListener"></bean>
<!-- 类型转换器 -->
<bean id="userMessageConverter"
class="com.xinwei.order.controller.UserMessageConverter" />
</beans>
package com.xinwei.util.quartz;

import java.util.Date;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate; import com.xinwei.order.entity.User; @SuppressWarnings("resource")
public class ProducerMain { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:msgconverter/producer.xml");
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
User user = new User();
user.setId("");
user.setName("jack");
user.setAge();
user.setBirthDate(new Date());
jmsTemplate.convertAndSend(user);
}
} ----
package com.xinwei.util.quartz; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ConsumerMain {
@SuppressWarnings("resource")
public static void main(String[] args) {
new ClassPathXmlApplicationContext("classpath:msgconverter/consumer.xml");
}
} ---- SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/maven_repository/org/slf4j/slf4j-log4j12/1.7./slf4j-log4j12-1.7..jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/maven_repository/org/apache/activemq/activemq-all/5.13./activemq-all-5.13..jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
-- ::, [main] INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@11cf437c: startup date [Mon Dec :: CST ]; root of context hierarchy
-- ::, [main] INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [msgconverter/consumer.xml]
-- ::, [main] INFO [org.springframework.context.support.DefaultLifecycleProcessor] - Starting beans in phase
-- ::, [main] INFO [org.springframework.jms.connection.CachingConnectionFactory] - Established shared JMS Connection: ActiveMQConnection {id=ID:fangping---:,clientId=null,started=false}
{"id":"","name":"jack","age":,"birthDate":"Dec 4, 2017 9:51:44 AM"}
 org.apache.activemq.command.ActiveMQObjectMessage.getObject(ActiveMQObjectMessage.java:206)
解决方法: http://activemq.apache.org/objectmessage.html




ActiveMQ之spring集成消息转换器MessageConverter的更多相关文章

  1. ActiveMQ与spring集成实现Queue模式

    ActiveMQ可以和spring很好的集成,下面我们来看看,如何做个集成的demo. (1)pom.xml引入相关jar <!-- spring相关 begin --> <depe ...

  2. SpringMVC自定义配置消息转换器踩坑总结

    问题描述 最近在开发时候碰到一个问题,springmvc页面向后台传数据的时候,通常我是这样处理的,在前台把数据打成一个json,在后台接口中使用@requestbody定义一个对象来接收,但是这次数 ...

  3. Spring MVC自定义消息转换器(可解决Long类型数据传入前端精度丢失的问题)

    1.前言 对于Long 类型的数据,如果我们在Controller层通过@ResponseBody将返回数据自动转换成json时,不做任何处理,而直接传给前端的话,在Long长度大于17位时会出现精度 ...

  4. 从零开始学 Java - Spring 集成 ActiveMQ 配置(一)

    你家小区下面有没有快递柜 近两年来,我们收取快递的方式好像变了,变得我们其实并不需要见到快递小哥也能拿到自己的快递了.对,我说的就是类似快递柜.菜鸟驿站这类的代收点的出现,把我们原来快递小哥必须拿着快 ...

  5. 从零开始学 Java - Spring 集成 ActiveMQ 配置(二)

    从上一篇开始说起 上一篇从零开始学 Java - Spring 集成 ActiveMQ 配置(一)文章中讲了我关于消息队列的思考过程,现在这一篇会讲到 ActivMQ 与 Spring 框架的整合配置 ...

  6. 【ActiveMQ入门-8】ActiveMQ学习-与Spring集成

    概述: 下面将介绍如何在Spring下集成ActiveMQ. 消费者:同步接收: 目的地:topic 环境: 主要包括4个文件: HelloSender.java: JMSTest.java: Pro ...

  7. Spring集成ActiveMQ配置 --转

    转自:http://suhuanzheng7784877.iteye.com/blog/969865 集成环境 Spring采用2.5.6版本,ActiveMQ使用的是5.4.2,从apache站点可 ...

  8. [置顶] spring集成mina 实现消息推送以及转发

    spring集成mina: 在学习mina这块时,在网上找了很多资料,只有一些demo,只能实现客户端向服务端发送消息.建立长连接之类.但是实际上在项目中,并不简单实现这些,还有业务逻辑之类的处理以及 ...

  9. activemq spring 集成与测试

    1.下载安装activemq 2.pom依赖配置 3.spring配置 4.生产消息,消费消息(同步消费),监听消息(异步消费) 4.测试 5.参考博客 http://www.cnblogs.com/ ...

随机推荐

  1. python-----双色球实现(实例1)

    #输出用户指定组数的双色球信息,其中一组信息 6个红色号码获取范围(1-33),1个蓝色号码获取范围(1-16),要求一组信息中红球内号码不可重复 import randomdef get_ball( ...

  2. PCI 设备调试手段

    Author: Younix Platform: RK3399 OS: Android 6.0 Kernel: 4.4 Version: v2017.04 一PCI 设备调试手段 busybox ls ...

  3. JAVA中的array是通过线性表还是链表实现的呢?

    由于高级程序设计语言中的数组类型也有随机存取的特性,因此,通常都用数组来描述数据结构中的顺序存储结构.

  4. [network] netfilter

    netfilter 是什么? netfilter.org is home to the software of the packet filtering framework inside the Li ...

  5. LeetCode 762 Prime Number of Set Bits in Binary Representation 解题报告

    题目要求 Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...

  6. IIS进程回收导致定时器失效的一种解决办法

    公司开发的网站使用的.net,网站中用到了定时器,放在Global.asax.cs文件中,但由于IIS设置了网站进程定期回收,回收后定时器也就没了.如果不让进程回收,又担心程序中有内存泄露.有人说可以 ...

  7. JavaScript, DOM查找元素

    1.document.getElementById("id"); => IE8 及较低版本不区分ID的大小写 => IE7及较低版本中表单元素的name特性和ID都会被 ...

  8. ORACLE安装入门篇OEL5.4安装ORACLE11g

    一.安装ORACLE11g软件(11.2.0.0) (一)安装前的包支持 1.检测yum仓库是否已经配置好 yum list all 2.搭建yum仓库 1).挂载所需要的安装光盘 虚拟机挂载光盘: ...

  9. 001-分布式理论-CAP定理

    一.概述 CAP原则又称CAP定理,指的是在一个分布式系统中,Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性)这三个基本需求 ...

  10. Java-idea-常用插件-lombok

    1.插件安装 打开perferences或者settings,找打plugins,选择Browse repositories...,搜索lombok,下载安装重启即可. 2.支持的注解: 2.1.@G ...