Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSC310 Lecture 13

CSC310 Lecture 13

MQTT

Avatar for Javier Gonzalez-Sanchez

Javier Gonzalez-Sanchez PRO

October 20, 2026

Transcript

  1. CSC 3100 Software Engineering Lecture 13. We Need a Cloud!

    Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.info ffi o ce: 14 -227
  2. What we already know • Classes and interfaces define responsibilities

    • UML describes structure before implementation • Observer notifies interested objects • Strategy changes behavior without changing a client • Java code should reflect the design Next: How do separate applications communicate? 03
  3. Vocabulary • Broker: server that routes published messages • Client:

    application that publishes and/or subscribes • Topic: named message channel, e.g., example/toRead • Payload: data sent with a publication • Subscribe: express interest in a topic 10 08
  4. Main: configures the broker/topics and starts two threads. Publisher implements

    Runnable: connects and publishes 10 messages per topic. Subscriber implements Runnable and MqttCallback: subscribes and handles incoming messages. MqttClient: library class supplied by Eclipse Paho. 12 10
  5. Main - BROKER_URL: String - TOPICS: String[] + main(String[]): void

    Subscriber - broker: String; - topics: String[] + Subscriber(String, String[]) + run(): void + connectionLost(Throwable): void + messageArrived(String, MqttMessage): void + deliveryComplete(IMqttDeliveryToken): void Publisher - broker: String; - topics: String[] + Publisher(String, String[]) + run(): void «interface» Runnable + run(): void 13 MqttClient + connect(); + subscribe(...) + publish(...); + disconnect() «interface» MqttCallback + connectionLost(...) + messageArrived(...); + deliveryComplete(...)
  6. Eclipse Paho for Java Wh t is Eclipse P ho?

    • A set of MQTT client libr ries developed by the Eclipse Found tion. • It supports multiple l ngu ges, such s J v , Python, J v Script, etc. J v Dependency (M ven) a a a a a a a a a a a a a 15 a <dependency> <groupId>org.eclipse.paho</groupId> <artifactId>org.eclipse.paho.client.mqttv3</artifactId> <version>1.2.5</version> </dependency>
  7. Quality-of-service options • 0 – "at most once": A.K.A. "fire-and-forget."

    This option is suitable when message loss is acceptable, as it doesn't require acknowledgment or persistence. • 1 – "at least once": This option should be chosen when message loss is unacceptable, and your subscribers can handle duplicate messages. • 2 – "Exactly once": This option is ideal when message loss is unacceptable, and your subscribers cannot handle duplicate messages. 18
  8. CSC 3100 / REPOSITORY UML Class diagram — cloud-mqtt Main

    - BROKER_URL: String - TOPICS: String[] + main(String[]): void Subscriber - broker: String; - topics: String[] + Subscriber(String, String[]) + run(): void + connectionLost(Throwable): void + messageArrived(String, MqttMessage): void + deliveryComplete(IMqttDeliveryToken): void Publisher - broker: String; - topics: String[] + Publisher(String, String[]) + run(): void «interface» Runnable + run(): void MqttClient + connect(); + subscribe(...) + publish(...); + disconnect() «interface» MqttCallback + connectionLost(...) + messageArrived(...); + deliveryComplete(...)
  9. public class Main { private static final String BROKER_URL =

    "tcp://broker.hivemq.com:1883"; public static void main(String[] args) { new Thread(new Subscriber(BROKER_URL, TOPICS)).start(); new Thread(new Publisher(BROKER_URL, TOPICS)).start(); } } 27 13
  10. public class Publisher implements Runnable { ... } public class

    Subscriber implements Runnable, MqttCallback { ... } Runnable defines run(). MqttCallback defines connectionLost(...), messageArrived(...), and deliveryComplete(...). 28 12
  11. Abbreviated from Publisher.java String clientId = MqttClient.generateClientId(); MqttClient client =

    new MqttClient(broker, clientId); client.connect(); String content = "This is message " + counter; MqttMessage message = new MqttMessage(content.getBytes(StandardCharsets.UTF_8)); message.setQos(2); for (String topic : topics) client.publish(topic, message); 29 18
  12. Abbreviated from Subscriber.java MqttClient client = new MqttClient(broker, clientId); client.setCallback(this);

    client.connect(); for (String topic : topics) client.subscribe(topic, 2); @Override public void messageArrived(String topic, MqttMessage message) { String payload = new String(message.getPayload(), StandardCharsets.UTF_8); System.out.println(payload); } 30 19
  13. Repository reference and running commands github.com/csc-3100/cloud-mqtt Main.java — broker configuration

    and thread startup Publisher.java — MqttClient.publish(...), QoS 2 Subscriber.java — MqttCallback and messageArrived(...) Broker: tcp://broker.hivemq.com:1883 Topics: example/toIgnore and example/toRead 31
  14. CSC 3100 Software Engineering Javier Gonzalez-Sanchez, Ph.D. [email protected] Fall 2026

    Copyright. These slides may be used only as study material for CSC 5100 within the California State University system. They may not be distributed or used for any other purpose.