https://activemq.apache.org/activemq-5016003-release
下载后解压即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
spring:
activemq:
broker-url: tcp://localhost:61616
jms:
template:
default-destination: itheima
@Service
public class MessageServiceActivemqImpl implements MessageService {
@Autowired
private JmsMessagingTemplate messagingTemplate;
@Override
public void sendMessage(String id) {
System.out.println("待发送短信的订单已经纳入队列:id=" + id);
messagingTemplate.convertAndSend("order.queue.id",id);
// 发消息的时候设定存储位置
}
@Override
public String doMessage() {
String id = messagingTemplate.receiveAndConvert("order.queue.id",String.class);
System.out.println("已完成短信发送业务,id:"+id);
return id;
}
}
消息怎么会手工点击消费呢,哈哈哈,肯定是收到消息赶紧处理,必须交给机器处理啦!
@Component
public class MessageListener {
@JmsListener(destination = "order.queue.id")
public void reveive(String id){
System.out.println("已完成短信发送业务,id:"+id);
}
}
监听器收到消息后,不仅可以消费消息,还可以将当前方法的返回值,在转发给下一个消息队列。
所以改造一下我们的监听器
@Component
public class MessageListener {
@JmsListener(destination = "order.queue.id")
@SendTo("order.other.queue.id")
public String reveive(String id){
System.out.println("已完成短信发送业务,id:"+id);
return "new:" + id;
}
}
jms的消息模型有两种
server:
port: 80
spring:
activemq:
broker-url: tcp://localhost:61616
jms:
template:
default-destination: itheima
pub-sub-domain: true