activemq整合springboot使用(個人微信小程式用)
1.引入依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency>
2.修改配置檔案(其實配置也是預設值,不配置也可以)
spring.activemq.in-memory=true spring.activemq.pool.enabled=false
3.新增ActiveMq連線池(如果不開啟連線池,則每傳送一條資料建立一個連線)
①.新增依賴 <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> </dependency> ②.修改配置檔案 #服務埠,8080被另一服務佔用 server.port=9090 spring.activemq.broker-url=tcp://127.0.0.1:61616 # 在考慮結束之前等待的時間 #spring.activemq.close-timeout=15s # 預設代理URL是否應該在記憶體中。如果指定了顯式代理,則忽略此值。 spring.activemq.in-memory=true # 是否在回滾回滾訊息之前停止訊息傳遞。這意味著當啟用此命令時,訊息順序不會被保留。 spring.activemq.non-blocking-redelivery=false # 等待訊息傳送響應的時間。設定為0等待永遠。 spring.activemq.send-timeout=0 #預設情況下activemq提供的是queue模式,若要使用topic模式需要配置下面配置 spring.jms.pub-sub-domain=true #賬號 spring.activemq.user=admin # 密碼 spring.activemq.password=admin # 是否信任所有包 #spring.activemq.packages.trust-all= # 要信任的特定包的逗號分隔列表(當不信任所有包時) #spring.activemq.packages.trusted= # 當連線請求和池滿時是否阻塞。設定false會拋“JMSException異常”。 #spring.activemq.pool.block-if-full=true # 如果池仍然滿,則在丟擲異常前阻塞時間。 #spring.activemq.pool.block-if-full-timeout=-1ms # 是否在啟動時建立連線。可以在啟動時用於加熱池。 #spring.activemq.pool.create-connection-on-startup=true # 是否用Pooledconnectionfactory代替普通的ConnectionFactory。 #spring.activemq.pool.enabled=false # 連線過期超時。 #spring.activemq.pool.expiry-timeout=0ms # 連線空閒超時 #spring.activemq.pool.idle-timeout=30s # 連線池最大連線數 #spring.activemq.pool.max-connections=1 # 每個連線的有效會話的最大數目。 #spring.activemq.pool.maximum-active-session-per-connection=500 # 當有"JMSException"時嘗試重新連線 #spring.activemq.pool.reconnect-on-exception=true # 在空閒連線清除執行緒之間執行的時間。當為負數時,沒有空閒連線驅逐執行緒執行。 #spring.activemq.pool.time-between-expiration-check=-1ms # 是否只使用一個MessageProducer #spring.activemq.pool.use-anonymous-producers=true
4.新增JMS相關配置
①.開啟JMS掃描註解:@EnableJms 相當於application.xml中的<jms:annotation-d riven/> ②.配置Queue類: @Bean public Queue queue() { return new ActiveMQQueue("queueName1"); } ③.建立生產者: @Resource JmsMessagingTemplate jmsMessagingTemplate; public void sendMessage(Destination destination, String message) { jmsMessagingTemplate.convertAndSend(destination, message); } ④.建立消費者: @JmsListener(destination = "queueName1") public void receiveQueue(String message) { log.info("=========接受到了訊息:" + message); grabService.addSearchContent(message, mainConfig.getCharset()); }
ps:@JmsListener(destination = "queueName1")註解用於監聽指定名稱的訊息
引數message代表具體的訊息