一、什么是消息队列

消息队列(Message Queue)是一种在应用程序之间进行异步通信的技术。它用于解耦消息的发送者和接收者,从而实现可靠的消息传递和处理。

消息队列通过引入一个中间代理(消息中间件)来传递消息。发送者将消息发送到消息队列的目标(主题或队列),然后接收者从该目标中获取消息进行处理。消息队列提供了一个缓冲区,允许发送者和接收者在时间上解耦,即使某一方暂时不可用或处理能力不足,消息也不会丢失。

二、消息队列的一些主要特点和用途

        (1). 异步通信:消息队列允许发送者和接收者之间的异步通信,发送者可以继续执行而无需等待接收者的响应。

        (2). 解耦应用程序:通过引入消息队列,发送者和接收者之间的依赖性降低,它们可以独立地开发、部署和扩展。

        (3). 可靠性:消息队列通常提供持久化机制,即使在发送者和接收者之间发生故障或中断,消息也能够得到可靠地传递。

        (4). 扩展性:通过在消息队列中添加更多的消费者,可以实现水平扩展,提高系统的处理能力。

        (5). 流量控制:消息队列可以根据消费者的处理能力来调整消息的传输速率,以避免消息堆积和系统资源的浪费。

三、消息队列可用于各种场景

包括但不限于:

- 异步任务处理:将耗时的任务放入消息队列中,让后台工作者异步处理。
- 事件驱动架构:各个组件之间通过消息进行通信,实现松耦合和可扩展性。
- 分布式系统协调:用于在分布式系统中协调和同步各个节点之间的操作和状态。
- 日志和审计跟踪:将系统的日志和审计记录发送到消息队列中,进行后续处理和分析。

总的来说,消息队列是一种重要的中间件技术,用于提供可靠和可扩展的异步通信机制,帮助构建弹性和高性能的分布式系统。

四、ActiveMq

        无注解,无xml配置带你了解activeMq
        jar包引入

<dependencies>
        <!-- activemq所需要的jar包 -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.15.9</version>
        </dependency>
        <!-- activeMQ对JMS的支持,整合Spring和ActiveMQ -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
            <version>5.15.9</version>
        </dependency>
</dependencies>

        (1)、生产者

import org.apache.activemq.ActiveMQConnectionFactory;

        import javax.jms.*;

public class MessageProducer {

    public static void main(String[] args) throws JMSException {
        // 设置ActiveMQ的连接信息
        String brokerUrl = "tcp://localhost:61616";
        String queueName = "your_queue_name";

        // 创建ActiveMQ连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);

        // 创建连接
        Connection connection = connectionFactory.createConnection();

        // 启动连接
        connection.start();

        // 创建会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // 创建消息目的地(队列)
        Destination destination = session.createQueue(queueName);

        // 创建消息生产者
        MessageProducer producer = session.createProducer(destination);

        // 创建要发送的消息
        TextMessage message = session.createTextMessage();
        // 设置消息内容,这里示例为 <MCCI_IN000011UV02> 消息的 XML 字符串
        String xmlContent = "<MCCI_IN000011UV02>...</MCCI_IN000011UV02>";
        message.setText(xmlContent);

        // 发送消息
        producer.send(message);

        System.out.println("Message sent successfully!");

        // 关闭连接
        connection.close();
    }
}

        (2)、消费者

import org.apache.activemq.ActiveMQConnectionFactory;

        import javax.jms.*;

public class MessageReceiver {

    public static void main(String[] args) throws JMSException {
        // 设置ActiveMQ的连接信息
        String brokerUrl = "tcp://localhost:61616";
        String queueName = "your_queue_name";

        // 创建ActiveMQ连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);

        // 创建连接
        Connection connection = connectionFactory.createConnection();

        // 启动连接
        connection.start();

        // 创建会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // 创建消息目的地(队列)
        Destination destination = session.createQueue(queueName);

        // 创建消息消费者
        MessageConsumer consumer = session.createConsumer(destination);

        // 注册消息监听器
        consumer.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                try {
                    if (message instanceof TextMessage) {
                        TextMessage textMessage = (TextMessage) message;
                        String xmlContent = textMessage.getText();
                        // 在这里处理接收到的消息,解析XML内容等操作
                        System.out.println("Received message: " + xmlContent);
                    }
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });

        // 等待接收消息
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}