一、ActiveMQ
-
介紹
Apache ActiveMQ是Apache軟件基金會所研發的開放源代碼消息中間件;由於ActiveMQ是一個純Java程序,因此只需要操作系統支持Java虛擬機,ActiveMQ便可執行。下載地址:http://activemq.apache.org/activemq-5153-release.html 官方教程:http://activemq.apache.org/getting-started.html
-
網頁地址與queue目錄介紹
瀏覽器輸入:http://127.0.0.1:8161/admin/ 用戶名與密碼:adminqueue目錄介紹:
Name:隊列名稱。
Number Of Pending Messages:等待消費的消息個數。
Number Of Consumers:當前連接的消費者數目
Messages Enqueued:進入隊列的消息總個數,包括出隊列的和待消費的,這個數量只增不減。
Messages Dequeued:已經消費的消息數量。 -
ActiveMQ之點對點操作
官方參考網址 :目錄:4.13.1
a、引入依賴:<!-- 整合消息隊列ActiveMQ --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <!-- 若配置線程池則加入 --> <dependency> <groupId>org.messaginghub</groupId> <artifactId>pooled-jms</artifactId> </dependency>
b、application.properties配置文件
#整合jms測試,安裝在別的機器,防火牆和端口號記得開放,若防火牆已關閉,此處可忽略 spring.activemq.broker-url=tcp://127.0.0.1:61616 #集羣配置 #spring.activemq.broker-url=failover:(tcp://localhost:61616,tcp://localhost:61617) spring.activemq.user=admin spring.activemq.password=admin #使用連接池 spring.activemq.pool.enabled=true #連接池最大連接數 spring.activemq.pool.max-connections=100
c、在啓動類加註解
@SpringBootApplication @EnableJms //開啓支持jms public class Springboot07Application { @Bean /** * 將當前方法注入到spring,拿到一個Queue對象 * @Component 將整個類注入到spring產生一個bean對象 * @Bean 是將當前方法注入到spring,把當前方法返回的對象作爲-個bean對象使用 */ public Queue queue(){ return new ActiveMQQueue("test.queue"); } }
d、提供一個發送消息的服務 ----- 寫一個service層
//發送消息的服務,也叫做消息生產者 public interface ActiveService { /** * * @param destination //指定接收消息的對列名 * @param msg //發送的消息 */ public void sendMsg(Destination destination,String msg); /** * 使用默認消息隊列 * @param msg */ public void defaultSendMsg(String msg); }
e、實現接口
通過JmsMessgingTemplate發送消息@Service public class ActiveServiceImpl implements ActiveService { @Autowired private Queue queue; //隊列對象 @Autowired private JmsMessagingTemplate template; //消息隊列模板 ,類似於jdbcTemplate模板 @Override public void sendMsg(Destination destination, String msg) { //將消息發送到指定消息隊列destination template.convertAndSend(destination,msg); } @Override public void defaultSendMsg(String msg) { //將消息發送到默認消息隊列 template.convertAndSend(msg); } }
f、從controller層接收消息
這裏使用了JsonData工具類,返回json數據@RestController @RequestMapping("/mq") public class ActiveController { @Autowired private ActiveService activeService; @RequestMapping("/active") //這是前臺發過來的消息 public Object active(String msg){ Destination destination = new ActiveMQQueue("test.queue");//指定接收隊列 activeService.sendMsg(destination,msg); return JsonData.buildSuccess(); } }
g、在jsm文件夾中創建消息消費者,消費掉剛纔發送的消息
@Component public class ActiveConsumer { /** * 監聽活動中的消息,並消費 --這是實時監聽 * @JmsListener 消息監聽(實時監聽,只有發現有消息發送過來,會立刻消費 * @param msg */ @JmsListener(destination = "test.queue") //指定接收隊列 public void consumeMsg(String msg){ System.out.println("ActiveConsume收到的報文是:" + msg); } }
h、測試
-
ActiveMQ之發佈訂閱模式
訂閱模式,一個消息可以被多個消費者消費,這是和點對點模式的最大區別。同時也支持點對點和發佈訂閱同時使用。a、在配置文件配置
#開啓訂閱模式,默認是點對點模式 spring.jms.pub-sub-domain=true
b、啓動類同樣需要@EnableJms ,然後加入一個topic 主題對象
/** * 訂閱者模式 --發佈者 * @return */ @Bean public Topic topic(){ return new ActiveMQTopic("test.topic");//指定接收隊列 }
c、業務層提供生產者(也就是消息發佈者)
/** * 訂閱者模式:消息發佈者 * @param msg */ public void producer(String msg);
d、實現業務層
@Autowired private Topic topic; @Override public void producer(String msg) { template.convertAndSend(topic,msg); }
e、創建消費者
@Component public class TopicConsumer { /** * 訂閱模式消費者 * @param msg */ @JmsListener(destination = "test.topic") //指定接收隊列 public void consumer1(String msg){ System.out.println("消費者1報文:" + msg); } @JmsListener(destination = "test.topic") public void consumer2(String msg){ System.out.println("消費者2報文:" + msg); } @JmsListener(destination = "test.topic") public void consumer3(String msg){ System.out.println("消費者3報文:" + msg); } }
f、在controller層編寫代碼
/** * 訂閱者模式 --消息生產 * @param msg * @return */ @RequestMapping("/topic") public Object topic(String msg){ activeService.producer(msg); return JsonData.buildSuccess(); }
g、測試
-
同時支持點對點和訂閱者模式
a、修改配置文件,把
spring.jms.pub-sub-domain=true
註釋
b、在啓動類添加代碼/** * 同時支持點對點和訂閱者模式 * @param activeMQConnectionFactory * @return */ @Bean public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) { DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory(); bean.setPubSubDomain(true); bean.setConnectionFactory(activeMQConnectionFactory); return bean; }
c、修改訂閱者註解
在原訂閱者模式註解@JmsListener
追加containerFactory="jmsListenerContainerTopic"
@Component public class TopicConsumer { /** * 訂閱模式消費者 * @param msg */ @JmsListener(destination = "test.topic",containerFactory="jmsListenerContainerTopic") public void consumer1(String msg){ System.out.println("消費者1報文:" + msg); } @JmsListener(destination = "test.topic",containerFactory="jmsListenerContainerTopic") public void consumer2(String msg){ System.out.println("消費者2報文:" + msg); } @JmsListener(destination = "test.topic",containerFactory="jmsListenerContainerTopic") public void consumer3(String msg){ System.out.println("消費者3報文:" + msg); } }
d、測試