Paho MQTT Java Client Implementation and IoTF service Java Client Implementation


关于Paho MQTT说一下:

https://www.eclipse.org/paho/

官方文档是这么说的:The Paho project provides open-source clientimplementations of MQTT and MQTT-SN messaging protocols aimed at new, existing,and emerging applications for Machine‑to‑Machine (M2M) and Internet of Things(IoT).

其实,Paho就是对MQTT的一个客户端实现,用来连接和操作MQTT Broker。目前已实现很多语言,C/C++,Java,Android,Python,JavaScript,Go,C#等。

如果你是Java的developer,那么就可以只下载Java 的部分,把用的的核心类打成jar包,在你的项目中就可以调用。至于如何使用,源码都有了,还不简单。


Paho MQTT Java Client Implementation,Java的demo:

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 MqttPublishSample {

    public static void main(String[] args) {

        String topic        = "MQTT Examples";
        String content      = "Message from MqttPublishSample";
        int qos             = 2;
        String broker       = "tcp://iot.eclipse.org:1883";
        String clientId     = "JavaSample";
        MemoryPersistence persistence = new MemoryPersistence();

        try {
            MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setCleanSession(true);
            System.out.println("Connecting to broker: "+broker);
            sampleClient.connect(connOpts);
            System.out.println("Connected");
            System.out.println("Publishing message: "+content);
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(qos);
            sampleClient.publish(topic, message);
            System.out.println("Message published");
            sampleClient.disconnect();
            System.out.println("Disconnected");
            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();
        }
    }
}

好吧,你看到的demo总是那么simple,帮你干不了任何事情,只是一个最最简单的使用,要想完成你的task,还是去读源码吧!


IoTF service Java Client Implementation:


Java Client Library - Introduction

https://docs.internetofthings.ibmcloud.com/java/javaintro.html

Dependencies

  • Paho MQTT Java Client - provides a client class which enable applications to connect to an MQTT broker
  • google-gson - library for interacting with JSON objects
  • Apache Commons Logging - library for logging various informations
  • Apache Commons Codec - provides common encoder and decoder functionalities such as Base64
  • Apache Ant - build tool for automated builds
  • Apache HttpClient - A HTTP Client library
  • Apache HttpCore - A HTTP Core library
  • Joda-Time - The date and time library for Java

Documentation

  • Device Client
  • Application Client
  • Historian Client

这一部分内容到目前作者还没有写完。其实这里是对Paho MQTT Java Client又作了进一步的封装,主要的目的是让它更方便的对IoTF (Internet of Things Foundation)service(a special MQTT Broker,已经做成了一种IoT MQTT云服务,不仅是一个MQTT Broker,还对topic作了转换)最根本的基础还是Paho  MQTT Java Client。意思就是说Paho MQTT Java Client Implementation完全可以完成你的需求。我做的测试就是把源码(org.eclipse.paho.mqtt.java-1.0.1.zip ,这里只是抽取出来Java Client的部分)打成了org.eclipse.paho.client.mqttv3-1.0.1.jar包,可以在这里下载。


这里是IoTF给的demo(好像是你不用自己管topic了,多说一句,IoTF service比普通的MQTT Broker多做了对topic的各种约定,自己设定比较麻烦,当然如果熟悉了也算不上太麻烦):

  import java.util.Properties;
  import com.google.gson.JsonObject;
  import com.ibm.iotf.client.device.DeviceClient;

  public class RegisteredDeviceEventPublish {

          public static void main(String[] args) {

                  //Provide the device specific data, as well as Auth-key and token using Properties class
                  Properties options = new Properties();

                  options.setProperty("org", "uguhsp");
                  options.setProperty("type", "iotsample-arduino");
                  options.setProperty("id", "00aabbccde03");
                  options.setProperty("auth-method", "token");
                  options.setProperty("auth-token", "AUTH TOKEN FOR DEVICE");

                  DeviceClient myClient = null;
                  try {
                          //Instantiate the class by passing the properties file
                          myClient = new DeviceClient(options);
                  } catch (Exception e) {
                          e.printStackTrace();
                  }

                  //Connect to the IBM IoT Foundation
                  myClient.connect();

                  //Generate a JSON object of the event to be published
                  JsonObject event = new JsonObject();
                  event.addProperty("name", "foo");
                  event.addProperty("cpu",  90);
                  event.addProperty("mem",  70);

                  //Registered flow allows 0, 1 and 2 QoS
                  myClient.publishEvent("status", event);
                  System.out.println("SUCCESSFULLY POSTED......");
    }
}

更多看 这里。



你可能感兴趣的:(Paho MQTT Java Client Implementation and IoTF service Java Client Implementation)