Apollo is an Apache-based fund project that is optimized for multi-core processors with faster, more reliable, and easier maintenance than the Apache activemq5.x, a message middleware that is re-implemented with a new thread and message scheduling architecture. Apollo supports multiple protocols like activeqq: STOMP, AMQP, Mqtt, Openwire, SSL, WebSockets, this article only describes the use of the MQTT protocol.
For ActiveMQ5 please refer to: http://activemq.apache.org, this article only describes the Apollo installation and application under Windows, Apollo's detailed documentation please refer to the official website:/http Activemq.apache.org/apollo/documentation/user-manual.html.
Apollo downloads and installs 1. Download and install
Go to http://activemq.apache.org/apollo/download.html, download the Windows version of the Tarball and unzip it to your working directory (e.g. E:\apache-apollo-1.7), and create an environment variable apollo_home=e:\apache-apollo-1.7. If the operation is Windows Vista or a later version, you need to install Microsoft Visual C + + redistributable (64-bit jvm:http://www.microsoft.com/en-us/ Download/details.aspx?id=14632;32 bit jvm:http://www.microsoft.com/en-us/download/details.aspx?id=5555).
2. Create a broker instance and start the service
Enter the bin directory below the E:\apache-apollo-1.7, open the cmd window, execute the command: Apollo create E:\apollo_broker, after the command executes successfully, there will be Apollo_broker directory under the E disk, There is a bin directory under which there are two files: Apollo-broker.cmd and Apollo-broker-service.exe, the first one is to start the Apollo service with the cmd command, and the second is to create a window service.
cmd command start: Apollo-broker Run, startup success can be viewed in the browser operation (http://127.0.0.1:61680/, default username/password: admin/password);
Windows service startup: Execute Apollo-broker-service.exe, create a Windows service, and you can start the Apollo service as a Windows service.
Application of 3.MQTT Protocol
The MQTT protocol has many client implementations, please refer to: http://activemq.apache.org/apollo/versions/1.7/website/documentation/mqtt-manual.html.
This article uses Eclipse's PAHO client implementation (https://eclipse.org/paho/).
A.javascript Client: https://eclipse.org/paho/clients/js/
Download the JavaScript client project and execute the MVN command under its project root, compile the target directory, generate the Mqttws31.js, Mqttws31-min.js Two JS files, copy them to their own project related directory, and reference in the page, you can implement the JavaScript client message subscription and release, the demo code is as follows:
var client = new Paho.MQTT.Client (Location.hostname, 61623, "/", "clientId");
61623 is the default port for the WS connection and can be configured in the Apollo middleware (for the configuration of Apollo refer to: http://activemq.apache.org/apollo/documentation/user-manual.html)
Set callback Handlers
Client.onconnectionlost = Onconnectionlost;
client.onmessagearrived = onmessagearrived;
Connect the Client
Client.connect ({userName: ' admin ', Password: ' Password ', onsuccess:onconnect});
Called when the client connects
function OnConnect () {//processing after successful connection
Once a connection have been made, make a subscription and send a message.
Console.log ("OnConnect");
Client.subscribe ("/topic/event"); Topics for subscribing to messages
var message = new Paho.MQTT.Message ("Hello,this is a Test");
Message.destinationname = "/topic/event";
Client.send (message); Send Message
}
Called when the client loses its connection
function Onconnectionlost (responseobject) {//Processing after lost connection
if (responseobject.errorcode!== 0) {
Console.log ("Onconnectionlost:" +responseobject.errormessage);
}
}
Called when a message arrives
function onmessagearrived (message) {//message received after successful processing
Console.log ("onmessagearrived:" +message.payloadstring);
}
B. Java Client implementation
PAHO currently only supports J2SE and Android: https://eclipse.org/paho/clients/java/, we use the MAVEN approach.
MAVEN Library Address:
https://repo.eclipse.org/content/repositories/paho-releases/-Official releases
https://repo.eclipse.org/content/repositories/paho-snapshots/-Nightly Snapshots
Maven dependency:
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.0.1</version>
</dependency>
Description: When the version is 1.0.0 or 0.9.0, its jar package is not loaded at all, and finally found 1.0. Version 1 can be used normally.
Java-side implementations:
public interface IMessage {
String getHost();
Integer getPort();
Integer getQos();
String getTopic();
String getClientId();
String getContent();
byte[] getContentBytes();
Map<String,Object> getOption();
Object getSender();
Date getSendTime();
}
Public final class Messageprocessingcenter {
protected static Logger Logger=loggerfactory.getlogger (Messageprocessingcenter.class);
Protected static final String broker_prefix= "tcp://";
Protected static final String broker_host= "localhost";
protected static final int port=61613;
protected static final int qos=2;
Protected static final String topic= "/topic/event";
Protected static final String client_id= "ClientId";
Protected static final String mq_user= "admin";
Protected static final String mq_password= "PASSWORD";
public static void Send (IMessage message) {
String topic= Stringutils.isempty (Message.gettopic ())? TOPIC:message.getTopic ();
int qos=null = = Message.getqos ()? QOS:message.getQos ();
String broker=broker_prefix+ (Stringutils.isempty (Message.gethost ())? BROKER_HOST:message.getHost ());
int port=null = = Message.getport ()? PORT:message.getPort ();
broker+= ":" +port;
String clientId = Stringutils.isempty (Message.getclientid ())? CLIENT_ID:message.getClientId ();
Map<string,object> opts=message.getoption ();
String User=mq_user;
String Password=mq_password;
if (null! = opts) {
if (null! = Opts.get ("UserName")) {
User=opts.get ("UserName"). ToString ();
}
if (null! = Opts.get ("password")) {
Password=opts.get ("password"). toString ();
}
}
Memorypersistence persistence = new memorypersistence ();
try {
Mqttclient sampleclient = new Mqttclient (broker, clientId, persistence);
Mqttconnectoptions connopts = new Mqttconnectoptions ();
Connopts.setusername (user);
Connopts.setpassword (Password.tochararray ());
Connopts.setcleansession (TRUE);
Sampleclient.connect (connopts);
Mqttmessage MQM = new Mqttmessage (Message.getcontentbytes ());
Mqm.setqos (QoS);
Sampleclient.publish (topic, MQM);
Sampleclient.disconnect ();
} catch (Mqttexception me) {
Logger.info ("********************* Send Message exception:");
Logger.info ("********************* Reason:" + me.getreasoncode ());
Logger.info ("********************* msg:" + me.getmessage ());
Logger.info ("********************* loc:" + me.getlocalizedmessage ());
Logger.info ("********************* cause:" + me.getcause ());
Logger.info ("********************* excep:" + Me);
Me.printstacktrace ();
}
}
public static void Send (Set<imessage> Set) {
for (IMessage Message:set) {
Send (message);
}
}
}
Summary
At this point, the MQTT protocol has been deployed, the Java side can publish messages, and the JavaScript side can subscribe to and receive information from Java-side publishing.
This article is only in accordance with the official website manual implementation of the simple application, the explanation is not necessarily very accurate, there is nothing wrong place also please more guidance, more detailed application please refer to the official website documents:
Apollo:http://activemq.apache.org/apollo/documentation/user-manual.html
Eclipse paho:https://eclipse.org/paho/
ActiveMQ Apollo's Mqtt