【SpringBoot笔记17】使用JMS操作ActiveMQ

本文介绍了如何在SpringBoot应用中使用JMS操作ActiveMQ,涵盖点对点和发布订阅两种模式。在点对点模式下,详细展示了生产者、消费者和测试类的实现;在发布订阅模式中,展示了发布者、订阅者及测试过程。此外,还讨论了解决ActiveMQ连接池配置和点对点与发布订阅不能同时使用的问题,提供了自定义解决方案。
摘要由CSDN通过智能技术生成

ActiveMQ版本:5.15.11

1 JMS原始API操作ActiveMQ

1.1 queue点对点模式

ActiveMqConfig.java

package com.tao.springbootdemo.mq.activemq;

import org.apache.activemq.ActiveMQConnection;

/**
 * activemq的配置
 */
public class ActiveMqConfig {
   

    public static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
    public static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
    public static final String BROKER_URL = "tcp://192.168.48.128:61616";
}

QueueProducer.java生产者:

package com.tao.springbootdemo.mq.activemq.jms.queue;

import com.tao.springbootdemo.mq.activemq.ActiveMqConfig;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jms.*;

/**
 * queue消息生产者
 */
public class QueueProducer {
   
    private static final Logger LOG = LoggerFactory.getLogger(QueueProducer.class);

    private Connection connection;
    private Session session;
    private MessageProducer producer;

    public QueueProducer(String queueName) {
   
        init(queueName);
    }

    /**
     * 初始化
     */
    public void init(String queueName) {
   
        try {
   
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMqConfig.BROKER_URL);
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue(queueName);
            producer = session.createProducer(queue);
        } catch (JMSException e) {
   
            LOG.error("" + e);
        }
    }

    /**
     * 发送消息
     */
    public void sendMsg(String message) {
   
        try {
   
            ActiveMQTextMessage msg = new ActiveMQTextMessage();
            msg.setText(message);
            producer.send(msg);
            session.commit();
            LOG.info(Thread.currentThread().getName() + " send a message: {}", msg.getText());
        } catch (JMSException e) {
   
            LOG.error("" + e);
        }
    }
}

QueueConsumer.java消费者:

package com.tao.springbootdemo.mq.activemq.jms.queue;

import com.tao.springbootdemo.mq.activemq.ActiveMqConfig;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jms.*;

/**
 * queue消息消费者
 */
public class QueueConsumer {
   

    private static final Logger LOG = LoggerFactory.getLogger(QueueConsumer.class);

    private Connection connection;
    private Session session;
    private MessageConsumer consumer;

    public QueueConsumer(String queueName) {
   
        init(queueName);
    }

    /**
     * 初始化
     */
    public void init(String queueName) {
   
        try {
   
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMqConfig.BROKER_URL);
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue(queueName);
            consumer = session.createConsumer(queue);
        } catch (JMSException e) {
   
            LOG.error("" + e);
        }
    }

    /**
     * 接收消息
     */
    public void receiveMsg() {
   
        try {
   
            consumer.setMessageListener(new MessageListener() {
   
                @Override
                public void onMessage(Message message) {
   
                    try {
   
                        TextMessage textMessage = (TextMessage) message;
                        LOG.info(Thread.currentThread().getName() + " receive a message : {}", textMessage.getText());
                    } catch (JMSException e) {
   
                        LOG.error("" + e);
                    }
                }
            });
        } catch (JMSException e) {
   
            LOG.error("" + e);
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值