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接收发送对象《二》的更多相关文章

  1. Linux 两台服务器之间传输文件和文件夹

    今天处理一个项目要迁移的问题,突然发现这么多图片怎么移过去,可能第一时间想到的是先从这台服务器下载下来,然后再上传到另外一台服务器上面去,这个方法确实是可行,但是实在是太费时间了,今天我就教大家怎么快 ...

  2. linux服务器之间传输文件的四种方式

    linux文件传输在内网渗透中至关重要,所以我在此总结一下几种Linux服务器之间传输文件的四种方式 1. scp [优点]简单方便,安全可靠:支持限速参数[缺点]不支持排除目录[用法]scp就是se ...

  3. Linux rsync配置用于服务器之间传输大量的数据

    Linux的rsync 配置,用于服务器之间远程传大量的数据   [教程主题]:rsync [课程录制]: 创E [主要内容] [1] rsync介绍 Rsync(Remote Synchronize ...

  4. Linux 两台服务器之间传输文件

    一.scp命令的使用 1.传输文件(不包括目录) 命令格式:scp 源文件路径目录/需要传输的文件 目标主机的用户名@目标主机IP/主机别名:目标主机存储目录 举个例子:scp /root/ceshi ...

  5. Android:客户端和服务器之间传输数据加密

    Android客户端与服务器进行数据传输时,一般会涉及到两类数据的加密情况,一类是只有创建者才能知道的数据,比如密码:另一类是其他比较重要的,但是可以逆向解密的数据. 第一类:密码类的数据,为了让用户 ...

  6. 利用ssh传输文件-服务器之间传输文件

    利用ssh传输文件   在linux下一般用scp这个命令来通过ssh传输文件. 1.从服务器上下载文件scp username@servername:/path/filename /var/www/ ...

  7. linux下用scp命令在两个服务器之间传输文件,利用php_scp函数进行文件传输

    在linux下利用scp进行文件传输, 从服务器下载文件 scp username@servername:/path/filename /path/filename 上传本地文件到服务器 scp /p ...

  8. 【转载】xShell5 利用 sftp 在本地和服务器之间传输文件

    sftp是Secure File TransferProtocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.sftp与 ftp有着几乎一样的语法和功能.SFTP为 SSH的一部分 ...

  9. linux服务器之间传输文件

    转载:https://www.jb51.net/article/82608.htm 1. scp(最近就使用了scp) [优点]简单方便,安全可靠:支持限速参数 [缺点]不支持排除目录[用法]scp就 ...

随机推荐

  1. REST解惑

    本文是「架构风格:你真的懂REST吗?」的补充! REST全称是Representational State Transfer,目前普遍接受的中文翻译为「表述性状态转移」! 即使翻译过来了,你依然有一 ...

  2. 使用jQuery实现向上循环滚动效果(超简单)

    今天突发奇想 想到的一个新思路 通过使用animate改变外边距达到滚动效果 再用复制节点插入到最后一行达到循环目的 HTML代码如下 <body> <ul style=" ...

  3. Angular.js进阶

    1.常用指令 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UT ...

  4. PHP实现openSug.js参数调试

    这是一款利PHP对百度搜索下拉框提示免费代码实现参数配置调试的程序源代码. 由想要对网站进行搜索下拉调试的站长朋友们进行方便.快速的效果演示,具体参考下面的PHP代码. 如何使用? 请新建一份PHP文 ...

  5. python django-admin startproject django-admin命令未找到

    在使用pip install安装django后使用django-admin生成项目失败解决办法 1.配置环境变量-在系统环境变量path添加后运行 D:\Program Files (x86)\pyt ...

  6. python -- 简单配置发送邮件功能

    本文用第三方类库:yagmail 实现:以QQ邮箱作为发送邮箱为例.最终的实现效果:给指定邮箱,发送指定内容的邮件. 准备工作 1.用于发送邮件的账号信息 比如账号用自己的qq邮箱,但'密码'需要在邮 ...

  7. 【Android】添加依赖包

    貌似好像不知一种方法,以后有时间再研究,下面是其中的一种方法

  8. python 基础练习题, 陆续添加中

    判定用户输入数字是否为闰年 闰年的定义:能够被4整除的年份 #input是自定义输入内容的函数 year = input("请输入年份数字:") #xxx.isdigit方法是检测 ...

  9. ACM1002:A + B Problem II

    Problem Description I have a very simple problem for you. Given two integers A and B, your job is to ...

  10. ...续上文(一个小萌新的C语言之旅)

    我们继续上次没介绍完的继续讲: 下面我们说一下二进制,二进制是计算技术中广泛采用的一种 数制. 二进制数据是用0和1两个 数码来表示的数.它的基数为2,进位规则是“逢二进一”.那么二进制怎么转化为十进 ...