整合ActiveMQ
基础
- 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.messaginghub</groupId>
<artifactId>pooled-jms</artifactId>
</dependency>
- 基础配置
#ActiveMQ
##基本配置
spring.activemq.broker-url=tcp://192.168.1.122:61616
spring.activemq.user=admin
spring.activemq.password=admin
## 集群配置
#spring.activemq.broker-url=failover:(tcp://192.168.1.122:61616,tcp://192.168.1.122:61617)
## 线程池配置
spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=50
生产者
- 配置默认队列
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.jms.Queue;
/**
* @title 配置默认队列
* @Desc 描述
* @author <a href="mailto:avaos.wei@gmail.com">avaos.wei</a>
* @Date 2020-03-27 16:11
*
*/
@Configuration
public class ActiveMQConfig {
public static final String DEFAULT_QUEUE = "avaos.queue";
@Bean
public Queue queue(){
return new ActiveMQQueue(DEFAULT_QUEUE);
}
}
- 生产服务
import javax.jms.Destination;
/**
* @title 消息生产者
* @Desc 描述
* @author <a href="mailto:avaos.wei@gmail.com">avaos.wei</a>
* @Date 2020-03-27 02:42
*
*/
public interface ProducerService {
/**
* @title 发送消息
* @desc 指定消息队列发送消息
* @param destination:
* @param message:
* @return
* @author <a href="mailto:avaos.wei@gmail.com">avaos.wei</a>
* @date 2020-03-27 02:44
*/
void send(Destination destination, final String message);
/**
* @title 发送消息
* @desc 使用默认队列发送消息
* @param message:
* @return
* @author <a href="mailto:avaos.wei@gmail.com">avaos.wei</a>
* @date 2020-03-27 02:45
*/
void send(final String message);
}
import cn.avaos.service.ProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.jms.Destination;
import javax.jms.Queue;
@Service
public class ProducerServiceImpl implements ProducerService {
@Autowired
private JmsMessagingTemplate jmsTemplate;
@Autowired
private Queue queue;
@Override
public void send(Destination destination, String message) {
jmsTemplate.convertAndSend(destination, message);
}
@Override
public void send(String message) {
jmsTemplate.convertAndSend(queue, message);
}
}
- 对外接口API
import cn.avaos.service.ProducerService;
import cn.avaos.web.form.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.jms.Destination;
/**
* @title 模拟下订单
* @Desc 描述
* @author <a href="mailto:avaos.wei@gmail.com">avaos.wei</a>
* @Date 2020-03-27 03:04
*
*/
@Slf4j
@RestController
@RequestMapping("/api/order")
public class OrderController {
@Autowired
private ProducerService producerService;
@GetMapping
public Object order(String msg) {
Destination destination = new ActiveMQQueue("order.queue");
producerService.send(destination, msg);
log.info("producer: 发送消息-{}", msg);
return JsonData.ok();
}
@GetMapping(path = "/common")
public Object common(String msg){
producerService.send(msg);
log.info("producer: 发送消息-{}", msg);
return JsonData.ok();
}
}
消费者
- 消费订单队列的消息
import lombok.extern.slf4j.Slf4j;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/**
* @title 消费order队列中的消息
* @Desc 描述
* @author <a href="mailto:avaos.wei@gmail.com">avaos.wei</a>
* @Date 2020-03-27 16:15
*
*/
@Slf4j
@Component
public class OrderConsumer {
@JmsListener(destination = "order.queue")
public void receiveQueue(String text) {
log.info("consumer: 接收的消息-{}", text);
}
}
- 消费默认队列的消息
import lombok.extern.slf4j.Slf4j;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/**
* @title 消费默认队列中的消息
* @Desc 描述
* @author <a href="mailto:avaos.wei@gmail.com">avaos.wei</a>
* @Date 2020-03-27 16:16
*
*/
@Slf4j
@Component
public class CommonConsumer {
@JmsListener(destination = "avaos.queue")
public void receiveQueue(String text) {
log.info("consumer: 接收的消息-{}", text);
}
}