Apollo Build Android push

Source: Internet
Author: User

Recently made a Apollo to achieve MQTT and mobile phone side of the push, summed up the experience,

Apollo is an open source Mqtt proxy Server in Apache with the following address

http://activemq.apache.org/apollo/


1. Configure the environment, download the installation

Java environment required, then download Apollo


2. Easy to run

2.1 After the download is complete unpack, create broker, in fact the proxy server is using this broker

Open cmd, enter G:\mqtt\apache-apollo-1.7.1\bin//Unpacked folder's Bin directory Apollo create mybroker G:\apollo//in G:\apollo directory creation broker
2.2 Running service, running up there are two ways 1. Run, run. A cmd window closes the cmd window, directly closes the "G:\mqtt\apache-apollo-1.7.1\bin\mybroker\bin\apollo-broker" run
2. Install in the form of service, start, will install a service in the computer, CMD window closes do not exit "G:\mqtt\apache-apollo-1.7.1\bin\mybroker\bin\apollo-broker-service" install "G:\mqtt\apache-apollo-1.7.1\bin\mybroker\bin\apollo-broker-service" Start
Browser input The following URL open, can appear in the login window to indicate that the service started successfully http://127.0.0.1:61680, at this time the default username password is the user name: admin Password: password
3. Configure Proxy Server

Configuration can refer to http://blog.csdn.net/guobing19871024/article/details/17006823

One thing to note is that when you configure Users.properties, add the username and add it to the groups.properties, as shown in the following example

admin=123456
tom=123456

Groups.properties need to write

Admins=admin|tom

, or use another login to prompt for a password error

Next Andro's connection code

private static String host = "tcp://192.168.1.1:61613";//This is your computer in the LAN IP, port is 61613,tcp is the configuration file inside the
private static String UserName = "admin";
private static String PassWord = "123456";
private static mqttclient client;
private static String Topicstr = "Mqtt/topic";
public static final String path = Environment.getexternalstoragedirectory ()
. GetAbsolutePath () + "/apollo";
//
private static Mqtttopic topic;
private static Mqttmessage message;


public static void Connect (Mqttcallback callback) {
Host is the hostname, and test is ClientID, the client ID of the connection MQTT, typically represented by a client-only identifier,
Memorypersistence sets the Save form for the ClientID, which defaults to memory saving
Mqttconnectoptions options = new Mqttconnectoptions ();
Sets whether to empty the session, where setting to False indicates that the server retains the client's connection record.
This setting to true means that each connection to the server is connected with a new identity
Options.setcleansession (TRUE);
Set the user name for the connection
Options.setusername (UserName);
Set the password for a connection
Options.setpassword (Password.tochararray ());
Set timeout unit to seconds
Options.setconnectiontimeout (10);
Set the session heartbeat time unit to seconds the server sends a message to the client every 1.5*20 seconds to determine whether the client is online, but this method does not have a mechanism for resetting
Options.setkeepaliveinterval (20);
Link
try {
Memorypersistence is stored in memory, do not write this words run up the error
Client = new Mqttclient (host, "Jyfdjdty", New Memorypersistence ());
Client.setcallback (callback);
Client.connect (options);
Subscription
Client.subscribe (TOPICSTR, 2);
catch (Mqttsecurityexception e) {
E.printstacktrace ();
catch (Mqttexception e) {
E.printstacktrace ();
}
}


public static void Sendmsg (String msg) {
if (client!= null && client.isconnected ()) {
TOPIC = Client.gettopic (Topicstrservice);
message = new Mqttmessage ();
Message.setqos (1);//At least once, may repeat
Message.setretained (TRUE);
System.out.println (message.isretained () + "------ratained state");
Mqttdeliverytoken token;
try {
Message.setpayload (Msg.getbytes ());
token = topic.publish (message);
Token.waitforcompletion ();
catch (Mqttpersistenceexception e) {
E.printstacktrace ();
catch (Mqttexception e) {
E.printstacktrace ();
}
} else {
System.out.println ("?");
}
}


Can write a simple Java main used to send to the server, here to complete the server to the mobile phone push

4. Some issues

1. If the Publisher publishes the message and the Subscriber is not connected, only the last message will be accepted

2. The previous impression of MQTT is that the transmission of data is small, in a weak network environment performance, but with the original sokect to do contrast found that they are the same

Want to know the principle of the people to share


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.