ActiveMQ服务器之间传输对象,项目A发送对象到项目B接收发送对象《二》
2017-06-21 13:55
337 查看
上一篇文章写到对象之间传输使用线程方式 ,无法使用监听方式,最近解决了使用监听方式接收对象,本次使用配置文件方式,贴出代码供大家参考
发送端: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>
所参考文章
发送端: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接收发送对象《一》
- activemq发送和接收对象
- ActiveMq发送接收对象
- EventSource 对象用于接收服务器发送事件通知,是网页自动获取来自服务器的更新
- [置顶] 直接在服务器与客户端之间传输对象
- Qt5 基于TCP传输的发送/接收文件服务器(支持多客户端)
- 基于两个项目 之间的RabbitMQ 发送和接收,并调用邮件接口,发送邮件
- 游戏对象之间发送和接收消息
- 客户端C和服务器S之间建立一个TCP连接,该连接总是以1KB的最大段长发送TCP段,客户端C有足够的数据要发送。当拥塞窗口为16KB的时候发生超时,如果接下来的4个RTT往返时间内的TCP段的传输是成
- Qt5 基于TCP传输的发送/接收文件服务器(支持多客户端)
- ActiveMQ学习笔记01 - 客户端与服务器之间的传输连接
- Qt5 基于TCP传输的发送/接收文件服务器(支持多客户端)
- Qt5 基于TCP传输的发送/接收文件服务器(支持多客户端)
- 用httpwebrequest/httpwebresponse向服务器POST发送及接收数据
- BW-- BW项目的对象传输
- 不同Web主机上的Servlet之间数据对象的相互传输
- 服务器与客户端之间的远程图片传输
- BW-- BW项目的对象传输
- 使用XMLHttpRequest对象向服务器发送简单请求
- 不同Web主机上的Servlet之间数据对象的相互传输