Object Oriented Programming with Java
ToledoSlackGitHub Org 1GitHub Org 2
Primary version
Primary version
  • About this Course
  • Introduction to Computer Programming
    • Introduction to Computer Programming
    • Challenges
    • Multiple Choice
  • Basic Building Blocks
    • Basic Building Blocks
    • Challenges
    • Multiple Choice
  • Starting in Java
    • Starting in Java
    • Challenges
    • Multiple Choice
  • Storing and Processing Data
    • Storing and Processing Data
    • Challenges
    • Multiple Choice
  • Making Decisions
    • Making Decisions
    • Challenges
    • Test Yourself
    • Multiple Choice
  • Loop Constructs
    • Loop Constructs
    • Challenges
    • Multiple Choice
  • Strings
    • Strings
    • Challenges
    • Multiple Choice
  • Arrays
    • Arrays
    • Challenges
    • Multiple Choice
  • Object Oriented Thinking
    • Object Oriented Thinking
  • All About Objects
    • All About Objects
    • Multiple Choice
  • Defining Classes
    • Defining Classes
    • Challenges
    • Multiple Choice
  • Methods
    • Methods
    • Challenges
    • Multiple Choice
  • Constructors
    • Constructors
    • Multiple Choice
  • Inheritance
    • Inheritance
  • Starting with JavaFX
    • Starting with JavaFX
  • Hands On
    • Hands on MQTT
    • Hands on GSON
  • Hack @ IT
    • Hack @ IT
    • Caesar Encryption
      • Solution
    • Complex Numbers
  • Assignments
    • Number Characteristics
    • Linear Equation
    • LineSegment
  • Videos
    • Videos
  • Sources
    • Sources
Powered by GitBook
On this page
  • Simple MQTT Client Library
  • A Simple MQTT Client
  • A Message Handler Interface
  1. Hands On

Hands on MQTT

This chapter introduces the MQTT protocol, often used in the context of IoT.

PreviousStarting with JavaFXNextHands on GSON

Last updated 6 years ago

MQTT, short for Message Queuing Telemetry Transport is actually a machine-to-machine (M2M) connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging protocol. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is limited. It is also ideal for mobile applications because of its small size, low power usage, minimized data packets, and efficient distribution of information to one or many receivers (more...). Basically MQTT is one of pillars of the Internet of Things.

This chapter is currently linking to MQTT Essentials by HiveMQ .

Simple MQTT Client Library

To start using the SimpleMQTTClient class, you first need to add the library as a dependency to your project.

Do this by first adding the JitPack repositories to your pom.xml file:

<project>
  ...
  <repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
  </repositories>

and then by adding the library itself as a dependency to the same pom.xml file.

<project>
  ...
  <dependencies>
    ...
    <dependency>
        <groupId>com.github.BioBoost</groupId>
        <artifactId>java_simple_mqtt_client</artifactId>
        <version>0.1.2</version>
    </dependency>

Now you should be able to import the classes into your application:

import be.biosplanet.bioboost.mqtt.simple_mqtt_client.*;

A Simple MQTT Client

The UML class diagram of the SimpleMQTTClient class is shown below:

To create a SimpleMQTTClient object, you can use any of the given contructors. The ones without a broker will automatically connect to mqtt.labict.be.

For example using the default constructor:

SimpleMQTTClient client = new SimpleMQTTClient();

From this point on messages can be easily published to a given topic:

client.publish("test/oop2/hello_world", "Hello world from Java");
  // Topic: "test/oop2/hello_world"
  // Message: "Hello world from Java"

A Message Handler Interface

When subscribing to a topic, the client needs to know where to redirect the received messages. This is handled using an interface called IMQTTMessageHandler. Implementing the messageArrived method in your own classes, allows your objects to be registered as message receivers.

You can pass an object of a class that implements the IMQTTMessageHandler explicitly, for example an FXMLController:

public class FXMLController implements Initializable, IMQTTMessageHandler {
  // ...

  @Override
  public void initialize(URL url, ResourceBundle rb) {
    client.subscribe("test/oop2/bug/health", this);
  }

  @Override
  public void messageArrived(String topic, String message) {
    System.out.println("Received message " + message + " on topic " + topic);
  }
}

Or you can use an anonymous inner class object:

client.subscribe("test/oop2/hello_world", new IMQTTMessageHandler() {
  @Override
  public void messageArrived(String topic, String message) {
    System.out.println("Received message " + message + " on topic " + topic);
  }
});
https://www.hivemq.com/mqtt-essentials/
UML Class Diagram of SimpleMQTTClient