Spring Boot + ActiveMq

下载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
 */
@SpringBootApplication
public 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
@EnableScheduling
public 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
 */
@Component
public class Consumer {
    @JmsListener(destination = "sample.queue")
    public void receiveQueue(String text) {
       System.out.println(text);
    }
}

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

猜你喜欢

转载自blog.csdn.net/yycarry/article/details/78775970