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. AFNetworking 打印错误信息(二进制信息)

    AFNetworking 打印错误信息(二进制信息) NSError *underError = error.userInfo[@"NSUnderlyingError"]; NSD ...

  2. 19-3-4 Python进制转换;bool str int三者之间的转换;字符串的索引,切片;for循环的使用

    进制转换: 二进制转十进制:  0010 1111 = 1*2**0+1*2**1+1*2**2+1*2**3+1*2**5 十进制转换二进制: 用十进制数除2逆序取余 --->101010 布 ...

  3. 简单json---转树形json

    var data = [ {"fileName":"navone","layFilterId":"layadmin-system- ...

  4. DataSet和泛型之间相互转换

    取数据的时候,存储过程返回了多个结果集,后台用DataSet去接收这几个结果集,然后接收之后,需要将结果集转换为不同的实体,于是下面的代码便出现了. /// <summary> /// 将 ...

  5. 「PHP」简单工厂模式

    引言   所属:创建型模式,常用设计模式之一 工厂模式分为:简单工厂模式.工厂方法模式.静态工厂模式.抽象工厂模式. 下面为简单工厂模式.   参考资料: <大话设计模式>程杰   模式概 ...

  6. curl下载文件

    * curl下载文件* 根据业务需求* 通过不同站点去访问路径* 下载文件* 但是不同站点需要设置header头* 这里使用curl方式下载* 具体看代码: //下载地址 $url = 'https: ...

  7. Dubbo client 启动报错:No provider available for the service use dubbo version 2.5.3

    1.异常 java.lang.IllegalStateException: Failed to check the status of the service org.ko.server.servic ...

  8. 树莓派驱动DHT22

    树莓派-DHT22测量湿度 一般的温湿度传感器有dht11和dht22,dht11比较便宜,dht22比dht11贵好几倍,自然测量的准确度肯定是dht22高一些.追求更高精准度的可以使用SHT1x. ...

  9. tcp/ip五层协议

    TCP/IP协议不是TCP和IP这两个协议的合称,而是指因特网整个TCP/IP协议族.互联网协议(Internet Protocol Suite)是一个网络通信模型,以及一整个网络传输协议家族,为互联 ...

  10. docker-compose入门示例:一键部署 Nginx+Tomcat+Mysql

    整体环境配置 整体环境的配置,如果一个一个 Dockerfile 去写,那么是相当麻烦的,好在 Docker 有一个名为 Docker-Compose 的工具提供,我们可以使用它一次性完成整体环境的配 ...