JMS实战

1、生产者
UserLoginIPServiceImpl
    @Autowired
    private JmsTemplate jmsTemplate;
    
    @Autowired
    @Qualifier("localCacheRefreshTopic")
    private Topic localCacheRefreshTopic;

    jmsTemplate.send(localCacheRefreshTopic, new LocalCacheRefreshMessage(LocalCacheRefreshMessage.SelectKey.DISABLED_IP.getKey()));


2、消息体定义:
LocalCacheRefreshMessage

LocalCacheRefreshMessage implements MessageCreator{

   @Override
    public String toString() {
        return "LocalCacheRefreshMessage [selectKey=" + selectKey + "]";
    }

    @Override
    public Message createMessage(Session session) throws JMSException {
        MapMessage message = session.createMapMessage();
        message.setInt("selectKey", selectKey);
        if(data != null) {
            for(Entry<String, Object> entry : data.entrySet()) {
                message.setObject(entry.getKey(), entry.getValue());
            }
        }
        return message;
    }

public static enum SelectKey {
        
        /** 禁封IP列表 **/
        DISABLED_IP(16);
        
        private int key;

        private SelectKey(int key) {
            this.key = key;
        }

        public int getKey() {
            return key;
        }
    }
}


3、配置topic,applicationContext-mq.xml配置:
<amq:topic id="localCacheRefreshTopic" physicalName="${jms.destination.topic.localcacherefresh}" />


4、实际名称system-online.properties
jms.destination.topic.localcacherefresh=T.DDSHOW.LOCALCACHEREFRESH


5、applicationContext-web.xml 配置监听:
<jms:listener-container destination-type="topic">
    	<!-- 礼物本地缓存更新监听者 -->
        <jms:listener destination="T.DDSHOW.ROOMFULLDESTCHANGE" ref="roomFullMessageListener"/>
		<!-- 人气接口频道白名单变化更新监听 -->
		<jms:listener destination="${jms.destination.topic.localcacherefresh}" ref="tudouCMSController"/>
		<!-- 封禁IP列表变化更新监听 -->
        <jms:listener destination="${jms.destination.topic.localcacherefresh}" ref="disableIpHandler"/>
    </jms:listener-container>
<bean id="disableIpHandler" class="com.youku.ddshow.handler.system.DisableIpHandler" />


6、消费消息:

public class DisableIpHandler implements InitializingBean, MessageListener {

 @Override
    public void onMessage(Message message) {
        if (message instanceof MapMessage) {
            MapMessage mm = (MapMessage) message;
            try {
                if (LocalCacheRefreshMessage.SelectKey.DISABLED_IP.getKey() == mm
                        .getInt("selectKey")) {
                    reload();
                }
            } catch (JMSException e) {
                logger.error(
                        "Refresh disabled ip cache failed: {}" + e.getMessage(),
                        e);
            }
        } else {
            logger.warn("Message must be of type MapMessage");
        }
    }
}


7、相关配置
<dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-spring</artifactId>
        <version>5.8.0</version>
      </dependency>
      <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-client</artifactId>
        <version>5.8.0</version>
      </dependency>
      <dependency>
        <groupId>javax.jms</groupId>
        <artifactId>jms-api</artifactId>
        <version>1.1-rev-1</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
        <version>3.2.2.RELEASE</version>
      </dependency>

你可能感兴趣的:(jms)