在ActiveMQ中的點對點模式中存在多個消費者

消息隊列一般有兩種模型

1.點對點模型(基於隊列 Point to Point,PTP) 每個消息只能有一個消費者。消息的生產者和消費者之間沒有時間上的 相關性.可以有多個發送者,但只能被一個消費者消費。 一個消息只能被一個接受者接受一次 生產者把消息發送到隊列中(Queue),接受者無需訂閱,當接受者未接受到消息時就會處於阻塞狀態

2.  發佈者/訂閱者模型(基於主題的Publish/Subscribe,pub/sub) 每個消息可以有多個消費者。 生產者和消費者之間有時間上的相關性。訂閱一個主題的消費者只能消 費自它訂閱之後發佈的消息. 允許多個接受者,類似於廣播的方式 生產者將消息發送到主題上(Topic) 接受者必須先訂閱  注:持久化訂閱者:特殊的消費者,告訴主題,我一直訂閱着,即使網絡斷開,消息服務器也記住所有持久化訂閱者,如果有新消息,也會知道必定有人回來消費。

在SpringBoot中寫的實例

連接ActiveMQ工具類:


/*
 * Copyright @ 2019 com.iflysse.trains
 * 01SpringBoot 下午2:18:30
 * All right reserved.
 *
 */

package com.dcx.comm.utils;

import java.util.List;

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.ScheduledMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.connection.ConnectionFactoryUtils;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.JmsUtils;
import org.springframework.stereotype.Component;

/**
 * @ClassName: ActiveMqUtils
 * @author: cxding
 * @createTime: 2019年4月26日 下午2:18:30
 * @version: v1.0
 */
@Component
public class ActiveMqUtils {
	/**
	 * 注入JMS
	 */
	@Autowired
	private JmsTemplate jmsTemplate;


	/**
	 * 設置普通
	 * @Title: sendNorMolMessage 
	 * @author: cxding
	 * @createTime: 2019年4月28日 下午1:03:56
	 * @param destination
	 * @param text void
	 */
	public <T> void sendNorMolMessage(Destination destination, String text) {
		// 連接工廠
		ConnectionFactory connectionFactory = jmsTemplate.getConnectionFactory();
		Connection connection = null;
		Session session = null;
		MessageProducer producer = null;
		try {
			// 創建鏈接
			connection = connectionFactory.createConnection();
			connection.start();
			// 創建session,開啓事物
			session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
			// 創建生產者
			producer = session.createProducer(destination);
			// 設置持久化
			producer.setDeliveryMode(DeliveryMode.PERSISTENT);
			// 設置過期時間
			//producer.setTimeToLive(time);
			TextMessage message = session.createTextMessage(text);
			producer.send(message);
			// 提交
			session.commit();
		} catch (JMSException e) {
			throw new RuntimeException(e);
		} finally {
			// 關閉連接
			close(producer, session, connection, connectionFactory);
		}
	}

}

連接信息配置application.properties

#-------------------------------activeMQ--------------------------------
# active mq
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=root
spring.activemq.password=pass

控制層

@PostMapping("/producer")
	@ApiOperation(value="產生消息隊列(點對點模式,監聽器目前只監聽:“queueSend”)",response=Result.class)
	public Result<String> createMq(@ApiParam(value = "隊列目的地", required = false,defaultValue="queueSend") @RequestParam String quenName,
			@ApiParam(value = "隊列消息內容", required = false) @RequestParam String text){
		Result<String> result = stuService.createMq(quenName,text);
		return result;
	}

生產者:實現層

/**
	 * 產生點對點的消息隊列
	 */
	@Override
	public Result<String> createMq(String quenName,String text) {
		Destination destination = new ActiveMQQueue(quenName);
		for (int i = 0; i < 10; i++) {
			utils.sendNorMolMessage(destination, text+i);
		}
		
		return new Result<String>("消息已經產生",true);
	}

監聽消費,設置兩個監聽者

@Component
public class ActiveMqListenConfig {
	
	@Autowired
	private ActiveMqUtils util;
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@JmsListener(destination="queueSend")
	 public void recieveTaskMq(String message) {
		//Destination destination = new ActiveMQQueue("easySend");  
		logger.info("消費者1監聽到的消息是:"+message+",並且這條消息已經被消費了");
		 //再次推送消息
		//util.sendMessage(destination, "監聽到消息,給你返回");
	 }
	
	@JmsListener(destination="queueSend")
	 public void recieveTaskMq2(String message) {
		//Destination destination = new ActiveMQQueue("easySend");  
		logger.info("消費者2監聽到的消息是:"+message+",並且這條消息已經被消費了");
		 //再次推送消息
		//util.sendMessage(destination, "監聽到消息,給你返回");
	 }

}

運行項目後

查看結果

由結果可以看出,有兩個消費者,並且產生的10條消息已經被消費了,從控制檯結果可以看出:點對點模式並不是只能有一個消費者,而是一條消息只能有一個消費者消費。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章