TOPIC and QUEUE of message middleware

TOPIC and QUEUE are two different message mechanisms, commonly used in message middleware and distributed systems.

1. TOPIC
- In TOPIC, the message publisher sends the message to the Topic (topic) and specifies a topic name.
- Subscribers can receive all incoming messages by subscribing to the topic.
- The message is broadcast among subscribers, that is, all subscribers who subscribe to the topic will receive the same message.

2. QUEUE
- In QUEUE, message publishers send messages to Queue (queue), and subscribers get and process these messages from the queue.
- The queue arranges the messages in the order of FIFO (First in, First Out), that is, the messages that enter the queue first are processed first, and the messages that enter the queue later will wait for the previous messages to be processed before continuing to process.
- Each message received by a consumer from a queue can only be processed by one consumer.

Therefore, topics are suitable for publish/subscribe mode, that is, a message needs to be delivered to multiple consumers, and queues are suitable for point-to-point messaging mode, that is, a message can only be processed by one consumer.

public class ActiveMQController {
	
	@Autowired
	Producer producer;
	
	@Autowired
	@Qualifier("topic")
	Topic topic;
	
	@Autowired
	@Qualifier("queue")
	Queue queue;
	
	@RequestMapping("demo")
	public String demo(){
		return "demo";
	}
	
	@RequestMapping(value="/produce",produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public void produce(@ModelAttribute("mail") Mail mail) throws Exception{
		producer.sendMail(queue, mail);
	}
	
	@RequestMapping(value="/topic",produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public void topic(@ModelAttribute("mail")Mail mail) throws Exception{
		producer.sendMail(topic, mail);
	}
	
}
@Service("producer")
public class ProducerImpl implements Producer{
	
	@Autowired
    public JmsMessagingTemplate jmsMessagingTemplate;
	
	@Override
	public void sendMail(Destination des, Mail mail) {
		jmsMessagingTemplate.convertAndSend(des, mail);
	}
	
}

Guess you like

Origin blog.csdn.net/qq_70143756/article/details/131006315