ActiveMQ点对点及topic使用

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

Producter1→Consumer1

Producter2→Consumer2

pom.xml


    
       org.springframework.boot
       spring-boot-starter-parent
       1.4.0.RELEASE
     
   
  
   
      junit
      junit
      test
    
   
    
    
       org.springframework.boot
       spring-boot-starter-web
    
   
    
    
           org.springframework.boot
           spring-boot-starter-activemq
    
   
  

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

你可能感兴趣的:(ActiveMQ,spring-boot)