ActiveMQ

2017-09-30  本文已影响0人  花丶小伟

ActiveMQ简单的示例

下载ActiveMQ

运行ActiveMQ

  1. 解压缩apache-activemq-5.9.0-bin.zip,
  2. 修改配置文件activeMQ.xml,将0.0.0.0修改为localhost

<transportConnector name="openwire" uri="tcp://localhost:61616"/>

​ <transportConnector name="ssl" uri="ssl://localhost:61617"/>

​ <transportConnector name="stomp" uri="stomp://localhost:61613"/>

​ <transportConnector uri="http://localhost:8081"/>

​ <transportConnector uri="udp://localhost:61618"/>

  1. 然后双击apache-activemq-5.9.0\bin\activemq.bat运行ActiveMQ程序。
  2. 启动ActiveMQ以后,登陆:http://localhost:8161/admin/ 账号密码:admin
  3. 创建一个Queue,命名为FirstQueue。
mark

点对点

producer生产者

public static void main(String[] args) throws Exception {
        // 1. 创建连接工厂ActiveMQConnectionFactory,需要ip和端口61616
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.37.161:61616");

        // 2. 从连接工厂中创建连接对象
        Connection connection = factory.createConnection();

        // 3. 执行start方法开启连接
        connection.start();

        // 4. 从连接中创建session对象
        // 第一个参数,是否开启事务,JTA分布式事务
        // 第二个参数,是否自动应答,如果第一个参数为true,第二个参数失效
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // 5. 从session中创建Destination对象,设置queue名称(有两种类型queue和topic)
        Queue queue = session.createQueue("test-queue");

        // 6. 从session中创建Product对象
        MessageProducer producer = session.createProducer(queue);

        // 7. 创建消息对象
        TextMessage textMessage = new ActiveMQTextMessage();
        // 设置消息内容
        textMessage.setText("开始发消息!");

        // 8. 发送消息
        producer.send(textMessage);

        // 9. 关闭session、连接
        producer.close();
        session.close();
        connection.close();
    }

consumer消费者

发布/订阅模式

producer生产者

public static void main(String[] args) throws Exception {
        // 1. 创建连接工厂ActiveMQConnectionFactory
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(
                "tcp://192.168.37.161:61616");

        // 2. 使用工厂创建连接
        Connection connection = activeMQConnectionFactory.createConnection();

        // 3. 使用start方法开启连接
        connection.start();

        // 4. 从连接创建session
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // 5. 从session创建Destination对象,设置topic名称
        Topic topic = session.createTopic("test-topic");

        // 6. 从session创建Product
        MessageProducer producer = session.createProducer(topic);

        // 7. 创建消息对象
        TextMessage textMessage = new ActiveMQTextMessage();
        textMessage.setText("topic消息");

        // 8. 发送消息
        producer.send(textMessage);

        // 9. 关闭session、连接等
        producer.close();
        session.close();
        connection.close();
    }

}

consumer消费者

整合spring

加入依赖

​ <dependency>

​ <groupId>org.apache.activemq</groupId>

​ <artifactId>activemq-all</artifactId>

​ </dependency>

​ <dependency>

​ <groupId>org.springframework</groupId>

​ <artifactId>spring-jms</artifactId>

​ </dependency>

​ <dependency>

​ <groupId>org.springframework</groupId>

​ <artifactId>spring-webmvc</artifactId>

​ </dependency>

消息发送

public static void main(String[] args) {
// 1. 创建spring容器
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext-activemq.xml");

// 2. 从容器中获取JMSTemplate对象
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);

// 3. 从容器中获取Destination对象
Destination destination = context.getBean(Destination.class);

// 4. 使用JMSTemplate发送消息
jmsTemplate.send(destination, new MessageCreator() {

@Override
public Message createMessage(Session session) throws JMSException {
// 创建消息对象
TextMessage textMessage = new ActiveMQTextMessage();

// 设置消息内容
textMessage.setText("spring整合ActiveMQ");

// 打印消息
System.out.println(textMessage.getText());

return textMessage;
}
});
}

消息接收

public class MyMessageListener implements MessageListener {

@Override
public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;

try {
// 获取消息内容
String msg = textMessage.getText();

// 打印消息
System.out.println("接受消息:" + msg);

} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

}

queue方式配置spring

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jms="http://www.springframework.org/schema/jms"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


    <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://192.168.37.161: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="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg>
            <value>queue</value>
        </constructor-arg>
    </bean>

    <!--这个是主题目的地,一对多的 -->
    <!-- <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> -->
    <!-- <constructor-arg value="topic" /> -->
    <!-- </bean> -->

    <!-- messageListener实现类 -->
    <bean id="myMessageListener" class="cn.itcast.activemq.spring.MyMessageListener"></bean>

    <!-- 配置一个jsm监听容器 -->
    <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="queueDestination" />
        <property name="messageListener" ref="myMessageListener" />
    </bean>

</beans>

topic方式配置spring

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jms="http://www.springframework.org/schema/jms" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://192.168.37.161: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="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> -->
    <!-- <constructor-arg> -->
    <!-- <value>queue</value> -->
    <!-- </constructor-arg> -->
    <!-- </bean> -->

    <!--这个是主题目的地,一对多的 -->
    <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg value="topic" />
    </bean>

    <!-- messageListener实现类 -->
    <bean id="myMessageListener" class="cn.itcast.activemq.spring.MyMessageListener"></bean>
    
    <!-- messageListener实现类 -->
    <bean id="myMessageListener2" class="cn.itcast.activemq.spring.MyMessageListener2"></bean>

    <!-- 配置一个jsm监听容器 -->
    <bean id="jmsContainer"
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="topicDestination" />
        <property name="messageListener" ref="myMessageListener" />
    </bean>
    
    <!-- 配置一个jsm监听容器 -->
    <bean id="jmsContainer2"
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="topicDestination" />
        <property name="messageListener" ref="myMessageListener2" />
    </bean>

</beans>
上一篇 下一篇

猜你喜欢

热点阅读