ActiveMQ点对点消息通信demo

本文详细介绍如何使用ActiveMQ搭建消息队列环境,并通过示例代码演示消息的发送与接收过程。具体步骤包括ActiveMQ的下载、安装及启动,配置队列,以及使用Java API进行消息交互。

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

1.下载ActiveMQ,安装并启动activeMQ,

2.登录控制台,http://localhost:8161/admin/index.jsp     default: admin/admin

新建queue名为:FirstQueue


依赖包:

<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-core</artifactId>
    <version>5.7.0</version>
</dependency>


package org.jun.util;

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

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

public class ActiveMQSenderUtil {
	
	public static void main(String arg[]) {
		Sender("FirstQueue", "通知:everybody,高性能activeMQ!");
	}

    /**
     * 发送消息
     *  
     * @param content	发送内容
     * @param queueName	 队列名
     */
    public static void Sender(String queueName, String content) {

    	// ConnectionFactory :连接工厂,JMS 用它创建连接工厂实例,此处采用ActiveMq的实现jar
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
        		ActiveMQConnection.DEFAULT_USER,
        		ActiveMQConnection.DEFAULT_PASSWORD,
        		"tcp://localhost:61616"); //tcp地址
        
        // Connection :JMS 客户端到JMS Provider 的连接
        Connection connection = null;
        // Session: 一个发送或接收消息的线程
        Session session;
        // Destination :消息的目的地;消息发送给谁.
        Destination destination;
        // MessageProducer:消息发送者
        MessageProducer producer;
        try {
            // 构造从工厂得到连接对象
            connection = connectionFactory.createConnection();
            // 启动
            connection.start();
            // 获取操作连接
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
            destination = session.createQueue(queueName);
            // 得到消息生成者【发送者】
            producer = session.createProducer(destination);
            // 设置不持久化,此处学习,实际根据项目决定
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            // 构造消息,此处写死,项目就是参数,或者方法获取
            TextMessage message = session.createTextMessage(content);
            System.out.println("ActiveMq 发送的消息:" + content);
            producer.send(message);// 发送消息到目的地方
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != connection) {
                	connection.close();
                }
            } catch (Throwable ignore) {
            }
        }
    }
}
package org.jun.util;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQReceiverUtil {
	
	public static void main(String args[]) {
		queueReceiver("FirstQueue");
	}

	/**
	 * 接收消息
	 * 
	 * @param quequName	队列名
	 * @return
	 */
	public static String queueReceiver(String quequName){
    	// ConnectionFactory :连接工厂,JMS 用它创建连接
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,
                "tcp://localhost:61616");//tcp地址
        // Connection :JMS 客户端到JMS Provider 的连接
        Connection connection = null;
        // Session: 一个发送或接收消息的线程
        Session session;
        // Destination :消息的目的地;消息发送给谁.
        Destination destination;
        // 消费者,消息接收者
        MessageConsumer consumer;
        String receiveMsg = "";
        try {
            // 构造从工厂得到连接对象
            connection = connectionFactory.createConnection();
            // 启动
            connection.start();
            // 获取操作连接
            session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
            // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
            destination = session.createQueue(quequName);
            consumer = session.createConsumer(destination);
            while (true) {
                //设置接收者接收消息的时间,为了便于测试,这里谁定为100s
                TextMessage message = (TextMessage) consumer.receive(100000);
                if (null != message) {
                	receiveMsg = message.getText();
                	System.out.println("收到消息:" + message.getText());
                    
                } else {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != connection) {
                	connection.close();                	
                }
            } catch (Throwable ignore) {
            }
        }
		return receiveMsg;
    }
}

运行结果:

113729_Afz1_1789904.png

113729_Aak4_1789904.png










转载于:https://my.oschina.net/xiejunbo/blog/505252

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值