写一个简单的ActiveMQ消息队列,ActiveMQ的安装以及启动请看我的上一篇博客 谢谢!!!!

本文介绍了如何使用ActiveMQ实现消息队列的功能,包括消息发送端和接收端的代码实现,以及如何配置ActiveMQ服务器来确保消息的持久化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从前有个山,山上有个庙,庙里有个大帅哥正在看我的博客。

不多说,上代码!

这是消息发送端的代码
package com.example.demo.activitemp;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ShakeDownActiviteMQ {

public static void main(String[] args) throws InterruptedException {

    Thread tl = new Thread(new Producer());
    tl.start();

}

}

class Producer implements Runnable{

public static final int SEND_NUM = 5 ;

@Override
public void run() {

    ConnectionFactory connectionFactory ;
    Connection connection = null ;
    Session session = null;
    Destination destination ;
    MessageProducer messageProducer ;
    try {

        //创建工厂连接
        connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61616");

        //通过工厂获取连接
        connection = connectionFactory.createConnection();

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

        //获得连接的session
        session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE );

        //获取连接目的地
        destination = session.createQueue("cxx");

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

        //支持消息持久化
        messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);

        //发送消息
        sendMsg(session,messageProducer);

        //提交会话
        session.commit();

    } catch (JMSException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        if(connection != null){
            try {
                connection.close();
            } catch (JMSException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(session != null){
            try {
                session.close();
            } catch (JMSException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

public static void sendMsg(Session session,MessageProducer producer) throws JMSException{

    for(int i = 1 ; i <= SEND_NUM ; i++){

        TextMessage textMessage = session.createTextMessage("添加第"+i+"几张票");
        System.out.println(textMessage);
        producer.send(textMessage);

    }
}

}

这是消息接收端的代码

package com.example.demo.activitemp;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

public class Consumer implements Runnable{

public static final int SEND_NUM = 5 ;

public static void main(String[] args) throws InterruptedException {

    Thread tl = new Thread(new Consumer());
    tl.start();

}

@Override
public void run() {

    ConnectionFactory connectionFactory ;
    Connection connection = null ;
    Session session = null;
    Destination destination ;
    MessageConsumer consumer ;

    try {

        //创建工厂的连接
        connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61616");

        //通过工厂获取连接
        connection = connectionFactory.createConnection();

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

        //使用连接创建一个会话
        session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);

        //获取session的队列
        destination = session.createQueue("cxx");

        //创建一个消费者
        consumer = session.createConsumer(destination);

        TextMessage tm = (TextMessage)consumer.receive(1000);

        while(true){
            if(tm != null){
                System.out.println(tm.getText()+"恭喜------抢票成功");
            }else{
                System.out.println("抢票失败");
                break ;
            }
        }

        session.commit();

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

        if(connection != null){
            try {
                connection.close();
            } catch (JMSException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(session != null){
            try {
                session.close();
            } catch (JMSException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

}

运行发送端之后
这里写图片描述

运行消息接收端之后

这里写图片描述

然后打开 帐号admin 密码 admin 看看自己的消息
http://127.0.0.1:8161/admin/queues.jsp

这里写图片描述

写到一半又有新任务了,同志们,加油吧!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值