Skip to content

Commit

Permalink
add mqtt client support
Browse files Browse the repository at this point in the history
  • Loading branch information
yongboy committed Apr 23, 2014
1 parent b9e3bdf commit 33817b4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 36 deletions.
54 changes: 18 additions & 36 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,21 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<!--repositories>
<repositories>
<repository>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
</snapshots>
<id>repo.youku.wireless</id>
<name>repo.youku.wireless</name>
<url>http://10.103.14.128:8081/nexus/content/groups/public/</url>
<layout>default</layout>
<id>paho-mqtt-client</id>
<name>Paho MQTT Client</name>
<url>https://repo.eclipse.org/content/repositories/paho-releases/</url>
</repository>
<repository>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
</snapshots>
<id>repo.youku.log</id>
<name>repo.youku.log</name>
<url>http://repo.data.1verge.net/nexus/content/groups/public/</url>
<layout>default</layout>
</repository>
</repositories-->

</repositories>

<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId> <!-- Use 'netty-all' for 4.0 or above -->
<version>4.0.18.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.18.Final</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>com.albin.mqtt</groupId>
Expand All @@ -66,5 +41,12 @@
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>mqtt-client</artifactId>
<version>0.4.0</version>
</dependency>

</dependencies>
</project>
33 changes: 33 additions & 0 deletions src/main/java/com/test/client/PubMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.test.client;
import java.io.UnsupportedEncodingException;

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.MqttTopic;

public class PubMessage {
public static void main(String[] args) throws MqttException,
UnsupportedEncodingException {

String topicName = "sub/client1";
String message = "Hello Mqtt Server !";
String tcpUrl = "tcp://127.0.0.1:8083";
String clientId = "pub-msg/client";
pubMsg(tcpUrl, clientId, topicName, message);
}

public static void pubMsg(String tcpUrl, String clientId, String topicName,
String message) throws MqttException, UnsupportedEncodingException {
MqttClient client = new MqttClient(tcpUrl, clientId);
MqttConnectOptions mqcConf = new MqttConnectOptions();
mqcConf.setConnectionTimeout(300);
mqcConf.setKeepAliveInterval(1200);
client.connect(mqcConf);

MqttTopic topic = client.getTopic(topicName);
topic.publish(message.getBytes("utf8"), 0, false);

client.close();
}
}

0 comments on commit 33817b4

Please sign in to comment.