一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達式|

服務(wù)器之家 - 編程語言 - JAVA教程 - springboot集成activemq的實例代碼

springboot集成activemq的實例代碼

2020-09-24 15:23zl18310999566 JAVA教程

本篇文章主要介紹了springboot集成activemq的實例代碼,詳細的介紹了ActiveMQ和Spring-Boot 集成 ActiveMQ,有興趣的可以了解下。

ActiveMQ

ActiveMQ 是Apache出品,最流行的,能力強勁的開源消息總線。ActiveMQ 是一個完全支持JMS1.1和J2EE 1.4規(guī)范的 JMS Provider實現(xiàn),盡管JMS規(guī)范出臺已經(jīng)是很久的事情了,但是JMS在當今的J2EE應(yīng)用中間仍然扮演著特殊的地位。

特性

  1. 多種語言和協(xié)議編寫客戶端。語言: Java,C,C++,C#,Ruby,Perl,Python,PHP。應(yīng)用協(xié)議: OpenWire,Stomp REST,WS Notification,XMPP,AMQP
  2. 完全支持JMS1.1和J2EE 1.4規(guī)范 (持久化,XA消息,事務(wù))
  3. 對Spring的支持,ActiveMQ可以很容易內(nèi)嵌到使用Spring的系統(tǒng)里面去,而且也支持Spring2.0的特性
  4. 通過了常見J2EE服務(wù)器(如 Geronimo,JBoss 4,GlassFish,WebLogic)的測試,其中通過JCA 1.5 resource adaptors的配置,可以讓ActiveMQ可以自動的部署到任何兼容J2EE 1.4 商業(yè)服務(wù)器上
  5. 支持多種傳送協(xié)議:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
  6. 支持通過JDBC和journal提供高速的消息持久化
  7. 從設(shè)計上保證了高性能的集群,客戶端-服務(wù)器,點對點
  8. 支持Ajax
  9. 支持與Axis的整合
  10. 可以很容易的調(diào)用內(nèi)嵌JMS provider,進行測試

更多關(guān)于 ActiveMQ 的內(nèi)容可以點擊這里

Spring-Boot 集成 ActiveMQ

添加maven依賴

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!--
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>
    -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jms</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-client</artifactId>
    </dependency>

沒有直接使用注釋的依賴,是因為其含有如下依賴

?
1
2
3
4
<dependency>
  <groupId>org.apache.activemq</groupId>
  <artifactId>activemq-broker</artifactId>
</dependency>

而它的作用是什么呢,會在程序中直接內(nèi)嵌 ActivityMQ,也就是說不需要安裝 ActiveMQ,但是這個如果服務(wù)宕機了,內(nèi)嵌的 ActiveMQ 也就沒了。關(guān)鍵,這個內(nèi)嵌的 ActiveMQ 而無法看到圖形化界面,所以這里沒有直接使用注釋里的依賴。

在application.properties中增加如下配置

?
1
2
3
4
5
6
# activemq
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false

這里對 ActiveMQ 的端口做一個簡短說明,61616為消息代理接口 ,8161 為管理界面

JAVA代碼實現(xiàn)

定義QUEUE

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.activemq.queue;
 
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import javax.jms.Queue;
 
@Configuration
public class QueueConfig {
 
  @Bean
  public Queue logQueue() {
    return new ActiveMQQueue(QueueName.LOG_QUEUE);
  }
}

消息生產(chǎn)者

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.activemq.producer;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
 
import javax.jms.Queue;
 
@Component
public class LogProducer implements CommandLineRunner {
 
  private static final Logger LOGGER = LoggerFactory.getLogger(LogProducer.class);
 
  @Autowired
  private JmsMessagingTemplate jmsMessagingTemplate;
 
  @Autowired
  private Queue logQueue;
 
  @Override
  public void run(String... strings) throws Exception {
    send("This is a log message.");
    LOGGER.info("Log Message was sent to the Queue named sample.log");
  }
 
  public void send(String msg) {
    this.jmsMessagingTemplate.convertAndSend(this.logQueue, msg);
  }
}

消息消費者

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.activemq.consumer;
 
import com.activemq.queue.QueueName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
 
@Component
public class LogConsumer {
 
  private static final Logger LOGGER = LoggerFactory.getLogger(LogConsumer.class);
 
  @JmsListener(destination = QueueName.LOG_QUEUE)
  public void receivedQueue(String msg) {
    LOGGER.info("Has received from " + QueueName.LOG_QUEUE + ", msg: " + msg);
  }
}

測試接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Autowired
private LogProducer logProducer;
 
@GetMapping("/activemq/send")
public String activemq(HttpServletRequest request, String msg) {
  msg = StringUtils.isEmpty(msg) ? "This is Empty Msg." : msg;
 
  try {
    logProducer.send(msg);
  } catch (Exception e) {
    e.printStackTrace();
  }
  return "Activemq has sent OK.";
}

啟動類

增加如下注解@EnableJms

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Configuration//配置控制
@EnableAutoConfiguration//啟用自動配置
@ComponentScan//組件掃描
@EnableConfigurationProperties({EmailProp.class})
@EnableJms
public class Bootstrap {
 
  private static final Logger LOGGER = LoggerFactory
      .getLogger(Bootstrap.class);
 
  public static void main(String[] args) throws Exception {
    SpringApplication.run(Bootstrap.class, args);
    LOGGER.info("Server running...");
  }
 
}

測試

運行服務(wù),在瀏覽器輸入 http://127.0.0.1:8080/activemq/send?msg=test%20log,會在控制臺看到如下輸出

?
1
2
INFO 1498 --- [enerContainer-1] c.j.a.activemq.consumer.LogConsumer   : Has received from sample.log, msg: test log
[DefaultMessageListenerContainer-1] INFO c.j.a.activemq.consumer.LogConsumer - Has received from sample.log, msg: test log

打開 ActiveMQ 的管理頁面,用戶名密碼都是admin,可以看到如下信息

springboot集成activemq的實例代碼

官方示例:https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-activemq

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/zl18310999566/article/details/54313464

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本hdxxxx护士 | 四虎影音 | 精品国产一区二区在线观看 | 久久久久青草大香线综合精品 | 欧美日韩中文国产一区二区三区 | 国产成人综合久久精品红 | 久久精品在现线观看免费15 | 娇妻被健身教练挺进小说阅读 | 好男人资源免费播放在线观看 | 日韩经典在线观看 | 明星h文集合短篇小说 | 国产精品日本亚洲777 | 日韩妹妹 | 大乳一级一区二区三区 | 污丝瓜视频 | 青草免费在线 | 好男人社区www影院在线观看 | 国产精品视频第一区二区三区 | 亚洲日韩男人网在线 | 思思久久精品在热线热 | www.av免费| 特级一级全黄毛片免费 | 校园全肉高h湿一女多男 | 日韩色在线观看 | 99久久国产综合精品麻豆 | 毛片一级毛片 | 亚洲成人免费 | 国产一成人精品福利网站 | 日韩影院在线观看 | 成人免费公开视频 | 99精品免费视频 | 羞羞私人影院可以直接免费观影吗 | 9久re在线观看视频精品 | 女人和男人搞鸡 | 国产色站| 99精品热线在线观看免费视频 | 天堂精品高清1区2区3区 | 成人高清网站 | 美女跪式抽搐gif动态图 | gaychinese男男2022| 国产成人综合手机在线播放 |