ActiveMQ(四):Topic方式使用MessageListener监听的方式接收消息

发布/订阅模式

消息发布者:

ActiveMQ(四):Topic方式使用MessageListener监听的方式接收消息_第1张图片

xml配置:


<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>

发送消息:

package ActiveMQ;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class HelloSender {

    /**
     * @param args
     * jmsTemplate和destination都是在spring配置文件中进行配制的
     * Sender只使用了配置文件中的jmsFactory,jmsTemplate,还有destination这三个属性
     */
    public static void main(String[] args) {
        ApplicationContext applicationContext = new FileSystemXmlApplicationContext("conf/applicationContext-jms.xml");
        JmsTemplate template = (JmsTemplate) applicationContext.getBean("jmsTemplate");
        Destination destination = (Destination) applicationContext.getBean("destination");
        template.send(destination, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage("发送消息:Hello ActiveMQ Text Message2!");
            }
        });
        System.out.println("成功发送了一条JMS消息");
    }
}

消息接收者:
ActiveMQ(四):Topic方式使用MessageListener监听的方式接收消息_第2张图片

xml配置:


<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>

    <bean id="messageListener" class="activeMQ.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>

消息监听者:

package activeMQ;

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

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;

//监听代码 
public class ConsumerMessageListener implements MessageListener{

    @Override
    public void onMessage(Message message) {
        // TODO Auto-generated method stub

        System.out.println("topic收到的消息:"+message);
        TextMessage textmessage = (TextMessage)message;
        try {
            System.out.println("message:"+textmessage.getText());
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

消费者代码:

package activeMQ;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

//这是消费者代码,这里你可以创建多个XML文件,模拟多个消费者
public class JmsTopicReceiver {
    public static void main(String[] args){
        //加载消费者监听
        ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/applicationContext.xml");
        //写个死循环
        while(true){}
    }
}

测试:
先启动消息监听者JmsTopicReceiver:
ActiveMQ(四):Topic方式使用MessageListener监听的方式接收消息_第3张图片

再启动消息发送端:
ActiveMQ(四):Topic方式使用MessageListener监听的方式接收消息_第4张图片

在监控端即可监控到发送的消息:
ActiveMQ(四):Topic方式使用MessageListener监听的方式接收消息_第5张图片

你可能感兴趣的:(ActiveMQ)