0
点赞
收藏
分享

微信扫一扫

ActiveMq消息推送


ActiveMq消息推送

  • ​​1.原理​​
  • ​​2.实例​​
  • ​​2.1创建项目​​
  • ​​2.2添加依赖​​
  • ​​2.3创建包结构及文件​​
  • ​​2.4创建生产者​​
  • ​​2.5创建生产者启动入口​​
  • ​​2.6创建消息消费者​​
  • ​​2.7启动消费者​​
  • ​​2.8启动生产者​​
  • ​​3.总结​​

1.原理

利用ActiveMQ提供的WebSTOMP插件,让浏览器能够使用WebSocket和ActiveMQ接收或者发送消息。由服务端向ActiveMQ发送STOMP消息,而浏览器作为客户端基于stomp.js利用WebSocket与之通信,订阅并接收消息。

2.实例

2.1创建项目

首先创建一个gradle项目

ActiveMq消息推送_stomp

2.2添加依赖

ActiveMq消息推送_socket_02

compile group: 'org.apache.activemq', name: 'activemq-all', version: '5.15.9'
compile group: 'org.springframework', name: 'spring-jms', version: '5.1.8.RELEASE'
compile group: 'org.apache.activemq', name: 'activemq-pool', version: '5.15.9'
compile group: 'org.springframework', name: 'spring-beans', version: '5.1.8.RELEASE'
compile group: 'org.springframework', name: 'spring-context', version: "5.1.8.RELEASE"

2.3创建包结构及文件

ActiveMq消息推送_socket_03

2.4创建生产者

package com.study.produce;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.Destination;
import java.util.Random;

/**
* @author jiayq
*/
@Service("produce")
public class Produce {
@Resource(name = "jmsTemplate")
private JmsTemplate jmsTemplate;

@Resource(name = "testQueue")
private Destination testQueue;

public void sendMessage(String messageContent) {
jmsTemplate.send(testQueue, session -> session.createTextMessage(new Random().nextDouble() + "queue produce"));
}
}

2.5创建生产者启动入口

package com.study.client;

import com.study.produce.Produce;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;

import java.util.Random;

/**
* @author jiayq
*/
public class Client {

public static void main(String[] args) throws InterruptedException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Produce produce = applicationContext.getBean("produce",Produce.class);
while (true) {
produce.sendMessage(new Random().nextDouble() + "");
Thread.sleep(3 * 1000);
}
}

}

2.6创建消息消费者

ActiveMq消息推送_推送_04

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ActiveMqPush:This is the consume.</title>
<!--<link rel="stylesheet" type="text/css" href="default.css">-->
<!--<link rel="stylesheet" type="text/css" href="jquery.notify.jcss">-->
<script type="text/javascript" src="stomp.js"></script>
<script type="text/javascript" src="jquery.min.js"></script>
</head>
<script type="text/javascript">$(document).ready(function () {
//创建客户端
var client = Stomp.client("ws://localhost:61614/");
//定义连接成功回调函数
var onConnect = function () {
//订阅商品折扣主题的消息
client.subscribe("/queue/spring_queue", function (message) {
//弹出业务消息提醒
$("#body").append("\t" + message.body + "<br/>");
});

};
//定义错误时回调函数
var onError = function (msg) {
$("#body").html(msg);
};
//连接服务器
client.connect("guest", "guest", onConnect, onError);
client.heartbeat.incoming = 5000;
client.heartbeat.outgoing = 5000;
});</script>
<body>
<div id="body"></div>
</body>
</html>

2.7启动消费者

ActiveMq消息推送_发布/订阅_05


ActiveMq消息推送_ActiveMq_06

2.8启动生产者

ActiveMq消息推送_stomp_07


ActiveMq消息推送_发布/订阅_08


ActiveMq消息推送_socket_09

3.总结

客户端连接时的URL地址取决于ActiveMQ服务器的配置,因为实例中浏览器与ActiveMQ之间采用WebSocket通信的,而在ActiveMQ中WebSocket的传输连接器默认配置端口是61614,所以连接的URL是ws://localhost:61614/.
ActiveMQ中默认的传输连接器配置如下:

<!--
The transport connectors expose ActiveMQ over a given protocol to
clients and other brokers. For more information, see:

http://activemq.apache.org/configuring-transports.html
-->
<transportConnectors>
<!-- Create a TCP transport that is advertised on via an IP multicast
group named default. -->
<transportConnector name="openwire" uri="tcp://localhost:61616" discoveryUri="multicast://default"/>
<!-- Create a SSL transport. Make sure to configure the SSL options
via the system properties or the sslContext element. -->
<transportConnector name="ssl" uri="ssl://localhost:61617"/>
<!-- Create a STOMP transport for STOMP clients. -->
<transportConnector name="stomp" uri="stomp://localhost:61613"/>
<!-- Create a Websocket transport for the websocket dmeo -->
<transportConnector name="ws" uri="ws://localhost:61614/" />
</transportConnectors>

apache-activemq-5.15.9-bin\apache-activemq-5.15.9\examples\conf

activemq-demo.xml

ActiveMq消息推送_socket_10


举报

相关推荐

0 条评论