ActiveMQ点对点及topic使用

本文介绍如何使用Spring Boot和ActiveMQ实现点对点(P2P)和发布订阅(主题)模式的消息传递。通过具体代码示例展示了P2P模式下每个生产者对应一个消费者的场景,以及主题模式下单一生产者向多个消费者发送消息的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、首先介绍点对点,就是一个Producter对应一个Consumer

Producter1→Consumer1

Producter2→Consumer2

pom.xml

<!--
       spring boot 父节点依赖,
       引入这个之后相关的引入就不需要添加version配置,
       spring boot会自动选择最合适的版本进行添加。
     -->
    <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>1.4.0.RELEASE</version>
    </parent> 
   
  <dependencies>
   <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
   
    <!-- spring boot web支持:mvc,aop... -->
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
   
    <!-- activemq support -->
    <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>
   
  </dependencies>

App.java

package cn.com.zjq;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.core.JmsMessagingTemplate;

@SpringBootApplication
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
	
	@Bean  
    public ActiveMQConnectionFactory connectionFactory() {  
		//此链接信息可放入配置文件中
        return new ActiveMQConnectionFactory("admin", "admin", "tcp://localhost:61616"); 
    }
	
	@Bean
	public JmsMessagingTemplate jmsMessagingTemplate(ActiveMQConnectionFactory connectionFactory){
		System.out.println("get JmsMessagingTemplate");
		return new JmsMessagingTemplate(connectionFactory);
	}
}

Producter1.java

package cn.com.zjq.activemq;

import javax.jms.Queue;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

@Component
public class Producter1 {
	@Autowired
	private JmsMessagingTemplate jmsMessagingTemplate;
	
	public Queue queue(){
		return new ActiveMQQueue("queue1");
	}
	
	public void send(String message){
		jmsMessagingTemplate.convertAndSend(queue(), message);
	}
}
Producter2.java
package cn.com.zjq.activemq;

import javax.jms.Queue;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

@Component
public class Producter2 {
	@Autowired
	private JmsMessagingTemplate jmsMessagingTemplate;
	
	public Queue queue(){
		return new ActiveMQQueue("queue2");
	}
	
	public void send(String message){
		jmsMessagingTemplate.convertAndSend(queue(), message);
	}
}
Consumer1.java
package cn.com.zjq.activemq;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer1 {

	@JmsListener(destination="queue1")
	public void receiveQueue(String message){
		System.out.println("Consumer1" + message);
	}
}
Consumer2.java
package cn.com.zjq.activemq;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer2 {

	@JmsListener(destination="queue2")
	public void receiveQueue(String message){
		System.out.println("Consumer2" + message);
	}
}

测试

DemoController.java

package cn.com.zjq.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.com.zjq.activemq.Producter1;
import cn.com.zjq.activemq.Producter2;
import cn.com.zjq.topic.Topic1;

@RestController
public class DemoController {
	
	@Autowired
	private Producter1 producter1;
	@Autowired
	private Producter2 producter2;
	
	@RequestMapping("test")
	public String test(){
		producter1.send("消息1");
		producter2.send("消息2");
		return "success";
	}
	
}


http://localhost:8101/test

结果

Consumer1消息1
Consumer2消息2


二、topic的使用,一个发送消息,多个接收

Topic1→Cm1、Topic1→Cm2、Topic1→Cm3

Topic1.java

package cn.com.zjq.topic;

import javax.jms.Topic;

import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

@Component
public class Topic1 {
	
	@Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
	
	public Topic topic(){
		return new ActiveMQTopic("topic1");
	}
	
	public void send(String message){
		jmsMessagingTemplate.convertAndSend(topic(), message);
	}
}
Cm1.java

package cn.com.zjq.topic;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class Cm1 {
	@JmsListener(destination = "topic1")
    public void receiveQueue(String text) {
       System.out.println("Consumer1="+text);
    }
}
Cm2.java

package cn.com.zjq.topic;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class Cm2 {
	@JmsListener(destination = "topic1")
    public void receiveQueue(String text) {
       System.out.println("Consumer2="+text);
    }
}
Cm3.java
package cn.com.zjq.topic;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class Cm3 {
	@JmsListener(destination = "topic1")
    public void receiveQueue(String text) {
       System.out.println("Consumer3="+text);
    }
}

配置文件必须加上,不然不能生效

spring.jms.pub-sub-domain=true

测试

DemoController.java

package cn.com.zjq.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.com.zjq.activemq.Producter1;
import cn.com.zjq.activemq.Producter2;
import cn.com.zjq.topic.Topic1;

@RestController
public class DemoController {
	
	@Autowired
	private Topic1 topic1;
	
	
	@RequestMapping("topic")
	public String topic(){
		topic1.send("topic1");
		return "success";
	}
}


http://localhost:8101/topic
结果
Consumer3=topic1
Consumer1=topic1
Consumer2=topic1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值