Spring Boot基礎教程21-使用異步消息服務-JMS(ActiveMQ)

Spring Boot支持的jms有ActiveMQArtemisHornetQ

 

  • 添加依賴

<!-- jms -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-activemq</artifactId>

</dependency>

  • 配置文件

# ACTIVEMQ (ActiveMQProperties)

spring.activemq.in-memory=true

#spring.activemq.broker-url=

#spring.activemq.password=

#spring.activemq.user=

#spring.activemq.packages.trust-all=false

#spring.activemq.packages.trusted=

#spring.activemq.pool.configuration.*=

#spring.activemq.pool.enabled=false

#spring.activemq.pool.expiry-timeout=0

#spring.activemq.pool.idle-timeout=30000

#spring.activemq.pool.max-connections=1

  • 代碼實現

啓動註解

@EnableJms 添加在main方法裏面

 

配置隊列

/**

 * jms 隊列配置

 *

 * @author wujing

 */

@Configuration

public class JmsConfiguration {

 

@Bean

public Queue queue() {

return new ActiveMQQueue("roncoo.queue");

}

 

}

 

代碼

/**

 *

 * @author wujing

 */

@Component

public class RoncooJmsComponent {

 

@Autowired

private JmsMessagingTemplate jmsMessagingTemplate;

 

@Autowired

private Queue queue;

 

public void send(String msg) {

this.jmsMessagingTemplate.convertAndSend(this.queue, msg);

}

 

@JmsListener(destination = "roncoo.queue")

public void receiveQueue(String text) {

System.out.println("接受到:" + text);

}

 

}

  • 測試

@Autowired

private RoncooJmsComponent roncooJmsComponent;

 

@Test

public void send() {

roncooJmsComponent.send("hello world");

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章