一、项目结构
二、所需jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
三、启动类
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import javax.jms.Queue;
import javax.jms.Topic;
@SpringBootApplication
public class DemoApplication {
@Bean
public Queue queue() {
return new ActiveMQQueue("sample.queue");
}
@Bean
public Topic topic() {
return new ActiveMQTopic("sample.topic");
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
四、创建生产者
这里设置了每三秒执行一次
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.jms.Queue;
import javax.jms.Topic;
@Component
@EnableScheduling
public class Producer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
@Autowired
private Topic topic;
@Scheduled(fixedDelay = 3000)//每3s执行1次
public void send() {
this.jmsMessagingTemplate.convertAndSend(this.queue, "hi,activeMQ(queue)");
this.jmsMessagingTemplate.convertAndSend(this.topic, "hi,activeMQ(topic)");
}
}
五、创建消费者,这里我们创建两个
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class Consumer3 {
@JmsListener(destination = "sample.topic")
public void receiveQueue(String text) {
System.out.println("Consumer3=" + text);
}
}
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class Consumer2 {
@JmsListener(destination = "sample.queue")
public void receiveQueue(String text) {
System.out.println("Consumer2=" + text);
}
}
六、配置文件(这里我采用的yml文件)
server:
port: 8080
spring:
messages:
basename: i18n/Messages,i18n/Pages
jms:
pub-sub-domain: false # 配置消息的类型,如果是true则表示为topic消息,如果为false表示Queue消息
activemq:
user: admin # 连接用户名
password: admin # 连接密码
broker-url: tcp://localhost:61616 # 消息组件的连接主机信息
send-timeout: 0
七、调试结果
配置文件 pub-sub-domain: false 时
配置文件 pub-sub-domain: true 时
我们也可以不用定时器,用controller来实现
一、项目结构
二、controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.jms.Queue;
import javax.jms.Topic;
@Controller
public class controller {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
@Autowired
private Topic topic;
@RequestMapping("send1")
public void queue(String msg) {
System.out.println(msg);
//pub-sub-domain: false
jmsMessagingTemplate.convertAndSend(this.queue, msg);
}
@RequestMapping("send2")
public void topic(String msg) {
System.out.println(msg);
//pub-sub-domain: true
jmsMessagingTemplate.convertAndSend(this.topic, msg);
}
}