ActiveMQ之spring集成消息转换器MessageConverter
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的更多相关文章
- ActiveMQ与spring集成实现Queue模式
ActiveMQ可以和spring很好的集成,下面我们来看看,如何做个集成的demo. (1)pom.xml引入相关jar <!-- spring相关 begin --> <depe ...
- SpringMVC自定义配置消息转换器踩坑总结
问题描述 最近在开发时候碰到一个问题,springmvc页面向后台传数据的时候,通常我是这样处理的,在前台把数据打成一个json,在后台接口中使用@requestbody定义一个对象来接收,但是这次数 ...
- Spring MVC自定义消息转换器(可解决Long类型数据传入前端精度丢失的问题)
1.前言 对于Long 类型的数据,如果我们在Controller层通过@ResponseBody将返回数据自动转换成json时,不做任何处理,而直接传给前端的话,在Long长度大于17位时会出现精度 ...
- 从零开始学 Java - Spring 集成 ActiveMQ 配置(一)
你家小区下面有没有快递柜 近两年来,我们收取快递的方式好像变了,变得我们其实并不需要见到快递小哥也能拿到自己的快递了.对,我说的就是类似快递柜.菜鸟驿站这类的代收点的出现,把我们原来快递小哥必须拿着快 ...
- 从零开始学 Java - Spring 集成 ActiveMQ 配置(二)
从上一篇开始说起 上一篇从零开始学 Java - Spring 集成 ActiveMQ 配置(一)文章中讲了我关于消息队列的思考过程,现在这一篇会讲到 ActivMQ 与 Spring 框架的整合配置 ...
- 【ActiveMQ入门-8】ActiveMQ学习-与Spring集成
概述: 下面将介绍如何在Spring下集成ActiveMQ. 消费者:同步接收: 目的地:topic 环境: 主要包括4个文件: HelloSender.java: JMSTest.java: Pro ...
- Spring集成ActiveMQ配置 --转
转自:http://suhuanzheng7784877.iteye.com/blog/969865 集成环境 Spring采用2.5.6版本,ActiveMQ使用的是5.4.2,从apache站点可 ...
- [置顶]
spring集成mina 实现消息推送以及转发
spring集成mina: 在学习mina这块时,在网上找了很多资料,只有一些demo,只能实现客户端向服务端发送消息.建立长连接之类.但是实际上在项目中,并不简单实现这些,还有业务逻辑之类的处理以及 ...
- activemq spring 集成与测试
1.下载安装activemq 2.pom依赖配置 3.spring配置 4.生产消息,消费消息(同步消费),监听消息(异步消费) 4.测试 5.参考博客 http://www.cnblogs.com/ ...
随机推荐
- zabbix客户端自动注册
1. 概述 上一篇内容<zabbix自动发现配置>,大概内容是zabbix server去扫描一个网段,把在线的主机添加到Host列表中.我们本篇内容与上篇相反,这次是Active age ...
- winform 科学计数法转为小数
先强制转换为decimal. 例如: double xyTolerance = 0.000000008983001; txtXYTolerance.Text = ((decimal)xyToleran ...
- block diagonal matrix 直和 块对角矩阵 不完美 有缺陷 缩放 射影几何
小结: 1.block diagonal matrix 直和 块对角矩阵 A block diagonal matrix is a block matrix that is a square mat ...
- int 存储大小 数组元素个数
为了得到某个类型或某个变量在特定平台上的准确大小,您可以使用 sizeof 运算符.表达式 sizeof(type) 得到对象或类型的存储字节大小.下面的实例演示了获取 int 类型的大小: 实例 # ...
- prometheus: celery, redis-export
https://github.com/nlighten/tomcat_exporter https://github.com/prometheus/jmx_exporter https://vexxh ...
- Copycat - 状态
Member.Status status的变迁是源于heartbeat heartbeat,append空的entries /** * Triggers a heartbeat to a majori ...
- python多线程相关知识点
1. 信号量 信号机维护着一个计数器,指定可同时访问资源或者进入临界区的线程数.每次有一个线程获得信号机时,计数器-1.若计数器为0,其他线程就停止访问信号机 Semphore,是一种带计数的线程同步 ...
- 转:string.Format格式化用法
String.format()方法使用说明(比较详细) 地址:https://blog.csdn.net/thc1987/article/details/17528093 典型案例 原文地址:http ...
- spring+shiro+springmvc+maven权限卡控示例
项目结构 UserController , 主要负责用户登入和注销. LinewellController, 主要负责请求受权限卡控的数据. MyRealm,自定义realm. Authorizati ...
- Servlet (二)ServletContext
package cn.sasa.serv; import java.io.IOException; import javax.servlet.ServletContext; import javax. ...