没有合适的资源?快使用搜索试试~ 我知道了~
SpringBoot整合ActiveMQ+websocket.docx
需积分: 50 14 下载量 136 浏览量
2019-08-05
22:41:03
上传
评论 3
收藏 31KB DOCX 举报
温馨提示


试读
24页
SpringBoot整合ActiveMQ+websocket.docx ,该文档包含于activemq整合的各个情况,springboot,js,websocket与mq整合,该资源是上传代码的word文档部分
资源推荐
资源详情
资源评论














Queue
Pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-
activemq</artifactId>
</dependency>
application.yml
server:
port: 7777
spring:
activemq:
broker-url: tcp://127.0.0.1:61616 # 自己的 mq 服务器地址
user: admin
password: admin
jms:
pub-sub-domain: false #fasle=queue true=topic,不写
默认是队列 queue
#自己定义队列名称
myqueue: boot-activemq-queue

队列生成者
Cong
package boot_mq_produce.config;
import javax.jms.Queue;
import org.apache.activemq.command.ActiveMQQueue;
import
org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import
org.springframework.scheduling.annotation.EnableSchedulin
g;
import org.springframework.stereotype.Component;
@Component
@EnableJms
@EnableScheduling
public class ConfigMq {
@Value("${myqueue}")
private String myQueue;
@Bean
public Queue queue() {
return new ActiveMQQueue(myQueue);
}
}
队列生成者代码
package boot_mq_produce.product;

import java.util.UUID;
import javax.jms.Queue;
import
org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import
org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class QueueProduce {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
public void produceMsg() {
jmsMessagingTemplate.convertAndSend(queue,"***"+UUID.rand
omUUID());
}
@Scheduled(fixedDelay=2000)
public void produceMsgScheduled() {
jmsMessagingTemplate.convertAndSend(queue,"***"+UUID.rand
omUUID());
System.out.println("间隔发送");
}
}

队列消费者
队列消费者代码
package boot_mq_consumer.cconsumer;
import javax.jms.TextMessage;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class QueueConsumer {
@JmsListener(destination="${myqueue}")
public void recive(TextMessage textMessage) throws
Exception {
System.out.println("消费者接到的消
息"+textMessage.getText());
}
}
Topic
Pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-
activemq</artifactId>

</dependency>
application.yml
主题生产者和消费者相同
server:
port: 6666
spring:
activemq:
broker-url: tcp://127.0.0.1:61616 # 自己的 mq 服务器地址
user: admin
password: admin
jms:
pub-sub-domain: true #fasle=queue true=topic,不写默
认是队列 queue
#自己定义队列名称
mytopic: boot-activemq-topic
主题生产者
Cong
package boot_mq_topic_produce.config;
import javax.jms.Topic;
import org.apache.activemq.command.ActiveMQTopic;
import
org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
剩余23页未读,继续阅读
资源评论


坚守己心
- 粉丝: 10
- 资源: 2
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制
