Java实现ActiveMQ多种消息模式

本文详细介绍如何使用ActiveMQ实现点对点(P2P)和发布订阅消息模式,包括配置依赖、建立连接、发送和接收消息的过程。通过具体代码示例,展示了生产者和消费者的实现方式。

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

找到上图文件启动ActiveMQ程序,如下图所示

使用ActiveMQ完成点对点(p2p)通讯模式

引入pom文件依赖

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

编写ActiveMQ连接的工具类

public class ActiveMQUntils {
	private static final String USERNAME="admin";
	private static final String PASSWORD="admin";
	private static final String BROKERURL="tcp://127.0.0.1:61616";
	public static final String QUEUENAME="FirstQueueName";
	/**
	 * 获取ActiveMQ的连接
	 * @param connection
	 * @param session
	 */
	public static  Connection getActiveMQConnection(){
		ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKERURL);
		Connection connection=null;
		try {
			connection = activeMQConnectionFactory.createConnection();
			//Session createSession = connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);
		} catch (JMSException e) {
			e.printStackTrace();
			System.out.println("创建ActiveMQ消息队列失败"+e.getCause());
		}
		return connection ;
	}
	
	/**
	 * 关闭ActiveMQ的连接
	 * @param connection
	 * @param session
	 */
	public static void closeActiveMQConnection(Connection connection,Session session){
		if(session!=null){
			try {
				session.close();
			} catch (JMSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(connection!=null){
			try {
				connection.close();
			} catch (JMSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		}
		
	}
}

编写ActiveMQ生产者类

public class Producter {
	public static void main(String[] args) {	
	try {
		//获取ActiveMQ连接
		Connection connection = ActiveMQUntils.getActiveMQConnection();
		connection.start();
		// Session: 一个发送或接收消息的线程
		Session session = connection.createSession(Boolean.FALSE,Session.CLIENT_ACKNOWLEDGE);
		Destination destination = session.createQueue(ActiveMQUntils.QUEUENAME);
		//创建消息的生产生产者
		MessageProducer producer = session.createProducer(destination);
		//设置不持久化
		producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
		for (int i = 0; i < 5; i++) {
			sendMsg(session, producer, i);
			//session.commit();
			System.out.println("我在生产消息"+i);
		}
		ActiveMQUntils.closeActiveMQConnection(connection, session);
		} catch (Exception e) {
			// TODO: handle exception
			e.getStackTrace();
			System.out.println(e.getCause());
		}
	}
	
	/**
	 * 
	 * @param session
	 * @param producer
	 * @param i
	 */
	public static void sendMsg(Session session, MessageProducer producer, int i)  {
		try {
			// 创建一条文本消息
			TextMessage message = session.createTextMessage("Hello ActiveMQ!" + i);
			// 通过消息生产者发出消息
			producer.send(message);
		} catch (Exception e) {
			e.getStackTrace();
		}
	}

 编写ActiveMQ生产者类 

package com.hongyu.test;

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

import com.hongyu.untils.ActiveMQUntils;

public class Consumer {

	public static void main(String[] args) {	
	try {
		//获取ActiveMQ连接
		Connection connection = ActiveMQUntils.getActiveMQConnection();
		connection.start();
		// Session: 一个发送或接收消息的线程
		Session session = connection.createSession(Boolean.FALSE,Session.CLIENT_ACKNOWLEDGE);
		Destination destination = session.createQueue(ActiveMQUntils.QUEUENAME);
		//创建消息的生产生产者
	    MessageConsumer consumer = session.createConsumer(destination);
	    while(true){
	    	TextMessage message = (TextMessage) consumer.receive(); 
	    	if(message!=null){
		    	System.out.println( message.getText());
		    	message.acknowledge();//手动签收
		    	//session.commit();
	    	}
	    	else{
	    		break;
	    	}
	   }
	    ActiveMQUntils.closeActiveMQConnection(connection, session);
		} catch (Exception e) {
			// TODO: handle exception
			e.getStackTrace();
			System.out.println(e.getCause());
		}

	}

}

发布订阅消息模式

生产者:

public class Producter {
	private static final String TopicName="FirstTopicName";
	public static void main(String[] args) {	
	try {
		//获取ActiveMQ连接
		Connection connection = ActiveMQUntils.getActiveMQConnection();
		connection.start();
		// Session: 一个发送或接收消息的线程
		Session session = connection.createSession(Boolean.FALSE,Session.CLIENT_ACKNOWLEDGE);
		Topic topic = session.createTopic(TopicName);
		//创建消息的生产生产者
		MessageProducer producer = session.createProducer(topic);
		//设置不持久化
		producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
		for (int i = 0; i < 5; i++) {
			TextMessage message = session.createTextMessage("Hello ActiveMQ!" + i);
			producer.send(message);
			//session.commit();
			System.out.println("我在生产消息"+i);
		}
		ActiveMQUntils.closeActiveMQConnection(connection, session);
		} catch (Exception e) {
			// TODO: handle exception
			e.getStackTrace();
			System.out.println(e.getCause());
		}

	}
}

消费者:

public class Consumer {
	private static final String TopicName="FirstTopicName";
	public static void main(String[] args) {	
	try {
		//获取ActiveMQ连接
		Connection connection = ActiveMQUntils.getActiveMQConnection();
		connection.start();
		// Session: 一个发送或接收消息的线程
		Session session = connection.createSession(Boolean.FALSE,Session.CLIENT_ACKNOWLEDGE);
		Topic topic = session.createTopic(TopicName);
		//创建消息的生产生产者
	    MessageConsumer consumer = session.createConsumer(topic);
	    while(true){
	    	TextMessage message = (TextMessage) consumer.receive(); 
	    	if(message!=null){
		    	System.out.println( message.getText());
		    	message.acknowledge();//手动签收
		    	//session.commit();
	    	}
	    	else{
	    		break;
	    	}
	   }
	    ActiveMQUntils.closeActiveMQConnection(connection, session);
		} catch (Exception e) {
			// TODO: handle exception
			e.getStackTrace();
			System.out.println(e.getCause());
		}

	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值