MQTT client JAVA 和 MQTT broker

Get the latest official version 

Apollo 1.7.1

Release Notes | Installation Instructions | Documentation

 

Binaries for Unix/Linux/OS X: apache-apollo-1.7.1-unix-distro.tar.gz GPG Signature
Binaries for Windows: apache-apollo-1.7.1-windows-distro.zip GPG Signature
Source Code Distribution: apollo-project-1.7.1-source-release.tar.gz GPG Signature

 

Development Snapshot

Want to help stablize the latest and greatest nightly development build? Warnning: These builds are extremely bleeding edge!

These development builds are automatically tested and deployed by a Jenkins CI server:

Apollo 99-trunk-SNAPSHOT

Installation Instructions | Documentation

 

Binaries for Unix/Linux/OS X: apache-apollo-99-trunk-SNAPSHOT-unix-distro.tar.gz
Binaries for Windows: apache-apollo-99-trunk-SNAPSHOT-windows-distro.zip

 

The development build source code should be directly checked out from our source code repository

 

 

MQTT client JAVA

A Java MQTT Client http://mqtt-client.fusesource.org/

 

MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium.

mqtt-client provides an ASL 2.0 licensed API to MQTT. It takes care of automatically reconnecting to your MQTT server and restoring your client session if any network failures occur. Applications can use a blocking API style, a futures based API, or a callback/continuations passing API style.

Using from Maven

Add the following to your maven pom.xml file.

 

<dependency>
  <groupId>org.fusesource.mqtt-client</groupId>
  <artifactId>mqtt-client</artifactId>
  <version>1.12</version>
</dependency>

Using from Gradle

Add the following to your gradle file.

 

compile 'org.fusesource.mqtt-client:mqtt-client:1.12'

Using from any Other Build System

Download the uber jar file and add it to your build. The uber contains all the stripped down dependencies which the mqtt-client depends on from other projects.

Using on Java 1.4

We also provide an java 1.4 uber jar file which is compatible with Java 1.4 JVMs. This version of the jar does not support SSL connections since the SSLEngine class used to implement SSL on NIO was not introduced until Java 1.5.

Configuring the MQTT Connection

The blocking, future, and callback APIs all share the same connection setup. You create a new instance of the MQTT class and configure it with connection and socket related options. At a minimum the setHost method be called before attempting to connect.

 

MQTT mqtt = new MQTT();
mqtt.setHost("localhost", 1883);
// or 
mqtt.setHost("tcp://localhost:1883");

 

 

 

 

 

 

 

当然,我可以为您提供一个基本的MQTT Java客户端示例代码。请注意,您需要确保已经安装了Eclipse Paho MQTT库。 ```java import org.eclipse.paho.client.mqttv3.*; public class MqttClientExample { public static void main(String[] args) { String broker = "tcp://mqtt.eclipse.org:1883"; String clientId = "JavaClient"; String topic = "test/topic"; try { MqttClient mqttClient = new MqttClient(broker, clientId); MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(true); mqttClient.connect(connOpts); System.out.println("Connected to MQTT broker"); mqttClient.subscribe(topic); System.out.println("Subscribed to topic: " + topic); mqttClient.setCallback(new MqttCallback() { public void connectionLost(Throwable cause) { System.out.println("Connection lost: " + cause.getMessage()); } public void messageArrived(String topic, MqttMessage message) throws Exception { System.out.println("Received message: " + new String(message.getPayload())); } public void deliveryComplete(IMqttDeliveryToken token) { // Not used in this example } }); } catch (MqttException e) { e.printStackTrace(); } } } ``` 上述示例代码创建了一个MQTT客户端,连接到指定的MQTT代理服务器(`broker`)。然后,它订阅了一个特定的主题(`topic`),并设置了一个回调函数来处理接收到的消息。当您运行此代码时,它将连接到MQTT代理并开始接收消息。 请注意,这只是一个简单的示例,你可以根据自己的需求进行扩展和修改。另外,确保在使用此代码之前替换有效的MQTT代理地址、客户端ID和主题。 希望对您有所帮助!如果您有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值