溫馨提示×

溫馨提示×

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

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

使用springboot如何實現(xiàn)對activemq進(jìn)行集成

發(fā)布時間:2020-11-18 16:12:03 來源:億速云 閱讀:166 作者:Leah 欄目:編程語言

這篇文章給大家介紹使用springboot如何實現(xiàn)對activemq進(jìn)行集成,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

ActiveMQ

ActiveMQ 是Apache出品,最流行的,能力強(qiáng)勁的開源消息總線。ActiveMQ 是一個完全支持JMS1.1和J2EE 1.4規(guī)范的 JMS Provider實現(xiàn),盡管JMS規(guī)范出臺已經(jīng)是很久的事情了,但是JMS在當(dāng)今的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,進(jìn)行測試
     

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

Spring-Boot 集成 ActiveMQ

添加maven依賴

<!-- 
    <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> 

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

<dependency> 
  <groupId>org.apache.activemq</groupId> 
  <artifactId>activemq-broker</artifactId> 
</dependency> 

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

在application.properties中增加如下配置

# 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

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)者

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); 
  } 
} 

消息消費者

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); 
  } 
} 

測試接口

@Autowired 
private LogProducer logProducer; 
 
@GetMapping("/activemq/send") 
public String activemq(HttpServletRequest request, String msg) { 
  msg = StringUtils.isEmpty(msg) &#63; "This is Empty Msg." : msg; 
 
  try { 
    logProducer.send(msg); 
  } catch (Exception e) { 
    e.printStackTrace(); 
  } 
  return "Activemq has sent OK."; 
} 

啟動類

增加如下注解@EnableJms

@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&#63;msg=test%20log,會在控制臺看到如下輸出

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如何實現(xiàn)對activemq進(jìn)行集成

關(guān)于使用springboot如何實現(xiàn)對activemq進(jìn)行集成就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI