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

spring-boot集成activemq

2017-12-13 09:46 381 查看
一、注入依赖

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

二、定义消息队列
import javax.jms.Queue;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QueneConfig {

public static final Queue mq=new ActiveMQQueue("sample.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;

@Component
@EnableScheduling
public class MQProducer {

@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private QueneConfig qc;

@Scheduled(fixedDelay=3000)//每3s执行1次
public void send() {
this.jmsMessagingTemplate.convertAndSend(qc.mq, "hi,activeMQ");
}

}

四、定义消费
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer {

@JmsListener(destination = "sample.queue")
public void receiveQueue(String text) {
System.out.println(text);
}

}

五、书写配置文件application.properties
spring.activemq.broker-url=tcp://localhost:61616 # 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.

集成完毕!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: