ActiveMQ服务器之间传输对象,项目A发送对象到项目B接收发送对象《二》
ActiveMQ服务器之间传输对象,项目A发送对象到项目B接收发送对象《一》
上一篇文章写到对象之间传输使用线程方式 ,无法使用监听方式,最近解决了使用监听方式接收对象,本次使用配置文件方式,贴出代码供大家参考
发送端:
public void send(User user) {
// 将user对象进行传递, 0620 ypr
String 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 ypr
final User userNew = new User();
userNew.setLoginName(user.getLoginName());
userNew.setSign("1");
// 消息发送 将userNew发送 0620 ypr
template.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 { @Override
public void onMessage(Message message) {
// TODO Auto-generated method stub
System.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 block
e1.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>
ActiveMQ服务器之间传输对象,项目A发送对象到项目B接收发送对象《二》的更多相关文章
- Linux 两台服务器之间传输文件和文件夹
今天处理一个项目要迁移的问题,突然发现这么多图片怎么移过去,可能第一时间想到的是先从这台服务器下载下来,然后再上传到另外一台服务器上面去,这个方法确实是可行,但是实在是太费时间了,今天我就教大家怎么快 ...
- linux服务器之间传输文件的四种方式
linux文件传输在内网渗透中至关重要,所以我在此总结一下几种Linux服务器之间传输文件的四种方式 1. scp [优点]简单方便,安全可靠:支持限速参数[缺点]不支持排除目录[用法]scp就是se ...
- Linux rsync配置用于服务器之间传输大量的数据
Linux的rsync 配置,用于服务器之间远程传大量的数据 [教程主题]:rsync [课程录制]: 创E [主要内容] [1] rsync介绍 Rsync(Remote Synchronize ...
- Linux 两台服务器之间传输文件
一.scp命令的使用 1.传输文件(不包括目录) 命令格式:scp 源文件路径目录/需要传输的文件 目标主机的用户名@目标主机IP/主机别名:目标主机存储目录 举个例子:scp /root/ceshi ...
- Android:客户端和服务器之间传输数据加密
Android客户端与服务器进行数据传输时,一般会涉及到两类数据的加密情况,一类是只有创建者才能知道的数据,比如密码:另一类是其他比较重要的,但是可以逆向解密的数据. 第一类:密码类的数据,为了让用户 ...
- 利用ssh传输文件-服务器之间传输文件
利用ssh传输文件 在linux下一般用scp这个命令来通过ssh传输文件. 1.从服务器上下载文件scp username@servername:/path/filename /var/www/ ...
- linux下用scp命令在两个服务器之间传输文件,利用php_scp函数进行文件传输
在linux下利用scp进行文件传输, 从服务器下载文件 scp username@servername:/path/filename /path/filename 上传本地文件到服务器 scp /p ...
- 【转载】xShell5 利用 sftp 在本地和服务器之间传输文件
sftp是Secure File TransferProtocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.sftp与 ftp有着几乎一样的语法和功能.SFTP为 SSH的一部分 ...
- linux服务器之间传输文件
转载:https://www.jb51.net/article/82608.htm 1. scp(最近就使用了scp) [优点]简单方便,安全可靠:支持限速参数 [缺点]不支持排除目录[用法]scp就 ...
随机推荐
- Unity经验之谈-DoTween动画结束匿名委托之巨坑
产生问题: 成百上千个物体放在List列表里面循环,每个物体都要使用移动和移动结束事件. BUG: 动画结束之后我想隐藏该物体,结果却没有正常的隐藏,代码如下 foreach (var item in ...
- 关于content-type请求头的说明
Content-Type请求头的作用,用于标记请求体数据的格式,如: 1. Content-Type:application/x-www-form-urlencoded 请求体:b'pwd=123&a ...
- 一点一点看JDK源码(五)java.util.ArrayList 后篇之removeIf与Predicate
一点一点看JDK源码(五)java.util.ArrayList 后篇之removeIf与Predicate liuyuhang原创,未经允许禁止转载 本文举例使用的是JDK8的API 目录:一点一点 ...
- 霍金:AI或许能根除疾病和贫穷,但也可能摧毁人类 | GMIC 2017
在我的一生中,我见证了社会深刻的变化.其中最深刻的,同时也是对人类影响与日俱增的变化,是人工智能的崛起.简单来说,我认为强大的人工智能的崛起,要么是人类历史上最好的事,要么是最糟的.我不得不说,是好是 ...
- js 变速动画函数
//获取任意一个元素的任意一个属性的当前的值---当前属性的位置值 function getStyle(element, attr) { return window.getComputedStyle ...
- Linux下安装 nginx
安装依赖 yum install gcc yum install pcre-devel yum install zlib zlib-devel yum install openssl openssl- ...
- Hadoop-Hive学习笔记(2)
1.Hive基本操作 #创建数据库hive>create database name;#创建新表hive> create table students(id int,name string ...
- log4j配置输出日志文件
在测试程序时,有时候运行一次可能需要很久,把日志文件保存下来是很有必要的,本文给出了scala程序输出日志文件的方式,同时使用本人的另一篇博客中介绍的将log4j.properties放到程序jar包 ...
- 计算机基础和Linux基础
计算机原理 计算机发展史 机器语言—让机器干活 差分机—让机器的数学运算和逻辑运算只简化成“加法”,计算机只处理“加法” 计算机硬件CPU=运算器+控制器+寄存器(缓存)硬盘=存储器+寄存器寄存器是为 ...
- UART学习之路(二)基本时序介绍
这次我们来介绍一下UART的基本时序,了解一下底层信号怎么传送的.方便以后使用Verilog HDL实现收发逻辑. 9600bit/s 的意思是每秒发送9600bit,因此可以理解为将1s分解为960 ...