A MQTT Java client library exported from libmqtt with JNI
NOTE: This library is still under work, some bugs can happen
- Go (with
GOPATH
configured) - JDK 1.6+
- make (for build ease)
- gcc (for cgo builds)
- Go get this project
go get github.com/thei4t/libmqtt
- Make the basic C library
cd $GOPATH/src/github.com/thei4t/libmqtt/c
make
- Make the Java library
cd ../java
make JAVA_HOME=/path/to/your/java/home
- Run the Example and explore how to make it work
make run-jni
- Import package
import org.goiiot.libmqtt.*;
- 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();
- 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);
}
});
- 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);
}
});
- 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
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.