其他分享
首页 > 其他分享> > activemq消息中间件的发送接收配置

activemq消息中间件的发送接收配置

作者:互联网

监听器监听消息:

	<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
	    <property name="brokerURL" value="tcp://192.168.200.148:61616"/>
	</bean>
	   
	<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
	<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">  
	<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->  
	    <property name="targetConnectionFactory" ref="targetConnectionFactory"/>  
	</bean>  
	
	<!--发布订阅模式, 生成页面-->
	<bean id="topicPageAndSolrDestination" class="org.apache.activemq.command.ActiveMQTopic">  
	    <constructor-arg value="pinyougou_topic_page_solr"/>  
	</bean>    
	
	<!-- 发布订阅模式, 消息监听容器   生成页面 -->
	<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<!--指定消息服务器-->
		<property name="connectionFactory" ref="connectionFactory" />
		<!--指定获取消息的队列-->
		<property name="destination" ref="topicPageAndSolrDestination" />
		<!--指定监听器-->
		<property name="messageListener" ref="pageListener" />
	</bean>
	<bean id="pageListener" class="cn.tsb.core.listener.PageListener"></bean>
public class PageListener implements MessageListener {
    @Autowired
    private CmsService cmsService;

    @Override
    public void onMessage(Message message) {
        ActiveMQTextMessage atm = (ActiveMQTextMessage) message;
        try {
            String goodsId = atm.getText();
            Map<String, Object> goodsData = cmsService.findGoodsData(Long.valueOf(goodsId));
            cmsService.createStaticPage(Long.valueOf(goodsId), goodsData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

发送消息:

  <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供-->
  <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="tcp://192.168.200.148:61616"/>
  </bean>

  <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
  <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
    <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
    <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
  </bean>

  <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
  <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
    <property name="connectionFactory" ref="connectionFactory"/>
  </bean>

  <!-- 发布订阅模式, 商品导入索引库和生成静态页面 -->
  <bean id="topicPageAndSolrDestination" class="org.apache.activemq.command.ActiveMQTopic">
    <!--将被上架的商品id发送到这个队列-->
    <constructor-arg value="pinyougou_topic_page_solr"/>
  </bean>


  <!-- 点对点模式,删除索引库-->
  <bean id="queueSolrDeleteDestination" class="org.apache.activemq.command.ActiveMQQueue">
    <!--将被下架的商品id发送到这个队列-->
    <constructor-arg value="pinyougou_queue_solr_delete"/>
  </bean>
	@Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private ActiveMQTopic topicPageAndSolrDestination;

	@Override
    public void updateStatus(final Long id, String status) {
		jmsTemplate.send(topicPageAndSolrDestination, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    TextMessage textMessage = session.createTextMessage(String.valueOf(id));
                    return textMessage;
         	}
    	});
	}

标签:valueOf,private,发送,goodsId,消息中间件,Override,activemq,public,cmsService
来源: https://blog.csdn.net/qq_41490274/article/details/102721130