需求 : 工厂员工完成某道工序后,需要将消息推送给 检查人员

也可以使用 WebSockets ,前端更容易实现

思路: 使用activeMQ推送消息,前端实时接收消息

实现 :

  1.基于springBoot的activeMQ 实现起来比较方便 配置文件如下

############ activemq
spring.activemq.broker-url=tcp://127.0.0.1:61616
# 账号
spring.activemq.user=xxx
# 密码
spring.activemq.password=xxx
# 等待消息发送响应的时间。设置为0等待永远。
spring.activemq.send-timeout=0 queueName=polling.queue
topicName=polling.topic
# 默认情况下activemq提供的是queue模式,若要使用topic模式需要配置下面配置
spring.jms.pub-sub-domain=true

  2.java代码 配置文件

package workstation.open.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
import org.springframework.jms.core.JmsMessagingTemplate; import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Topic; /**
* @author ThreeNut
* @date 2021/5/18 13:55
*/
@Configuration
public class ActiveMQConfig {
@Value("${queueName}")
private String queueName; @Value("${topicName}")
private String topicName; @Value("${spring.activemq.user}")
private String usrName; @Value("${spring.activemq.password}")
private String password; @Value("${spring.activemq.broker-url}")
private String brokerUrl; @Bean
public Queue queue(){
return new ActiveMQQueue(queueName);
} @Bean
public Topic topic(){
return new ActiveMQTopic(topicName);
} @Bean
public ActiveMQConnectionFactory connectionFactory() {
return new ActiveMQConnectionFactory(usrName, password, brokerUrl);
} @Bean
public JmsMessagingTemplate jmsMessageTemplate(){
return new JmsMessagingTemplate(connectionFactory());
} // 在Queue模式中,对消息的监听需要对containerFactory进行配置
@Bean("queueListener")
public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(ConnectionFactory connectionFactory){
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setPubSubDomain(false);
return factory;
} //在Topic模式中,对消息的监听需要对containerFactory进行配置
@Bean("topicListener")
public JmsListenerContainerFactory<?> topicJmsListenerContainerFactory(ConnectionFactory connectionFactory){
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setPubSubDomain(true);
return factory;
}
}

  3.java代码 消息推送  消息生产者

3.1 Queue(点到点)模式在点对点的传输方式中,消息数据被持久化,每条消息都能被消费,没有监听QUEUE地址也能被消费,数据不会丢失,一对一的发布接受策略,保证数据完整。

  3.2Topic主题模式,就是订阅模式,不保证消息都能被接收到   发送方式 :群发

package workstation.open.mq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component; import javax.jms.Destination;
import javax.jms.Topic; /**
* 消息生产
* @author BinPeng
* @date 2021/5/18 13:47
*/
@Component
public class PollingQueueProducer { @Autowired
private JmsMessagingTemplate jmsMessagingTemplate; /**
* 群发消息
*/
@Autowired
private Topic topic; public String sendQueue(String msg) {
this.sendMessage(this.topic,msg);
System.out.println("发送消息成功!!!");
return "success";
} private void sendMessage(Destination destination,String msg){
jmsMessagingTemplate.convertAndSend(destination, msg);
} }

  4.后端测试(仅用来在后端测试,看是否接收到消息)

package workstation.open.mq;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import workstation.common.util.ReqResult; import javax.servlet.http.Cookie;
import java.util.LinkedList;
import java.util.Queue; @Component
public class QueueConsumer {
/**
* queue模式的消费者
* @param message 消息
*/ @JmsListener(destination="${topicName}", containerFactory="topicListener")
public ReqResult readActiveQueue(String message) {
Queue<String> queue = new LinkedList<String>();
queue.offer(message);
System.out.println(message +"----------------------------------");
return new ReqResult().put("msg",queue);
}
}

  5.基于前端VUE使用Stompjs接收ActiveMQ消息实现方法  -- 消息消费者

  准备工作:

     npm install stompjs

    npm install net

  5.1 linkparam.js 配置文件

export const MQ_SERVICE = 'ws://127.0.0.1:61614/stomp' // mq服务地址(默认监听消息端口)
export const MQ_USERNAME = 'xxx' // mq连接用户名
export const MQ_PASSWORD = 'xxx' //mq连接密码

  5.2 sock.vue

<template>
<div></div>
</template> <script>
import Stomp from 'stompjs'
import { MQ_SERVICE, MQ_USERNAME, MQ_PASSWORD } from '../../utils/linkparam'
export default {
name: 'entry',
data () {
return {
client: Stomp.client(MQ_SERVICE),
topic : '/topic/iqc_check_list'
}
},
created () {
this.connect()
}, methods: {
onConnected: function (frame) {
console.log('Connected: ---------' + frame.body)
// 主题模式
this.client.subscribe(this.topic, this.responseCallback, { id: 20210820 })
},
onFailed: function (frame) { },
responseCallback: function (frame) {
if(frame.body != null){
this.$notify.info({
showClose: true,
message: '有新的检验单 ' + frame.body,
position: 'bottom-right',
duration: '3000',
onClose: () => {
// 执行查询方法
this.$parent.query()
},
});
}
}, connect: function () {
let headers = {
'login': MQ_USERNAME,
'passcode': MQ_PASSWORD
}
// 心跳发送频率
this.client.heartbeat.outgoing = 50000;
// 心跳接收频率
this.client.heartbeat.incoming = 50000;
this.client.connect(headers, this.onConnected, this.onFailed)
}
}
}
</script>

  5.3 在APP.vue 文件下添加(因为要在此页面展示所以添加如下,展示页面按照你自己的实际情况配置即可)

import sock from './components/sock'

components:{

  sock
}

至此前后端配置结束
总结: 需注意 如果监听的是远程服务 需要配置为外网ip地址,端口 61614和61616加入安全组. 监听端口为: 61614
         activeMQ 客户端地址:http://localhost:8161/admin/topics.jsp

前端vue监听activeMQ消息后端推送消息--实战的更多相关文章

  1. iOS监听模式系列之推送消息通知

    推送通知 和本地通知不同,推送通知是由应用服务提供商发起的,通过苹果的APNs(Apple Push Notification Server)发送到应用客户端.下面是苹果官方关于推送通知的过程示意图: ...

  2. node配置微信小程序解密消息以及推送消息

    上一篇文章介绍过 微信小程序配置消息推送,没有看过的可以先去查看一下,这里就直接去把那个客服消息接口去解密那个消息了. 在这里我选择的还是json格式的加密. 也就是给小程序客服消息发送的消息都会被微 ...

  3. 利用socket.io实现消息实时推送

    最近在写的项目中存在着社交模块,需要实现这样的一个功能:当发生了用户被点赞.评论.关注等操作时,需要由服务器向用户实时地推送一条消息.最终完成的项目地址为:socket-message-push,这里 ...

  4. signalr推送消息

    参考:Tutorial: Getting Started with SignalR 2 and MVC 5 环境:vs2013,webapi2,entity framework6.0 实现效果:当用户 ...

  5. JAVA多线程(四) Executor并发框架向RabbitMQ推送消息

    github代码地址: https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service ...

  6. spring boot 集成 websocket 实现消息主动推送

    spring boot 集成 websocket 实现消息主动 前言 http协议是无状态协议,每次请求都不知道前面发生了什么,而且只可以由浏览器端请求服务器端,而不能由服务器去主动通知浏览器端,是单 ...

  7. spring+activemq实战之配置监听多队列实现不同队列消息消费

    摘选:https://my.oschina.net/u/3613230/blog/1457227 摘要: 最近在项目开发中,需要用到activemq,用的时候,发现在同一个项目中point-to-po ...

  8. Android高效率编码-第三方SDK详解系列(二)——Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能

    Android高效率编码-第三方SDK详解系列(二)--Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能 我的本意是第二篇写Mob的shareSD ...

  9. Spring Boot 监听 Activemq 中的特定 topic ,并将数据通过 RabbitMq 发布出去

    1.Spring Boot 和 ActiveMQ .RabbitMQ 简介 最近因为公司的项目需要用到 Spring Boot , 所以自学了一下, 发现它与 Spring 相比,最大的优点就是减少了 ...

  10. Vue跨路由触发事件,Vue监听sessionStorage

    近来,在做公司的聊天系统,引用的是极光的api.项目需求实时监听别人发过来的消息,进行渲染到页面,还有历史记录也要渲染,历史记录和实时聊天记录返回的结构体还不一样,看到需求的我欲哭无泪,首先登录是在首 ...

随机推荐

  1. 【Azure Service Bus】使用Spring Cloud integration示例代码,为多个 Service Bus的连接使用 ConnectionString 方式

    问题描述 查看Service Bus的Java示例代码,发现使用Spring Cloud Integration,配置 Application.yaml 可以连接到两个Service Bus. 但代码 ...

  2. 【Azure 事件中心】Event Hub 消费端出现 Timeout Exception,errorContext中 LINK_CREDIT为0的解释

    问题描述 在使用Event Hub SDK消费数据过程中,出现大量的Timeout Exception,详细消息为: com.microsoft.azure.eventhubs.TimeoutExce ...

  3. 【XInput】手柄模拟鼠标运作之 .NET P/Invoke 和 UWP-API 方案

    上一篇中,老周简单肤浅地介绍了 XInput API 的使用,并模拟了鼠标移动,左.右键单击和滚轮.本篇,咱们用 .NET 代码来完成相同的效果. 说起来也是倒霉,博文写了一半,电脑忽然断电了.不知道 ...

  4. 内部UI自动化测试培训之python基础

    这个文档的由来是公司内部UI自动化测试培训的资料.部门为了减少测试工作量,准备做UI自动化测试.我写python,其他同事都是java,所以python基础和UI自动化测试selenium的培训就由我 ...

  5. zabbix“专家坐诊”第178期问答汇总

    大家好,我是乐乐.早在三年前,我们就在社区举办了zabbix公益问答活动,并且定在每周三邀请资深的zabbix技术工程师,为社群的小伙伴进行免费的答疑.到现在已经178期了.后续我将会把每期的答疑汇总 ...

  6. 摆脱鼠标系列 - vscode 花括号 开始结束 间的跳转 Ctrl + Shift + \

    为什么 摆脱鼠标系列 - vscode 花括号 开始结束 间的跳转 Ctrl + Shift + \ 快速移动到下一个 注意有时候输入法会有问题 因为 Ctrl + Shift 是切换输入法,所以回头 ...

  7. obs 屏幕录像 1. 屏幕放大缩小 2. 圆形摄像头

    obs 屏幕录像 1. 屏幕放大缩小 2. 圆形摄像头 屏幕放大缩小 windows 放大镜 屏幕放大快捷键 win + '+' 是放大屏幕 win + '-' 是缩小屏幕 因为obs不支持缩放功能, ...

  8. 关于Sql server数据类型HierarchyID 数据类型用法和递归显示完整路径

    SQL Server 2008版本之后的新类型HierarchyID 不知道大家有没有了解, 该类型作为取代id, parentid的一种解决方案,让人非常惊喜. 官方给的案例浅显易懂,但是没有实现我 ...

  9. 已安装docker-compose,安装harbor时还是提示docker-compose未安装或者Permission denied的解决方案

    安装Harbor时,下载安装了docker-compose并赋予权限 sudo curl -L "https://github.com/docker/compose/releases/dow ...

  10. IDEA/Android Studio的gradle控制台输出中文乱码问题解决

    原文地址: IDEA/Android Studio的gradle控制台输出中文乱码问题解决 - Stars-One的杂货小窝 在项目中,有使用到Gradle自定义脚本,会有些输出日志,但是输出中文就变 ...