Added Basic MQTT Pub Sub example
This commit is contained in:
parent
d68fd6099b
commit
f48fc9bf76
|
@ -0,0 +1,29 @@
|
|||
Running the ActiveMQ Artemis Examples
|
||||
============================
|
||||
|
||||
To run an individual example firstly cd into the example directory and run
|
||||
|
||||
```
|
||||
mvn verify -Pexample
|
||||
```
|
||||
|
||||
If you are running against an un released version, i.e. from master branch, you will have to run `mvn install` on the root
|
||||
pom.xml and the example/activemq-jms-examples-common/pom.xml first.
|
||||
|
||||
If you want to run all the examples (except those that need to be run standalone) you can run `mvn verify -Pexample` in the examples
|
||||
directory but before you do you will need to up the memory used by running:
|
||||
|
||||
```
|
||||
export MAVEN_OPTS="-Xmx1024m -XX:MaxPermSize=256m"
|
||||
```
|
||||
|
||||
To run the javaee examples follow the instructions in examples/javaee/README.md
|
||||
|
||||
### Recreating the examples
|
||||
|
||||
If you are trying to copy the examples somewhere else and modifying them. Consider asking Maven to explicitly list all the dependencies:
|
||||
|
||||
```
|
||||
# if trying to modify the 'topic' example:
|
||||
cd examples/jms/topic && mvn dependency:list
|
||||
```
|
|
@ -0,0 +1,104 @@
|
|||
<?xml version='1.0'?>
|
||||
<!--
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you under the Apache License, Version 2.0 (the
|
||||
"License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations
|
||||
under the License.
|
||||
-->
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.apache.activemq.examples.mqtt</groupId>
|
||||
<artifactId>mqtt-examples</artifactId>
|
||||
<version>1.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>artemis-mqtt-publish-example</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>ActiveMQ Artemis MQTT Publish Example</name>
|
||||
|
||||
<properties>
|
||||
<activemq.basedir>${project.basedir}/../../..</activemq.basedir>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.fusesource.mqtt-client</groupId>
|
||||
<artifactId>mqtt-client</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>example</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.activemq</groupId>
|
||||
<artifactId>artemis-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>create</id>
|
||||
<goals>
|
||||
<goal>create</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>start</id>
|
||||
<goals>
|
||||
<goal>cli</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<spawn>true</spawn>
|
||||
<testURI>tcp://localhost:1883</testURI>
|
||||
<args>
|
||||
<param>run</param>
|
||||
</args>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>runClient</id>
|
||||
<goals>
|
||||
<goal>runClient</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<clientClass>org.apache.activemq.artemis.mqtt.example.MQTTBasicPubSubExample</clientClass>
|
||||
<args>
|
||||
<param>${basedir}/target/server0</param>
|
||||
</args>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.activemq.examples.mqtt</groupId>
|
||||
<artifactId>artemis-mqtt-publish-example</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.fusesource.mqtt-client</groupId>
|
||||
<artifactId>mqtt-client</artifactId>
|
||||
<version>${fuse.mqtt.client.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,132 @@
|
|||
<!--
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you under the Apache License, Version 2.0 (the
|
||||
"License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations
|
||||
under the License.
|
||||
-->
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>ActiveMQ Artemis MQTT Example</title>
|
||||
<link rel="stylesheet" type="text/css" href="../common/common.css" />
|
||||
<link rel="stylesheet" type="text/css" href="../common/prettify.css" />
|
||||
<script type="text/javascript" src="../common/prettify.js"></script>
|
||||
</head>
|
||||
<body onload="prettyPrint()">
|
||||
<h1>MQTT Example</h1>
|
||||
|
||||
<p>This is a basic MQTT example that demonstrates how to setup and connect to an Apache Artemis broker and
|
||||
send and receive messages using the MQTT protocol.</p>
|
||||
|
||||
<h2>Setting up the server</h2>
|
||||
|
||||
<p>This example will use the default out of the box configuration of Artemis you don't need to change anything to run
|
||||
this example. Artemis ships with all protocols enabled on port 61616 and also MQTT on port 1883. To enable MQTT
|
||||
on a different port you can add the following XML snippet to the 'acceptors' section of your broker.xml
|
||||
configuration file (changing the port from 1883 to what ever you require).</p>
|
||||
|
||||
|
||||
<pre class="prettyprint">
|
||||
<!-- Escaped: <acceptor name="hornetq">tcp://0.0.0.0:1883?protocols=MQTT"></acceptor> -->
|
||||
<acceptor name="hornetq">tcp://0.0.0.0:1883?protocols=MQTT"></acceptor>
|
||||
</pre>
|
||||
|
||||
For more information on configuring protocol transports see the "Configuring Transports" section of the user
|
||||
manual, specifically the section called "Single Port Support".
|
||||
|
||||
<h2>MQTT Clients</h2>
|
||||
|
||||
<p>There are a number of MQTT client implementations for various languages. The Paho project:
|
||||
http://www.eclipse.org/paho/ offers a number of clients for languages such as C, Python, JavaScript and .Net and
|
||||
is also a great resource for all things MQTT.
|
||||
|
||||
This example is actually based on the Fuse MQTT java client and was chosen as it is Apache 2.0 licensed and
|
||||
available to download from maven central. The specific client used in the example is not of importance and is
|
||||
used simply to demonstrate the features of MQTT as provided by Apache Artemis.</p>
|
||||
|
||||
<p>If you'd like to use the client demonstrated in this example, simple add the following dependency to your
|
||||
pom.xml</p>
|
||||
|
||||
<pre class="prettyprint">
|
||||
<dependency>
|
||||
<groupId>org.fusesource.mqtt-client</groupId>
|
||||
<artifactId>mqtt-client</artifactId>
|
||||
<version>1.10</version>
|
||||
</dependency>
|
||||
</pre>
|
||||
|
||||
<h2>Example Step by Step</h2>
|
||||
|
||||
<o1>
|
||||
<li>1. Connect to Artemis</li>
|
||||
|
||||
<p>We start by creating a connection to the Apache Artemis broker. In this example we specify to use TCP
|
||||
protocol on localhost. By default Apache Artemis will start all protocols on port 61616, so we connect
|
||||
to that port.</p>
|
||||
|
||||
<pre class="prettyprint">
|
||||
MQTT mqtt = new MQTT();
|
||||
mqtt.setHost("tcp://localhost:61616");
|
||||
BlockingConnection connection = mqtt.blockingConnection();
|
||||
connection.connect();
|
||||
</pre>
|
||||
|
||||
<li>2. Create subscriptions</li>
|
||||
|
||||
<p>Subscriptions in MQTT are realised by subscribing to a particular Topic. Each Topic has an address
|
||||
and a quality of service level (QoS level). Subscriptions also support wildcards. In the code below we
|
||||
subscribe to a Topic with address "mqtt/example/publish" and also a wildcard address "mqtt/#" which will
|
||||
match anything starting with "mqtt/".</p>
|
||||
|
||||
<pre class="prettyprint">
|
||||
Topic[] topics = { new Topic("mqtt/example/publish", QoS.AT_LEAST_ONCE), new Topic("mqtt/#", QoS.EXACTLY_ONCE) };
|
||||
connection.subscribe(topics);
|
||||
</pre>
|
||||
|
||||
<li>3. Sending messages</li>
|
||||
|
||||
<p>There is no type system in MQTT, messages simply consist of a number of bytes. Below we send two messages with
|
||||
UTF8 encoded strings (as a byte array). Notice the second message is sent to "mqtt/test" which should match
|
||||
our wildcard subscription we defined previously.</p>
|
||||
|
||||
<pre class="prettyprint">
|
||||
String payload1 = "This is message 1";
|
||||
String payload2 = "This is message 2";
|
||||
|
||||
connection.publish("mqtt/example/publish", payload1.getBytes(), QoS.AT_LEAST_ONCE, false);
|
||||
connection.publish("mqtt/test", payload2.getBytes(), QoS.AT_MOST_ONCE, false);
|
||||
</pre>
|
||||
|
||||
<li>4. Receiving messages</li>
|
||||
|
||||
<p>Since we have subscribed to a number of topics and sent messages to them, the client should now receive
|
||||
2 messages. We are not using callbacks here on message receive so we specifically call receive to get
|
||||
the messages. Once we recieve the message we convert the payload consisting of bytes back to a UTF8
|
||||
encoded string and print the result.</p>
|
||||
|
||||
<pre class="prettyprint">
|
||||
Message message1 = connection.receive(5, TimeUnit.SECONDS);
|
||||
Message message2 = connection.receive(5, TimeUnit.SECONDS);
|
||||
|
||||
System.out.println(new String(message1.getPayload()));
|
||||
System.out.println(new String(message2.getPayload()));
|
||||
</pre>
|
||||
</o1>
|
||||
|
||||
<h2>Result</h2>
|
||||
This example has shown you how to set up the basics of MQTT including how to connect to the Artemis broker and
|
||||
how to send and receive messages including subscriptions using wildcard addresses.
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.activemq.artemis.mqtt.example;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.fusesource.mqtt.client.BlockingConnection;
|
||||
import org.fusesource.mqtt.client.MQTT;
|
||||
import org.fusesource.mqtt.client.Message;
|
||||
import org.fusesource.mqtt.client.QoS;
|
||||
import org.fusesource.mqtt.client.Topic;
|
||||
|
||||
/**
|
||||
* A simple MQTT publish and subscribe example.
|
||||
*/
|
||||
public class MQTTBasicPubSubExample
|
||||
{
|
||||
public static void main(final String[] args) throws Exception
|
||||
{
|
||||
// Create a new MQTT connection to the broker. We are not setting the client ID. The broker will pick one for us.
|
||||
System.out.println("Connecting to Artemis");
|
||||
MQTT mqtt = new MQTT();
|
||||
mqtt.setHost("tcp://localhost:61616");
|
||||
BlockingConnection connection = mqtt.blockingConnection();
|
||||
connection.connect();
|
||||
System.out.println("Connected to Artemis");
|
||||
|
||||
// Subscribe to topics
|
||||
Topic[] topics = { new Topic("mqtt/example/publish", QoS.AT_LEAST_ONCE),
|
||||
new Topic("mqtt/#", QoS.EXACTLY_ONCE) };
|
||||
connection.subscribe(topics);
|
||||
System.out.println("Subscribed to topics.");
|
||||
|
||||
// Publish Messages
|
||||
String payload1 = "This is message 1";
|
||||
String payload2 = "This is message 2";
|
||||
|
||||
connection.publish("mqtt/example/publish", payload1.getBytes(), QoS.AT_LEAST_ONCE, false);
|
||||
connection.publish("mqtt/test", payload2.getBytes(), QoS.AT_MOST_ONCE, false);
|
||||
System.out.println("Sent messages.");
|
||||
|
||||
Message message1 = connection.receive(5, TimeUnit.SECONDS);
|
||||
Message message2 = connection.receive(5, TimeUnit.SECONDS);
|
||||
System.out.println("Received messages.");
|
||||
|
||||
System.out.println(new String(message1.getPayload()));
|
||||
System.out.println(new String(message2.getPayload()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
<?xml version='1.0'?>
|
||||
<!--
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you under the Apache License, Version 2.0 (the
|
||||
"License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations
|
||||
under the License.
|
||||
-->
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.apache.activemq.examples</groupId>
|
||||
<artifactId>artemis-examples</artifactId>
|
||||
<version>1.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>org.apache.activemq.examples.mqtt</groupId>
|
||||
<artifactId>mqtt-examples</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<name>ActiveMQ Artemis MQTT Examples</name>
|
||||
|
||||
<properties>
|
||||
<activemq.basedir>${project.basedir}/../..</activemq.basedir>
|
||||
</properties>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>release</id>
|
||||
<modules>
|
||||
<module>basic-pubsub</module>
|
||||
</modules>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>example</id>
|
||||
<activation>
|
||||
<activeByDefault>true</activeByDefault>
|
||||
</activation>
|
||||
<modules>
|
||||
<module>basic-pubsub</module>
|
||||
</modules>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
</project>
|
|
@ -65,6 +65,7 @@ under the License.
|
|||
<module>core</module>
|
||||
<module>jms</module>
|
||||
<module>soak</module>
|
||||
<module>mqtt</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
|
6
pom.xml
6
pom.xml
|
@ -59,6 +59,7 @@
|
|||
</ActiveMQ-Version>
|
||||
<resteasy.version>3.0.9.Final</resteasy.version>
|
||||
<proton.version>0.9.1</proton.version>
|
||||
<fuse.mqtt.client.version>1.10</fuse.mqtt.client.version>
|
||||
<skipUnitTests>true</skipUnitTests>
|
||||
<skipJmsTests>true</skipJmsTests>
|
||||
<skipExtraTests>true</skipExtraTests>
|
||||
|
@ -167,12 +168,11 @@
|
|||
<!-- There are newer versions of the JUnit but they break our tests -->
|
||||
</dependency>
|
||||
|
||||
<!-- ### For MQTT Tests -->
|
||||
<!-- ### For MQTT Tests && Examples -->
|
||||
<dependency>
|
||||
<groupId>org.fusesource.mqtt-client</groupId>
|
||||
<artifactId>mqtt-client</artifactId>
|
||||
<version>1.10</version>
|
||||
<scope>test</scope>
|
||||
<version>${fuse.mqtt.client.version}</version>
|
||||
<!-- Apache v2.0 License -->
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
|
Loading…
Reference in New Issue