activeMQ实例在项目中的运用二【项目实战系列】

        上一篇我们已经介绍了activeMQ的服务器端的开发和代码,那么这一篇就介绍一下怎么获取对应的activeMQ里面的信息,并


通过websocket推送到前端。


        首先来看一下接收端项目结构:



项目结构中主要的就是listener包里面的内容,用于监听MQ中的消息,我们来看其中一个UserPushListener.java类:

package com.lwl.activemq.listener;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.lwl.activemq.domain.User;
import com.lwl.activemq.controller.websocket.WebsocketController;

@Component("userPushListener")
public class UserPushListener implements MessageListener {
	 protected static final Logger logger = Logger.getLogger(UserPushListener.class);
	@Override
	public void onMessage(Message message) {
		 logger.info("[UserPushListener.onMessage]:begin onMessage.");
	        TextMessage textMessage = (TextMessage) message;
	        try {
	        	//获取数据
	            String jsonStr = textMessage.getText();
	            logger.info("[UserPushListener.onMessage]:receive message is,"+ jsonStr);
	            if (jsonStr != null) {
	                User info = JSON.parseObject(jsonStr, User.class);
	                System.out.println("==============================接受到的用户信息 开始====================================");
	                System.out.println(info.toString());
	                System.out.println("==============================接受到的用户信息 结束====================================");
	                WebsocketController.broadcast("user", jsonStr);
	            }
	        } catch (JMSException e) {
	            logger.error("[UserPushListener.onMessage]:receive message occured an exception",e);
	        }
	        logger.info("[UserPushListener.onMessage]:end onMessage.");
	    }
}
其余的2个和这个很相似,只是换了实体类的接收而已。


还记得我们在配置服务器端使用的MQ的配置文件嘛,当然在接收端也会使用相应的配置文件:activemq_config.xml

<?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"  
    xmlns:jms="http://www.springframework.org/schema/jms"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
     http://www.springframework.org/schema/context  
     http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd">  
     
    <!-- 内部统一使用的MQ地址 -->
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616"/>
    </bean>
    <bean id="connectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">  
        <property name="connectionFactory" ref="targetConnectionFactory"/>  
        <property name="maxConnections" value="50"/>
    </bean>
    <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
        <property name="connectionFactory" ref="connectionFactory"/>
    </bean>

   <!-- 推送给用户信息 -->
	<bean id="userPushListenerMQ" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg>
			<value>user.service.queue</value>
		</constructor-arg>
	</bean>
	<!-- 推送给新闻信息 -->
	<bean id="newsPushListenerMQ" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg>
			<value>news.service.queue</value>
		</constructor-arg>
	</bean>
	<!-- 推送给客户信息 -->
	<bean id="clientPushListenerMQ" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg>
			<value>client.service.queue</value>
		</constructor-arg>
	</bean>
	
	<!-- 用户接受推送 -->
    <bean id="userPushListenerConsumer"
          class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="userPushListenerMQ" />
        <property name="messageListener" ref="userPushListener" />
    </bean>
    
	<!-- 新闻接受推送 -->
    <bean id="newsPushListenerConsumer"
          class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="newsPushListenerMQ" />
        <property name="messageListener" ref="newsPushListener" />
    </bean>
    
	<!-- 客户接受推送 -->
    <bean id="clientPushListenerConsumer"
          class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="clientPushListenerMQ" />
        <property name="messageListener" ref="clientPushListener" />
    </bean>
</beans>

好了,数据接受端已经全部搞定了,就这么简单,没什么难度的。至于通过websocket推送到前端的功能,已经在我另一篇的


文章里了:http://blog.csdn.net/lovelong8808/article/details/52212566 有兴趣的可以看看,这2个是同一个项目使用,重在结合


知识和运用。


ActiveMQ的相关代码也已经提交到github上,有兴趣的同学可以去看看 https://github.com/1181888200/activeMQ.git

  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值