您的位置:首页 > 编程语言 > Java开发

springboot整合activemq案例,queue,topic两种模式

2019-03-01 17:47 1641 查看
版权声明:未经作者同意禁止转载 https://blog.csdn.net/liubin4475/article/details/88063963

一、项目结构

二、所需jar包

[code] <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

三、启动类

[code]
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);
}

}

四、创建生产者

这里设置了每三秒执行一次

[code]
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.convert
4000
AndSend(this.queue, "hi,activeMQ(queue)");
this.jmsMessagingTemplate.convertAndSend(this.topic, "hi,activeMQ(topic)");

}
}

 

五、创建消费者,这里我们创建两个

[code]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);
}
}
[code]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文件)

[code]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

[code]
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);
}
}

三、调试结果

配置文件  pub-sub-domain: false 时

在浏览器输入  http://localhost:8080/send1?msg=123

在控制台可查看输出结果

源码下载:https://download.csdn.net/download/liubin4475/10988924

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: