Name:队列名称
Nubmer of Pending Messages:等待消费的消息个数
Nubmer Of Consumers :当前连接的消费者数目
Messages Enqueued: 进入队列的消息总个数,包含出队列和待消费的,这个数量只增不减。
Messages Dequeued:已经消费的消息数量。
./activemq start
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-activemqartifactId>
dependency>
<dependency>
<groupId>org.apache.activemqgroupId>
<artifactId>activemq-poolartifactId>
<version>5.15.0version>
dependency>
#整合jms测试,防火墙和端口号要开放
spring.activemq.broker-url=tcp://127.0.0.1:61616
#集群配置
#spring.activemq.broker-url=failover:(tcp://localhost:61616,tcp://localhost:61617)
spring.activemq.user=admin
spring.activemq.password=admin
#下列配置要添加依赖
spring.activemq.pool.enable=true
spring.activemq.pool.max-connections=100
@SpringBootApplication
@EnableJms //启动消息队列
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
@Bean
public Queue queue(){
return new ActiveMQQueue("common.queue");
}
功能描述:
指定消息队列和消息。
使用默认消息队列,发送消息。
public interface ProducerService {
/**
* 功能描述:指定消息队列,还有消息
* @param destination
* @param message
*/
public void sendMessage(Destination destination, final String message);
/**
* 功能描述:使用默认消息队列, 发送消息
* @param message
*/
public void sendMessage( final String message);
/**
* 功能描述:消息发布者
* @param msg
*/
public void publish(String msg);
}
@Service
public class ProducerServiceImpl implements ProducerService{
@Autowired
private Queue queue;
//这个就是springboot集成的发送消息队列的各种模板。
@Autowired
private JmsMessagingTemplate jmsTemplate; //用来发送消息到broker的对象
//发送消息,destination是发送到的队列,message是待发送的消息
@Override
public void sendMessage(Destination destination, String message) {
jmsTemplate.convertAndSend(destination, message);
}
//发送消息,destination是发送到的队列,message是待发送的消息
@Override
public void sendMessage(final String message) {
jmsTemplate.convertAndSend( message);
}
//=======发布订阅相关代码=========
@Autowired
private Topic topic;
@Override
public void publish(String msg) {
this.jmsTemplate.convertAndSend(this.topic, msg);
}
}
@GetMapping("order")
public Object order(String msg){
//生成消息队列地址,new创建一个队列,然后将该队列给父类,也就是多态Destination,destination包含该队列的所有属性。
Destination destination = new ActiveMQQueue("order.queue");
producerService.sendMessage(destination, msg);
return JsonData.buildSuccess();
}
@Component//这个用于spring扫描和application一起启动
public class OrderConsumer {
@JmsListener(destination="order.queue")//JmsListener监听消息队列,一旦有消息就会消费。
public void receiveQueue(String text){
System.out.println("OrderConsumer收到的报文为:"+text);
}
}
spring.jms.pub-sub-domain=true
@Bean
public Topic topic(){//创建一个订阅队列,video.topic
return new ActiveMQTopic("video.topic");
}
public void publish(Strin msg);
@Autowired
private Topic topic;//这个说明已经交给Spring来管理了,有这个的前提表明需要提前将队列交给Spring
public void publish(String msg){
this.jmsTemplate.convertAndSend(this.topic,msg);
}
@Bean
public Topic topic(){
return new ActiveMQTopic("video.topic");
}
@GetMapping("topic")
public Object topic(String msg){
producerService.publish(msg);
return JsonData.buildSuccess();
}
@Component
public class TopicConsumer{
@JmsListener(destination="video.topic")
public void receive(String text){
System.out.println("video.topic消费者receivel="+text);
}
@JmsListener(destination="video.topic")
public void receive1(String text){
System.out.println("video.topic消费者receivel="+text);
}
@JmsListener(destination="video.topic")
public void receive2(String text){
System.out.println("video.topic消费者receivel="+text);
}
}
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setPubSubDomain(true);
bean.setConnectionFactory(activeMQConnectionFactory);
return bean;
}
@JmsListener(destination="video.topic", containerFactory="jmsListenerContainerTopic")
public void receive1(String text){
System.out.println("video.topic 消费者:receive1="+text);
}