亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot中怎么利用ActiveMQ實現延遲消息

發布時間:2021-08-06 16:37:42 來源:億速云 閱讀:521 作者:Leah 欄目:編程語言

這篇文章將為大家詳細講解有關SpringBoot中怎么利用ActiveMQ實現延遲消息,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

一、安裝activeMQ

Windows下安裝ActiveMQ:

到官網(http://activemq.apache.org/download-archives.html)下載最新發布的壓縮包(我下的是5.15.9)到本地后解壓(我解壓到D盤Dev目錄下)即可。進入解壓后的bin目錄,我是64位機器,再進入win64目錄后,雙擊activemq.bat啟動:

wrapper | --> Wrapper Started as Consolewrapper | Launching a JVM...jvm 1 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.orgjvm 1 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.jvm 1 |jvm 1 | Java Runtime: Oracle Corporation 1.8.0_181 C:\Program Files\Java\jre1.8.0_181jvm 1 | Heap sizes: current=125952k free=115299k max=932352kjvm 1 | JVM args: -Dactivemq.home=../.. -Dactivemq.base=../.. -Djavax.net.ssl.keyStorePassword=password -Djavax.net.ssl.trustStorePassword=password -Djavax.net.ssl.keyStore=../../conf/broker.ks -Djavax.net.ssl.trustStore=../../conf/broker.ts -Dcom.sun.management.jmxremote -Dorg.apache.activemq.UseDedicatedTaskRunner=true -Djava.util.logging.config.file=logging.properties -Dactivemq.conf=../../conf -Dactivemq.data=../../data -Djava.security.auth.login.config=../../conf/login.config -Xmx1024m -Djava.library.path=../../bin/win64 -Dwrapper.key=mChNCWMZ2FoXhZ9g -Dwrapper.port=32000 -Dwrapper.jvm.port.min=31000 -Dwrapper.jvm.port.max=31999 -Dwrapper.pid=3500 -Dwrapper.version=3.2.3 -Dwrapper.native_library=wrapper -Dwrapper.cpu.timeout=10 -Dwrapper.jvmid=1jvm 1 | Extensions classpath:jvm 1 | [..\..\lib,..\..\lib\camel,..\..\lib\optional,..\..\lib\web,..\..\lib\extra]jvm 1 | ACTIVEMQ_HOME: ..\..jvm 1 | ACTIVEMQ_BASE: ..\..jvm 1 | ACTIVEMQ_CONF: ..\..\confjvm 1 | ACTIVEMQ_DATA: ..\..\datajvm 1 | Loading message broker from: xbean:activemq.xmljvm 1 | INFO | Refreshing org.apache.activemq.xbean.XBeanBrokerFactory$1@f0ef68d: startup date [Fri May 24 15:16:21 CST 2019]; root of context hierarchyjvm 1 | INFO | Using Persistence Adapter: KahaDBPersistenceAdapter[D:\Dev\apache-activemq-5.15.9\bin\win64\..\..\data\kahadb]jvm 1 | INFO | PListStore:[D:\Dev\apache-activemq-5.15.9\bin\win64\..\..\data\localhost\tmp_storage] startedjvm 1 | INFO | Apache ActiveMQ 5.15.9 (localhost, ID:wulf00-51057-1558682182909-0:1) is startingjvm 1 | INFO | Listening for connections at: tcp://wulf00:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600jvm 1 | INFO | Connector openwire startedjvm 1 | INFO | Listening for connections at: amqp://wulf00:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600jvm 1 | INFO | Connector amqp startedjvm 1 | INFO | Listening for connections at: stomp://wulf00:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600jvm 1 | INFO | Connector stomp startedjvm 1 | INFO | Listening for connections at: mqtt://wulf00:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600jvm 1 | INFO | Connector mqtt startedjvm 1 | INFO | Starting Jetty serverjvm 1 | INFO | Creating Jetty connectorjvm 1 | WARN | ServletContext@o.e.j.s.ServletContextHandler@17bc7c8a{/,null,STARTING} has uncovered http methods for path: /jvm 1 | INFO | Listening for connections at ws://wulf00:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600jvm 1 | INFO | Connector ws startedjvm 1 | INFO | Apache ActiveMQ 5.15.9 (localhost, ID:wulf00-51057-1558682182909-0:1) startedjvm 1 | INFO | For help or more information please see: http://activemq.apache.orgjvm 1 | WARN | Store limit is 102400 mb (current store usage is 0 mb). The data directory: D:\Dev\apache-activemq-5.15.9\bin\win64\..\..\data\kahadb only has 92649 mb of usable space. - resetting to maximum available disk space: 92649 mbjvm 1 | INFO | No Spring WebApplicationInitializer types detected on classpathjvm 1 | INFO | ActiveMQ WebConsole available at http://0.0.0.0:8161/jvm 1 | INFO | ActiveMQ Jolokia REST API available at http://0.0.0.0:8161/api/jolokia/jvm 1 | INFO | Initializing Spring FrameworkServlet 'dispatcher'jvm 1 | INFO | No Spring WebApplicationInitializer types detected on classpathjvm 1 | INFO | jolokia-agent: Using policy access restrictor classpath:/jolokia-access.xml

默認端口8161,訪問下http://localhost:8161/admin,用戶名密碼都是admin,進入控制臺頁面:

我們用坐上方的Queues來創建一個叫vboxlog的隊列:

二、修改activeMQ配置文件

broker新增配置信息 schedulerSupport="true"

<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}" schedulerSupport="true" > <destinationPolicy> <policyMap> <policyEntries> <policyEntry topic=">" > <!-- The constantPendingMessageLimitStrategy is used to prevent slow topic consumers to block producers and affect other consumers by limiting the number of messages that are retained For more information, see: http://activemq.apache.org/slow-consumer-handling.html --> <pendingMessageLimitStrategy> <constantPendingMessageLimitStrategy limit="1000"/> </pendingMessageLimitStrategy> </policyEntry> </policyEntries> </policyMap> </destinationPolicy>

三、創建SpringBoot工程

1、配置ActiveMQ工廠信息,信任包必須配置否則會報錯

package com.example.demoactivemq.config;import org.apache.activemq.ActiveMQConnectionFactory;import org.apache.activemq.RedeliveryPolicy;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.util.ArrayList;import java.util.List;/** * @author shanks on 2019-11-12 */@Configurationpublic class ActiveMqConfig { @Bean public ActiveMQConnectionFactory factory(@Value("${spring.activemq.broker-url}") String url){ ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url); // 設置信任序列化包集合 List<String> models = new ArrayList<>(); models.add("com.example.demoactivemq.domain"); factory.setTrustedPackages(models); return factory; }}

消息實體類

package com.example.demoactivemq.domain;import lombok.Builder;import lombok.Data;import java.io.Serializable;/** * @author shanks on 2019-11-12 */@Builder@Datapublic class MessageModel implements Serializable { private String titile; private String message;}

生產者

package com.example.demoactivemq.producer;import lombok.extern.slf4j.Slf4j;import org.apache.activemq.ScheduledMessage;import org.apache.activemq.command.ActiveMQQueue;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.jms.JmsProperties;import org.springframework.jms.core.JmsMessagingTemplate;import org.springframework.stereotype.Service;import javax.jms.*;import java.io.Serializable;/** * 消息生產者 * * @author shanks */@Service@Slf4jpublic class Producer { public static final Destination DEFAULT_QUEUE = new ActiveMQQueue("delay.queue"); @Autowired private JmsMessagingTemplate template; /** * 發送消息 * * @param destination destination是發送到的隊列 * @param message message是待發送的消息 */ public <T extends Serializable> void send(Destination destination, T message) { template.convertAndSend(destination, message); } /** * 延時發送 * * @param destination 發送的隊列 * @param data 發送的消息 * @param time 延遲時間 */ public <T extends Serializable> void delaySend(Destination destination, T data, Long time) { Connection connection = null; Session session = null; MessageProducer producer = null; // 獲取連接工廠 ConnectionFactory connectionFactory = template.getConnectionFactory(); try { // 獲取連接 connection = connectionFactory.createConnection(); connection.start(); // 獲取session,true開啟事務,false關閉事務 session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 創建一個消息隊列 producer = session.createProducer(destination); producer.setDeliveryMode(JmsProperties.DeliveryMode.PERSISTENT.getValue()); ObjectMessage message = session.createObjectMessage(data); //設置延遲時間 message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time); // 發送消息 producer.send(message); log.info("發送消息:{}", data); session.commit(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (producer != null) { producer.close(); } if (session != null) { session.close(); } if (connection != null) { connection.close(); } } catch (Exception e) { e.printStackTrace(); } } }}

消費者

package com.example.demoactivemq.producer;import com.example.demoactivemq.domain.MessageModel;import lombok.extern.slf4j.Slf4j;import org.springframework.jms.annotation.JmsListener;import org.springframework.stereotype.Component;/** * 消費者 */@Component@Slf4jpublic class Consumer { @JmsListener(destination = "delay.queue") public void receiveQueue(MessageModel message) { log.info("收到消息:{}", message); }}

application.yml

spring: activemq: broker-url: tcp://localhost:61616

測試類

package com.example.demoactivemq;import com.example.demoactivemq.domain.MessageModel;import com.example.demoactivemq.producer.Producer;import org.junit.jupiter.api.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@SpringBootTest(classes = DemoActivemqApplication.class)@RunWith(SpringRunner.class)class DemoActivemqApplicationTests { /** * 消息生產者 */ @Autowired private Producer producer; /** * 及時消息隊列測試 */ @Test public void test() { MessageModel messageModel = MessageModel.builder() .message("測試消息") .titile("消息000") .build(); // 發送消息 producer.send(Producer.DEFAULT_QUEUE, messageModel); } /** * 延時消息隊列測試 */ @Test public void test2() { for (int i = 0; i < 5; i++) { MessageModel messageModel = MessageModel.builder() .titile("延遲10秒執行") .message("測試消息" + i) .build(); // 發送延遲消息 producer.delaySend(Producer.DEFAULT_QUEUE, messageModel, 10000L); } try { // 休眠100秒,等等消息執行 Thread.currentThread().sleep(100000L); } catch (InterruptedException e) { e.printStackTrace(); } }}

執行結果

2019-11-12 22:18:52.939 INFO 17263 --- [ main] c.e.demoactivemq.producer.Producer : 發送消息:MessageModel(titile=延遲10秒執行, message=測試消息0)2019-11-12 22:18:52.953 INFO 17263 --- [ main] c.e.demoactivemq.producer.Producer : 發送消息:MessageModel(titile=延遲10秒執行, message=測試消息1)2019-11-12 22:18:52.958 INFO 17263 --- [ main] c.e.demoactivemq.producer.Producer : 發送消息:MessageModel(titile=延遲10秒執行, message=測試消息2)2019-11-12 22:18:52.964 INFO 17263 --- [ main] c.e.demoactivemq.producer.Producer : 發送消息:MessageModel(titile=延遲10秒執行, message=測試消息3)2019-11-12 22:18:52.970 INFO 17263 --- [ main] c.e.demoactivemq.producer.Producer : 發送消息:MessageModel(titile=延遲10秒執行, message=測試消息4)2019-11-12 22:19:03.012 INFO 17263 --- [enerContainer-1] c.e.demoactivemq.producer.Consumer : 收到消息:MessageModel(titile=延遲10秒執行, message=測試消息0)2019-11-12 22:19:03.017 INFO 17263 --- [enerContainer-1] c.e.demoactivemq.producer.Consumer : 收到消息:MessageModel(titile=延遲10秒執行, message=測試消息1)2019-11-12 22:19:03.019 INFO 17263 --- [enerContainer-1] c.e.demoactivemq.producer.Consumer : 收到消息:MessageModel(titile=延遲10秒執行, message=測試消息2)2019-11-12 22:19:03.020 INFO 17263 --- [enerContainer-1] c.e.demoactivemq.producer.Consumer : 收到消息:MessageModel(titile=延遲10秒執行, message=測試消息3)2019-11-12 22:19:03.021 INFO 17263 --- [enerContainer-1] c.e.demoactivemq.producer.Consumer : 收到消息:MessageModel(titile=延遲10秒執行, message=測試消息4)

關于SpringBoot中怎么利用ActiveMQ實現延遲消息就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

闽侯县| 犍为县| 白朗县| 扎赉特旗| 昌乐县| 左贡县| 宜城市| 德清县| 横山县| 博爱县| 综艺| 仁寿县| 瑞安市| 改则县| 涟水县| 龙门县| 寿阳县| 通州市| 淄博市| 南澳县| 岑巩县| 云南省| 疏勒县| 武清区| 保定市| 林甸县| 溧阳市| 和平区| 渭源县| 库伦旗| 扶风县| 万州区| 晋江市| 沿河| 筠连县| 唐河县| 克东县| 永和县| 汉阴县| 新民市| 上思县|