Skip to content

Latest commit

 

History

History
164 lines (122 loc) · 3.78 KB

README.md

File metadata and controls

164 lines (122 loc) · 3.78 KB

libmqtt Java lib

A MQTT Java client library exported from libmqtt with JNI

NOTE: This library is still under work, some bugs can happen

Contents

  1. Build
  2. Usage

Build

Prerequisite

  1. Go (with GOPATH configured)
  2. JDK 1.6+
  3. make (for build ease)
  4. gcc (for cgo builds)

Steps

  1. Go get this project
go get github.com/goiiot/libmqtt
  1. Make the basic C library
cd $GOPATH/src/github.com/goiiot/libmqtt/c

make
  1. Make the Java library
cd ../java

make JAVA_HOME=/path/to/your/java/home
  1. Run the Example and explore how to make it work
make run-jni

Usage

  1. Import package
import org.goiiot.libmqtt.*;
  1. Create a client with client builder
Client client = Client.newBuilder("localhost:8883")
    .setTLS("client-cert.pem", "client-key.pem", "ca-cert.pem", "server.name", true)
    .setCleanSession(true)
    .setDialTimeout(10)
    .setKeepalive(10, 1.2)
    .setIdentity("foo", "bar")
    .setLog(LogLevel.Verbose)
    .setSendBuf(100)
    .setRecvBuf(100)
    .setClientID("foo")
    .build();
  1. Set the client lifecycle callback
client.setCallback(new Callback() {
    public void onConnResult(boolean ok, String descp) {
        if (!ok) {
            println("connection error:", descp);
            return;
        }
        println("connected to server");
        client.subscribe(sTopicName, 0);
    }

    public void onLost(String descp) {
        println("connection lost, err:", descp);
    }

    public void onSubResult(String topic, boolean ok, String descp) {
        if (!ok) {
            println("sub", topic, "failed:", descp);
            return;
        }
        println("sub", topic, "success");
        client.publish(sTopicName, 0, sTopicMsg.getBytes());
    }

    public void onPubResult(String topic, boolean ok, String descp) {
        if (!ok) {
            println("pub", topic, "failed:", descp);
            return;
        }
        println("pub", topic, "success");
        client.unsubscribe(sTopicName);
    }

    public void onUnSubResult(String topic, boolean ok, String descp) {
        if (!ok) {
            println("unsub", topic, "failed:", descp);
            return;
        }
        println("unsub", topic, "success");
        client.destroy(true);
    }

    public void onPersistError(String descp) {
        println("persist err happened:", descp);
    }
});
  1. Handle the topic message

NOTE: Current routing behavior of the Java lib is to route all topic message to the last registered topic handler, if that's null, no message will be notified, we are now working on it)

client.handle(sTopicName, new TopicMessageCallback(){
    public void onMessage(String topic, int qos, byte[] payload) {
        println("received message:", new String(payload), "from topic:", topic, "qos = " + qos);
    }
});
  1. Connect to server and wait for exit

NOTE: Currenly, once called waitClient() will not exit even when all connection has been closed, we are working on that.

client.connect();
client.waintClient();

You can refer to the example for a full usage example

LICENSE

Copyright Go-IIoT (https://github.com/goiiot)

Licensed 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.