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>

 

posted @ 2018-10-12 14:03  ypr-  阅读(797)  评论(0编辑  收藏  举报