Getting started with the MQTT client for Java
Get up and running with the MQTT client for Java sample applications, using either IBM® MessageSight or IBM WebSphere® MQ as the MQTT server. The sample applications use a client library from the MQTT software development toolkit (SDK) from IBM. The SampleAsyncCallBack sample application is a model for writing MQTT applications for Android and other event-driven operating systems.
Before you begin
- You can run an MQTT client for Java app on any platform with JSE 1.5 or above that is
Java Compatible
. See System requirements for IBM Mobile Messaging and M2M Client Pack. - If there is a firewall between your client and the server, check that it does not block MQTT traffic.
About this task
The purpose of the task is to check that you can build and run an MQTT client for Java sample application, connect it to IBM WebSphere MQ or IBM MessageSight as the MQTT version 3 server, and exchange messages.
Follow this task to run the sample application from the Eclipse workbench, or from a command line. The steps in the example are for Windows. With small modifications, you can run the sample application on any platform that supports JSE 1.5 or above.
You can run applications on the same server as IBM WebSphere MQ, where the environment
for running applications that connect to IBM WebSphere MQ is configured
for you. Follow the task Configuring the MQTT service from the command line to
install and configure IBM WebSphere MQ with
the IBM WebSphere MQ Telemetry option
on Windows or Linux®. When the
environment is installed and configured, run the sample application, MQTTV3Sample
,
to verify the installation.
Procedure
Results
You compiled and ran the MQTT Java sample applications that are connected to IBM WebSphere MQ or IBM MessageSight as the MQTT server.
What to do next
Study the Javadoc reference information; see step 3 of Compile and run all the MQTT client sample Java apps from Eclipse. Alternatively open the Javadoc html files that are in the SDK\clients\java\doc\javadoc directory in the Mobile Messaging and M2M Client Pack.
Compile and run the Paho sample programs from the command line
Compile and run the Paho sample application Sample.java from the command line. The sample is in the MQTT SDK. The sample is built with the MQTT Paho client libraries in the org.eclipse.paho.client.mqttv3 package. It demonstrates an MQTT publisher and subscriber. Two other Paho samples in the same directory can be built and run the same way. They differ by calling the MQTT library asynchronously.
Before you begin
About this task
Compile and run Sample.java from the SDK client samples subdirectory SDK\clients\java\samples. The Java code is in the directory, SDK\clients\java\samples\org\eclipse\paho\sample\mqttv3app.
Procedure
What to do next
Create scripts to compile and run the other samples in the SDK\clients\java\samples\org\eclipse\paho\sample\mqttv3app subdirectory. Copy the script in Figure 1 and replace Sample with SampleAsyncWait or SampleAsyncCallBack. The other samples are asynchronous versions of the synchronous Sample program.
- SampleAsyncWait
- SampleAsyncWait is in the org.eclipse.paho.client.mqttv3 package. It uses the asynchronous MQTT API; it waits on a different thread until an action completes. The main thread can do other work until it synchronizes on the thread that is waiting for the MQTT action to complete.
- SampleAsyncCallBack
- SampleAsyncCallBack is in the org.eclipse.paho.client.mqttv3 package. It calls the asynchronous MQTT API. The asynchronous API does not wait for MQTT to complete processing a call; it returns to the application. The application carries on with other tasks, then waits for the next event to arrive for it to process. MQTT posts an event notification back to the application when it completes processing. The event driven MQTT interface is suited to the service and activity programming model of Android and other event driven operating systems.
The asynchronous samples demonstrate how to reduce the amount of time that an MQTT application blocks while it is waiting for the MQTT client. It is important to eliminate blocking calls in the main thread to increase responsivity and battery life in a mobile environment.
- SampleAsyncWait does not block while the MQTT is waiting for network interactions.
- SampleAsyncCallBack does not block waiting for the MQTT client to complete any actions. The latter is necessary when a JavaScript page is calling the MQTT client from a browser. JavaScript pages must not block. Responses to actions must be posted back to the main browser thread, which calls the MQTT event handler you write to process the notification.
Compile and run all the MQTT client sample Java apps from Eclipse
Compile and run the MQTT client sample Java apps that are in the Mobile Messaging and M2M Client Pack. They demonstrate a MQTT publisher and subscriber.