订阅消息
应用之间的解耦,一方发布,多方订阅;
1. maven依赖
<!-- mpush --> <dependency> <groupId>com.mengka.mpush</groupId> <artifactId>mpush-client</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <!-- ****** -->
2. 设置notifyManager的配置:
mpush-message.xml
(1)消息的主题topic;
(2)消息的分组groupId;
(3)接收到消息之后的处理操作messageListener;
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="notifyManager" class="com.mengka.mpush.client.NotifyManagerBean" init-method="initSubscribe"> <property name="groupId" value="1001" /> <property name="name" value="apkNotifyManager" /> <property name="description" value="apkNotifyManager" /> <property name="topic" value="news_key_mengka"/> <property name="messageListener" ref="messageListener" /> </bean> <bean id="messageListener" class="com.mengka.mpush.TaaMessageListener"> </bean> </beans>
3. 设置消息监听器
package com.mengka.mpush; import com.mengka.mpush.client.MessageListener; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Created by mengka */ public class TaaMessageListener implements MessageListener { private static final Log log = LogFactory.getLog(TaaMessageListener.class); @Override public void receiveMessage(String message) { log.error("--------------- , receiveMessage success!"); log.info("--------------- , receiveMessage message = "+message); } }
4. 启动web应用,接收对应topic的消息
<context:annotation-config/> <context:component-scan base-package="com.mengka.*"/> <!--<import resource="dubbo-product.xml" />--> <import resource="dubbo-consumer.xml"/> <import resource="mpush-message.xml"/>