日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

使用Spring Boot和注释支持配置Spring JMS应用程序

發布時間:2023/12/3 javascript 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Spring Boot和注释支持配置Spring JMS应用程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.簡介

在以前的文章中,我們學習了如何使用Spring JMS配置項目。 如果查看有關使用Spring JMS進行消息傳遞的文章介紹 ,您會注意到它是使用XML配置的。 本文將利用Spring 4.1版本中引入的改進 ,并僅使用Java config來配置JMS項目。

在這個示例中,我們還將看到使用Spring Boot配置項目是多么容易。

在開始之前,請注意,與往常一樣,您可以看一下下面示例中使用的項目的源代碼。

請參閱github上的示例項目 。

欄目:

  • 介紹。
  • 示例應用程序。
  • 設置項目。
  • 一個使用JMS偵聽器的簡單示例。
  • 使用@SendTo將響應發送到另一個隊列。
  • 結論。
  • 2.示例應用程序

    該應用程序使用客戶端服務將訂單發送到JMS隊列,在該隊列中將注冊JMS偵聽器并處理這些訂單。 收到后,偵聽器將通過Store服務存儲訂單:

    我們將使用Order類來創建訂單:

    public class Order implements Serializable {private static final long serialVersionUID = -797586847427389162L;private final String id;public Order(String id) {this.id = id;}public String getId() {return id;} }

    在繼續第一個示例之前,我們將首先探討如何構建項目結構。

    3.設置項目

    3.1配置pom.xml

    首先要做的是將工件spring-boot-starter-parent定義為我們的父pom。

    <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.2.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --> </parent>

    這個父級基本上設置了幾個Maven默認值,并為我們將使用的主要依賴項提供了依賴項管理,例如Spring版本(4.1.6)。

    重要的是要注意,此父pom定義了許多庫的版本,但未對我們的項目添加任何依賴關系。 因此,不必擔心會得到不使用的庫。

    下一步是設置Spring Boot的基本依賴關系:

    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId> </dependency>

    除了核心Spring庫之外,此依賴項還將帶來Spring Boot的自動配置功能。 這將允許框架嘗試根據您添加的依賴項自動設置配置。

    最后,我們將添加Spring JMS依賴項和ActiveMQ消息代理,將整個pom.xml保留如下:

    <groupId>xpadro.spring</groupId> <artifactId>jms-boot-javaconfig</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>JMS Spring Boot Javaconfig</name><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.2.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --> </parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><start-class>xpadro.spring.jms.JmsJavaconfigApplication</start-class><java.version>1.8</java.version><amq.version>5.4.2</amq.version> </properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-core</artifactId><version>${amq.version}</version></dependency> </dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins> </build>

    3.2使用Java Config進行Spring配置

    配置類僅需要配置嵌入式消息代理。 其余的由Spring Boot自動配置:

    @SpringBootApplication public class JmsJavaconfigApplication {private static final String JMS_BROKER_URL = "vm://embedded?broker.persistent=false,useShutdownHook=false";@Beanpublic ConnectionFactory connectionFactory() {return new ActiveMQConnectionFactory(JMS_BROKER_URL);}public static void main(String[] args) {SpringApplication.run(JmsJavaconfigApplication.class, args);} }

    我們使用@SpringBootApplication代替了通常的@Configuration批注。 這個Spring Boot注釋也用@Configuration注釋。 此外,它還設置其他配置,例如Spring Boot自動配置:

    @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Configuration @EnableAutoConfiguration @ComponentScan public @interface SpringBootApplication {

    現在都設置好了。 在下一部分的示例中,我們將了解如何配置JMS偵聽器,因為它已配置了注釋。

    4.使用JMS偵聽器的簡單示例

    4.1將訂單發送到JMS隊列

    ClientService類負責將新訂單發送到JMS隊列。 為了做到這一點,它使用一個JmsTemplate:

    @Service public class ClientServiceImpl implements ClientService {private static final String SIMPLE_QUEUE = "simple.queue";private final JmsTemplate jmsTemplate;@Autowiredpublic ClientServiceImpl(JmsTemplate jmsTemplate) {this.jmsTemplate = jmsTemplate;}@Overridepublic void addOrder(Order order) {jmsTemplate.convertAndSend(SIMPLE_QUEUE, order);} }

    在這里,我們使用JmsTemplate轉換Order實例并將其發送到JMS隊列。 如果您希望直接通過發送消息發送消息,則可以使用新的JmsMessagingTemplate 。 這是更好的選擇,因為它使用了更加標準化的Message類。

    4.2接收發送到JMS隊列的訂單

    將JMS偵聽器注冊到JMS偵聽器容器就像將@JmsListener批注添加到我們要使用的方法一樣簡單。 這將在幕后創建一個JMS偵聽器容器,該容器將接收發送到指定隊列的消息并將它們委派給我們的偵聽器類:

    @Component public class SimpleListener {private final StoreService storeService;@Autowiredpublic SimpleListener(StoreService storeService) {this.storeService = storeService;}@JmsListener(destination = "simple.queue")public void receiveOrder(Order order) {storeService.registerOrder(order);} }

    StoreService接收訂單并將其保存到已接收訂單的列表中:

    @Service public class StoreServiceImpl implements StoreService {private final List<Order> receivedOrders = new ArrayList<>();@Overridepublic void registerOrder(Order order) {this.receivedOrders.add(order);}@Overridepublic Optional<Order> getReceivedOrder(String id) {return receivedOrders.stream().filter(o -> o.getId().equals(id)).findFirst();} }

    4.3測試應用程序

    現在讓我們添加一個測試來檢查我們是否正確完成了所有操作:

    @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = JmsJavaconfigApplication.class) public class SimpleListenerTest {@Autowiredprivate ClientService clientService;@Autowiredprivate StoreService storeService;@Testpublic void sendSimpleMessage() {clientService.addOrder(new Order("order1"));Optional<Order> storedOrder = storeService.getReceivedOrder("order1");Assert.assertTrue(storedOrder.isPresent());Assert.assertEquals("order1", storedOrder.get().getId());} }

    5.使用@SendTo將響應發送到另一個隊列

    Spring JMS的另一個附加功能是@SendTo批注。 此批注允許偵聽器將消息發送到另一個隊列。 例如,以下偵聽器從“ in.queue”接收命令,并在存儲該命令后向“ out.queue”發送確認。

    @JmsListener(destination = "in.queue") @SendTo("out.queue") public String receiveOrder(Order order) {storeService.registerOrder(order);return order.getId(); }

    在那里,我們注冊了另一個偵聽器,它將處理此確認ID:

    @JmsListener(destination = "out.queue") public void receiveOrder(String orderId) {registerService.registerOrderId(orderId); }

    六,結論

    有了注釋支持,現在可以更輕松地配置Spring JMS應用程序,從而利用使用注釋JMS偵聽器進行異步消息檢索的優勢。

    翻譯自: https://www.javacodegeeks.com/2015/04/configure-a-spring-jms-application-with-spring-boot-and-annotation-support.html

    總結

    以上是生活随笔為你收集整理的使用Spring Boot和注释支持配置Spring JMS应用程序的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。