ActiveMQ服务器之间传输对象,项目A发送对象到项目B接收发送对象《二》
来源:互联网 发布:win10风扇控制软件 编辑:程序博客网 时间:2024/06/15 17:51
上一篇文章写到对象之间传输使用线程方式 ,无法使用监听方式,最近解决了使用监听方式接收对象,本次使用配置文件方式,贴出代码供大家参考
发送端:
public void send(User user) {// 将user对象进行传递, 0620 yprString path = UserController.class.getClassLoader().getResource("/").getPath();System.out.println(path);ApplicationContext applicationContext = new FileSystemXmlApplicationContext(path + "applicationContext.xml");JmsTemplate template = (JmsTemplate) applicationContext.getBean("jmsTemplate");Destination destination = (Destination) applicationContext.getBean("destination");// 将user中信息 加到 不可变的userNew中,进行传递 0620 yprfinal User userNew = new User();userNew.setLoginName(user.getLoginName());userNew.setSign("1");// 消息发送 将userNew发送 0620 yprtemplate.send(destination, new MessageCreator() {public Message createMessage(javax.jms.Session session)throws JMSException {return session.createObjectMessage(userNew);}});System.out.println("成功发送了一条消息");}
发送端配置文件:<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-autowire="byName"> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616" /> <property name="userName" value="admin" /> <property name="password" value="admin" /> </bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean> <bean id="destination" class="org.apache.activemq.command.ActiveMQTopic"> <!-- 设置消息队列的名字 --> <constructor-arg index="0" value="STATUS" /> </bean> </beans>
接收端:
/** * 监听接收消息 0620 ypr * @author Administrator * */public class ConsumerMessageListener implements MessageListener {@Overridepublic void onMessage(Message message) {// TODO Auto-generated method stubSystem.out.println("ddddddd");System.out.println("topic收到的消息:" + message);MessageService messageService = (MessageService)ApplicationContextHandle.getBean("messageService");try {//将接收到的对象强制转换为user 对象 0620 ypr User user = (User) ((ObjectMessage) message).getObject();String id=user.getId();List<User> list=messageService.getId(id);if (message != null){//for(int i=0;i<list.size();i++){if(list.size() ==0 && user.getSign().equals("1")){// User s = (User) message.getObject();System.out.println("收到的消息对象:" + user.getLoginName());user.setCreateBy(new User("1"));user.setUpdateBy(new User("1"));//使用getbean 方式获取 systemService 0620 ypr //新增 user对象messageService.xinZeng(user);}else if(list.size()!=0 && user.getSign().equals("1")){System.out.println("收到的消息对象:" + user.getLoginName());messageService.xiuGai(user);}else if(user.getSign().equals("2")){System.out.println("收到的消息对象:" + user.getLoginName());messageService.shanChu(user);}}//}} catch (JMSException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}}
接收端配置:<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd"> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616" /> <property name="userName" value="admin" /> <property name="password" value="admin" /> </bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean> <bean id="mqDestination" class="org.apache.activemq.command.ActiveMQTopic"> <!-- 设置消息队列的名字 --> <constructor-arg index="0" value="STATUS" /> </bean><!-- 设置监听路径 0620 ypr --> <bean id="messageListener" class="com.thinkgem.jeesite.modules.sys.listener.ConsumerMessageListener"></bean> <bean id="listenerContainer" class="org.springframework.jms.listener.SimpleMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="mqDestination" /> <property name="messageListener" ref="messageListener" /> </bean> </beans>
web.xml配置
<!-- Context ConfigLocation --><!-- 扫描applicationContext.xml 0620 ypr --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:/spring-context*.xml,classpath*:/applicationContext.xml</param-value></context-param>
所参考文章
阅读全文
2 0
- ActiveMQ服务器之间传输对象,项目A发送对象到项目B接收发送对象《二》
- ActiveMQ服务器之间传输对象,项目A发送对象到项目B接收发送对象《一》
- ActiveMq发送接收对象
- activemq发送和接收对象
- activemq 对象的传输
- C# 发送接收序列化对象
- SpringMVC 接收ajax发送的数组对象
- BW项目的对象传输
- jmeter(postman)发送请求对象,springmvc接收对象
- asp.net 中Ajax发送XML对象到服务器和服务器获取XML对象
- 往服务器发送自定义的对象
- flex socket.writeObject()发送对象,java接收flex socket发送过来的对象
- spring整合ActiveMQ订阅模式(对象消息发送)
- jersey接收对象参数到服务器
- 发送接收的项目总结
- DWR传输和接收对象
- 10XMLHttpRequest对象的创建,及发送和接收数据
- springMVC如何接收和发送json数据对象
- PHP面向对象——继承(个人笔记)
- 微服务架构实践之邮件通知系统改造
- Dumpzilla工具第615行bug的解决办法
- C++ 线程
- 美团A轮 合并回文子串 【dp】
- ActiveMQ服务器之间传输对象,项目A发送对象到项目B接收发送对象《二》
- spring mvc 任务定时器 @Scheduled
- CentOS7.0安装配置Storm集群
- 透明度
- 设置UserAgent让电脑浏览器访问微信手机网站
- 程序猿们_一二三四线城市你更愿意选择去哪里工作?
- LeetCode 513. Find Bottom Left Tree Value (C++)
- PAT练习-部分A+B
- struts2拦截器使用注解