Spring Boot + ActiveMq

来源:互联网 发布:深圳云计算招聘岗位 编辑:程序博客网 时间:2024/06/13 23:26

下载activeMq:http://activemq.apache.org/activemq-5152-release.html

启动activeMq:bin目录下activemq.bat直接启动

http://localhost:8161进入activemq首页
这里写图片描述

pom.xml引入activeMq插件

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

application.properties配置

# ACTIVEMQ (ActiveMQProperties)spring.activemq.broker-url= # URL of the ActiveMQ broker. Auto-generated by default. For instance `tcp://localhost:61616`spring.activemq.in-memory=true # Specify if the default broker URL should be in memory. Ignored if an explicit broker has been specified.spring.activemq.password= # Login password of the broker.spring.activemq.user= # Login user of the broker.spring.activemq.packages.trust-all=false # Trust all packages.spring.activemq.packages.trusted= # Comma-separated list of specific packages to trust (when not trusting all packages).spring.activemq.pool.configuration.*= # See PooledConnectionFactory.spring.activemq.pool.enabled=false # Whether a PooledConnectionFactory should be created instead of a regular ConnectionFactory.spring.activemq.pool.expiry-timeout=0 # Connection expiration timeout in milliseconds.spring.activemq.pool.idle-timeout=30000 # Connection idle timeout in milliseconds.spring.activemq.pool.max-connections=1 # Maximum number of pooled connections.

这里主要涉及到几个角色,消息生产者,消息队列,消息消费者。所以只需要把这个解决实现了,编码也就完成了。

消息队列Queue,这里编写在启动类App.java中,以@Bean的方式注入:

package com.kfit;import javax.jms.Queue;import org.apache.activemq.command.ActiveMQQueue;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;/** * * @author Angel --YYR * @version v.0.1 */@SpringBootApplicationpublic class App {    @Bean    public Queue queue() {       return new ActiveMQQueue("sample.queue");    }    public static void main(String[] args) {       SpringApplication.run(App.class, args);    }}

在这里注入了一个ActiveMQQueue。消息生产者com.kfit.demo.Producer:

package com.kfit.demo;import javax.jms.Queue;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsMessagingTemplate;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;/** * 消息生产者. * @author Angel --YYR * @version v.0.1 */@Component@EnableSchedulingpublic class Producer {    @Autowired    private JmsMessagingTemplate jmsMessagingTemplate;    @Autowired    private Queue queue;    @Scheduled(fixedDelay=3000)//每3s执行1次    public void send() {       this.jmsMessagingTemplate.convertAndSend(this.queue, "hi,activeMQ");    }}

这里使用JmsMessagingTemplate 进行消息的操作,然后使用任务调度3秒1次执行消息的发布。消息消费者com.kfit.demo.Consumer:

package com.kfit.demo;import org.springframework.jms.annotation.JmsListener;import org.springframework.stereotype.Component;/** * 消息消费者. * @author Angel YYR * @version v.0.1 */@Componentpublic class Consumer {    @JmsListener(destination = "sample.queue")    public void receiveQueue(String text) {       System.out.println(text);    }}

这里主要是加入了@JmsListener进行监听,然后接收消息然后打印。

原创粉丝点击