DOCKER+ACTIVEMQ+MQTT Example _activemq

Source: Internet
Author: User
Tags getmessage docker run

1. Start-up vessel: Docker run-p 1883:1883-p 8161:8161-d leo/activemq

If there is no Docker, it is also possible to start activemq directly.

We just need to write publisher and Subscriber when the ACTIVEMQ is ready to start.

2. Need to add 2 maven dependencies:

		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactid>activemq-all </artifactId>
			<version>5.13.3</version>
		</dependency>

		<dependency>
			<groupId>org.eclipse.paho</groupId>
			<artifactid>org.eclipse.paho.client.mqttv3</ artifactid>
			<version>1.1.0</version>
		</dependency>

The first dependency is ACTIVEMQ, the second depends on the details, please refer to: https://eclipse.org/paho/clients/java/


The 3.Publisher code is as follows:

Package leo.mqtt;
Import org.eclipse.paho.client.mqttv3.MqttClient;
Import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
Import org.eclipse.paho.client.mqttv3.MqttException;
Import Org.eclipse.paho.client.mqttv3.MqttMessage;

Import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
		public class Publisher {public static void main (string[] args) {String topic = "Topicofleo";
		String content = "Hello leo!";  int QoS = 1;//0:msg is only issued 1 times and does not require confirmation.   The 1:msg is issued at least 1 times and needs confirmation.
		2. Hair and only 1 times, and need to do 4 waves.
		String broker = "tcp://127.0.0.1:1883";
		String clientId = "Publisher1";

		Memorypersistence persistence = new memorypersistence ();
			try {mqttclient client = new Mqttclient (broker, clientId, persistence);
			Mqttconnectoptions connopts = new Mqttconnectoptions ();
			Connopts.setcleansession (false);//first start whether to clear session client.connect (connopts);
			Mqttmessage message = new Mqttmessage (Content.getbytes ());
			Message.setqos (QoS);
			Client.publish (topic, message); Client.disconnecT ();
		System.exit (0);
			catch (mqttexception me) {System.out.println ("reason" + me.getreasoncode ());
			System.out.println ("msg" + me.getmessage ());
			System.out.println ("loc" + Me.getlocalizedmessage ());
			SYSTEM.OUT.PRINTLN ("Cause" + me.getcause ());
			System.out.println ("excep" + Me);
		Me.printstacktrace ();
 }
	}
}


The 4.subscriber code is as follows:

Package leo.mqtt;
Import Org.eclipse.paho.client.mqttv3.IMqttMessageListener;
Import org.eclipse.paho.client.mqttv3.MqttClient;
Import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
Import org.eclipse.paho.client.mqttv3.MqttException;
Import Org.eclipse.paho.client.mqttv3.MqttMessage;

Import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
		public class Subscriber {public static void main (string[] args) {String topic = "Topicofleo";
		String broker = "tcp://127.0.0.1:1883";
		String clientId = "Subscriber1";

		Memorypersistence persistence = new memorypersistence ();
			try {mqttclient client = new Mqttclient (broker, clientId, persistence);
			Mqttconnectoptions connopts = new Mqttconnectoptions ();
			Connopts.setcleansession (FALSE);
			Client.connect (connopts); Client.subscribe (topic, new Imqttmessagelistener () {@Override public void messagearrived (String topic, Mqttmessa GE message) throws Exception {System.out.println (topic + "->" + MessaGE);
		}
			});
			catch (mqttexception me) {System.out.println ("reason" + me.getreasoncode ());
			System.out.println ("msg" + me.getmessage ());
			System.out.println ("loc" + Me.getlocalizedmessage ());
			SYSTEM.OUT.PRINTLN ("Cause" + me.getcause ());
			System.out.println ("excep" + Me);
		Me.printstacktrace ();
 }
	}
}

5. Run Subscriber first, and then run Publisher to see the effect.

This is the end of the introductory demo.



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.