本文整理汇总了Java中org.fusesource.mqtt.client.MQTT.futureConnection方法的典型用法代码示例。如果您正苦于以下问题:Java MQTT.futureConnection方法的具体用法?Java MQTT.futureConnection怎么用?Java MQTT.futureConnection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.fusesource.mqtt.client.MQTT
的用法示例。
在下文中一共展示了MQTT.futureConnection方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
String user = env("ACTIVEMQ_USER", "admin");
String password = env("ACTIVEMQ_PASSWORD", "password");
String host = env("ACTIVEMQ_HOST", "localhost");
int port = Integer.parseInt(env("ACTIVEMQ_PORT", "1883"));
final String destination = arg(args, 0, "/topic/event");
int messages = 10000;
int size = 256;
String DATA = "abcdefghijklmnopqrstuvwxyz";
String body = "";
for (int i = 0; i < size; i++) {
body += DATA.charAt(i % DATA.length());
}
Buffer msg = new AsciiBuffer(body);
MQTT mqtt = new MQTT();
mqtt.setHost(host, port);
mqtt.setUserName(user);
mqtt.setPassword(password);
FutureConnection connection = mqtt.futureConnection();
connection.connect().await();
final LinkedList<Future<Void>> queue = new LinkedList<Future<Void>>();
UTF8Buffer topic = new UTF8Buffer(destination);
for (int i = 1; i <= messages; i++) {
// Send the publish without waiting for it to complete. This allows
// us
// to send multiple message without blocking..
queue.add(connection.publish(topic, msg, QoS.AT_LEAST_ONCE, false));
// Eventually we start waiting for old publish futures to complete
// so that we don't create a large in memory buffer of outgoing
// message.s
if (queue.size() >= 1000) {
queue.removeFirst().await();
}
if (i % 1000 == 0) {
System.out.println(String.format("Sent %d messages.", i));
}
}
queue.add(connection.publish(topic, new AsciiBuffer("SHUTDOWN"), QoS.AT_LEAST_ONCE, false));
while (!queue.isEmpty()) {
queue.removeFirst().await();
}
connection.disconnect().await();
System.exit(0);
}
示例2: main
import org.fusesource.mqtt.client.MQTT; //导入方法依赖的package包/类
public static void main(String []args) throws Exception {
String user = env("ACTIVEMQ_USER", "admin");
String password = env("ACTIVEMQ_PASSWORD", "password");
String host = env("ACTIVEMQ_HOST", "localhost");
int port = Integer.parseInt(env("ACTIVEMQ_PORT", "1883"));
final String destination = arg(args, 0, "/topic/event");
int messages = 10000;
int size = 256;
String DATA = "abcdefghijklmnopqrstuvwxyz";
String body = "";
for( int i=0; i < size; i ++) {
body += DATA.charAt(i%DATA.length());
}
Buffer msg = new AsciiBuffer(body);
MQTT mqtt = new MQTT();
mqtt.setHost(host, port);
mqtt.setUserName(user);
mqtt.setPassword(password);
FutureConnection connection = mqtt.futureConnection();
connection.connect().await();
final LinkedList<Future<Void>> queue = new LinkedList<Future<Void>>();
UTF8Buffer topic = new UTF8Buffer(destination);
for( int i=1; i <= messages; i ++) {
// Send the publish without waiting for it to complete. This allows us
// to send multiple message without blocking..
queue.add(connection.publish(topic, msg, QoS.AT_LEAST_ONCE, false));
// Eventually we start waiting for old publish futures to complete
// so that we don't create a large in memory buffer of outgoing message.s
if( queue.size() >= 1000 ) {
queue.removeFirst().await();
}
if( i % 1000 == 0 ) {
System.out.println(String.format("Sent %d messages.", i));
}
}
queue.add(connection.publish(topic, new AsciiBuffer("SHUTDOWN"), QoS.AT_LEAST_ONCE, false));
while( !queue.isEmpty() ) {
queue.removeFirst().await();
}
connection.disconnect().await();
System.exit(0);
}