MQTT moquette 的Server發佈主題java
- package com.etrip.mqtt.future;
-
- import java.net.URISyntaxException;
-
- import org.fusesource.mqtt.client.FutureConnection;
- import org.fusesource.mqtt.client.MQTT;
- import org.fusesource.mqtt.client.QoS;
- import org.fusesource.mqtt.client.Topic;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- public class MQTTFutureServer {
- private static final Logger LOG = LoggerFactory.getLogger(MQTTFutureServer.class);
- private final static String CONNECTION_STRING = "tcp://192.168.208.46:1883";
- private final static boolean CLEAN_START = true;
- private final static short KEEP_ALIVE = 30;
- public static Topic[] topics = {
- new Topic("china/beijing", QoS.EXACTLY_ONCE),
- new Topic("china/tianjin", QoS.AT_LEAST_ONCE),
- new Topic("china/henan", QoS.AT_MOST_ONCE)};
- public final static long RECONNECTION_ATTEMPT_MAX=6;
- public final static long RECONNECTION_DELAY=2000;
-
- public final static int SEND_BUFFER_SIZE=2*1024*1024;
- public static void main(String[] args) {
- MQTT mqtt = new MQTT();
- try {
-
- mqtt.setHost(CONNECTION_STRING);
-
- mqtt.setCleanSession(CLEAN_START);
-
- mqtt.setReconnectAttemptsMax(RECONNECTION_ATTEMPT_MAX);
-
- mqtt.setReconnectDelay(RECONNECTION_DELAY);
-
- mqtt.setKeepAlive(KEEP_ALIVE);
-
- mqtt.setSendBufferSize(SEND_BUFFER_SIZE);
-
-
- final FutureConnection connection= mqtt.futureConnection();
- connection.connect();
- int count=1;
- while(true){
- count++;
-
-
- String message="hello "+count+"chinese people !";
- String topic = "china/beijing";
- connection.publish(topic, message.getBytes(), QoS.AT_LEAST_ONCE,
- false);
- System.out.println("MQTTFutureServer.publish Message "+"Topic Title :"+topic+" context :"+message);
-
- }
- } catch (URISyntaxException e) {
-
- e.printStackTrace();
- } catch (Exception e) {
-
- e.printStackTrace();
- }
- }
- }
MQTT moquette 的Client接收主題網絡
- package com.etrip.mqtt.future;
-
- import java.net.URISyntaxException;
-
- import org.fusesource.mqtt.client.Future;
- import org.fusesource.mqtt.client.FutureConnection;
- import org.fusesource.mqtt.client.MQTT;
- import org.fusesource.mqtt.client.Message;
- import org.fusesource.mqtt.client.QoS;
- import org.fusesource.mqtt.client.Topic;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- public class MQTTFutureClient {
- private static final Logger LOG = LoggerFactory.getLogger(MQTTFutureClient.class);
- private final static String CONNECTION_STRING = "tcp://192.168.208.46:1883";
- private final static boolean CLEAN_START = true;
- private final static short KEEP_ALIVE = 30;
- private final static String CLIENT_ID = "publishService";
- public static Topic[] topics = {
- new Topic("china/beijing", QoS.EXACTLY_ONCE),
- new Topic("china/tianjin", QoS.AT_LEAST_ONCE),
- new Topic("china/henan", QoS.AT_MOST_ONCE)};
- public final static long RECONNECTION_ATTEMPT_MAX=6;
- public final static long RECONNECTION_DELAY=2000;
-
- public final static int SEND_BUFFER_SIZE=2*1024*1024;
-
-
- public static void main(String[] args) {
-
- MQTT mqtt = new MQTT();
- try {
-
- mqtt.setHost(CONNECTION_STRING);
-
- mqtt.setCleanSession(CLEAN_START);
-
- mqtt.setReconnectAttemptsMax(RECONNECTION_ATTEMPT_MAX);
-
- mqtt.setReconnectDelay(RECONNECTION_DELAY);
-
- mqtt.setKeepAlive(KEEP_ALIVE);
-
- mqtt.setSendBufferSize(SEND_BUFFER_SIZE);
-
-
- final FutureConnection connection= mqtt.futureConnection();
- connection.connect();
- connection.subscribe(topics);
- while(true){
- Future<Message> futrueMessage=connection.receive();
- Message message =futrueMessage.await();
-
-
- System.out.println("MQTTFutureClient.Receive Message "+ "Topic Title :"+message.getTopic()+" context :"+String.valueOf(message.getPayloadBuffer()));
- }
- } catch (URISyntaxException e) {
-
- e.printStackTrace();
- } catch (Exception e) {
-
- e.printStackTrace();
- }finally{
-
- }
- }
- }