Spring 集成 JMS 连接 ActiveMQ

Spring 集成 JMS 连接 ActiveMQ

  • ConnectionFactory:用于管理连接的连接工厂
    由于 JmsTemplate 每次发送消息都会重新创建连接、会话和 productor,比较消耗性能,因此,Spring 提供了 SingleConnectionFactoryCachingConnectionFactory 两种连接池。

  • JmsTemplate:用于发送和接收消息的模板类
    JmsTemplate 由 Spring 提供,只需向 Spring 容器注册便可使用,线程安全。

  • MessageListener:消息监听器
    只要实现 onMessage 方法,便可处理消息。

1. 队列模式

1.1 spring 配置文件

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <!-- ActiveMQ 提供的 ConnectionFactory -->
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://127.0.0.1:61616"/>
    </bean>

    <!-- Spring JMS 提供的连接池 -->
    <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
        <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
    </bean>

    <!-- 一个队列目的地,点对点 -->
    <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg value="queue"/>
    </bean>

    <!-- 配置 JmsTemplate,用于发送消息 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
    </bean>

    <bean class="com.chen.jms.spring.queue.producer.QueueProducerServiceImpl"></bean>

    <!-- 配置消息监听器 -->
    <bean id="queueConsumerMessageListener" class="com.chen.jms.spring.queue.consumer.QueueConsumerMessageListener"></bean>

    <!-- 配置消息容器 -->
    <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="destination" ref="queueDestination"/>
        <property name="messageListener" ref="queueConsumerMessageListener"/>
    </bean>
</beans>

1.2 生产者接口

package com.chen.jms.spring.queue.producer;

/**
 * 队列模式:生产者接口
 *
 * @Author LeifChen
 * @Date 2018-11-16
 */
public interface QueueProducerService {

    /**
     * 发送消息
     *
     * @param message
     */
    void sendMessage(String message);
}

1.3 生产者实现类

package com.chen.jms.spring.queue.producer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;

import javax.annotation.Resource;
import javax.jms.Destination;

/**
 * 队列模式:生产者实现
 *
 * @Author LeifChen
 * @Date 2018-11-16
 */
public class QueueProducerServiceImpl implements QueueProducerService {

    @Autowired
    JmsTemplate jmsTemplate;

    @Resource(name = "queueDestination")
    Destination destination;

    @Override
    public void sendMessage(String message) {
        jmsTemplate.send(destination, session -> session.createTextMessage(message));
        System.out.println("发送消息:" + message);
    }
}

1.4 生产者

package com.chen.jms.spring.queue.producer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 队列模式:生产者
 *
 * @Author LeifChen
 * @Date 2018-11-16
 */
public class QueueProducer {

    private static final int COUNT = 100;

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("producer-queue.xml");
        QueueProducerService service = context.getBean(QueueProducerService.class);

        for (int i = 0; i < COUNT; i++) {
            service.sendMessage("test" + i);
        }
        context.close();
    }
}

1.5 消费者消息监听器

package com.chen.jms.spring.queue.consumer;

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

/**
 * 队列模式:消费者消息监听器
 *
 * @Author LeifChen
 * @Date 2018-11-16
 */
public class QueueConsumerMessageListener implements MessageListener {

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

        try {
            System.out.println("接收消息:" + textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

1.6 消费者

package com.chen.jms.spring.queue.consumer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 队列模式:消费者
 *
 * @Author LeifChen
 * @Date 2018-11-16
 */
public class QueueConsumer {

    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("consumer-queue.xml");
    }
}

2. 主题模式

2.1 spring 配置文件

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <!-- ActiveMQ 提供的 ConnectionFactory -->
    <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://127.0.0.1:61616"/>
    </bean>

    <!-- Spring JMS 提供的连接池 -->
    <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
        <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
    </bean>

    <!-- 一个主题目的地,发布订阅模式 -->
    <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg value="topic"/>
    </bean>

    <!-- 配置 JmsTemplate,用于发送消息 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
    </bean>

    <bean class="com.chen.jms.spring.topic.producer.TopicProducerServiceImpl"></bean>

    <!-- 配置消息监听器 -->
    <bean id="topicConsumerMessageListener" class="com.chen.jms.spring.topic.consumer.TopicConsumerMessageListener"></bean>

    <!-- 配置消息容器 -->
    <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="destination" ref="topicDestination"/>
        <property name="messageListener" ref="topicConsumerMessageListener"/>
    </bean>
</beans>

2.2 发布者接口

package com.chen.jms.spring.topic.producer;

/**
 * 主题模式:发布者接口
 *
 * @Author LeifChen
 * @Date 2018-11-16
 */
public interface TopicProducerService {

    /**
     * 发送消息
     *
     * @param message
     */
    void sendMessage(String message);
}

2.3 发布者实现类

package com.chen.jms.spring.topic.producer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;

import javax.annotation.Resource;
import javax.jms.Destination;

/**
 * 主题模式:发布者实现
 *
 * @Author LeifChen
 * @Date 2018-11-16
 */
public class TopicProducerServiceImpl implements TopicProducerService {

    @Autowired
    JmsTemplate jmsTemplate;

    @Resource(name = "topicDestination")
    Destination destination;

    @Override
    public void sendMessage(String message) {
        jmsTemplate.send(destination, session -> session.createTextMessage(message));
        System.out.println("发送消息:" + message);
    }
}

2.4 发布者

package com.chen.jms.spring.topic.producer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 主题模式:发布者
 *
 * @Author LeifChen
 * @Date 2018-11-16
 */
public class TopicProducer {

    private static final int COUNT = 100;

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("producer-topic.xml");
        TopicProducerService service = context.getBean(TopicProducerService.class);

        for (int i = 0; i < COUNT; i++) {
            service.sendMessage("test" + i);
        }
        context.close();
    }
}

2.5 订阅者消息监听器

package com.chen.jms.spring.topic.consumer;

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

/**
 * 主题模式:订阅者消息监听器
 *
 * @Author LeifChen
 * @Date 2018-11-16
 */
public class TopicConsumerMessageListener implements MessageListener {

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

        try {
            System.out.println("接收消息:" + textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

2.6 订阅者

package com.chen.jms.spring.topic.consumer;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 主题模式:订阅者
 *
 * @Author LeifChen
 * @Date 2018-11-16
 */
public class TopicConsumer {

    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("consumer-topic.xml");
    }
}

参考

  1. GitHub
  2. Java消息中间件
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值