您的位置:首页 > 编程语言 > Java开发

Spring Boot 2.0.0.M7 使用异步消息服务-JMS(ActiveMQ)

2018-01-12 07:41 489 查看


使用异步消息服务-JMS(ActiveMQ)

Spring Boot支持的jms有:ActiveMQ、Artemis、HornetQ


添加依赖

<!-- 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队列配置
*
*/
@Configuration
publicclass JmsConfiguration {

@Bean
publicQueue queue() {
returnnew ActiveMQQueue("ctoedu.queue");
}

}


@Component
publicclass JmsComponent {

@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;

@Autowired
private Queue queue;

publicvoid send(String msg) {
this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
}

@JmsListener(destination = "ctoedu.queue")
publicvoid receiveQueue(String text) {
System.out.println("接受到:" + text);
}

}



测试

@Autowired
private JmsComponent jmsComponent;

@Test
public void send() {
jmsComponent.send("hello world");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: