本文整理汇总了Java中org.fusesource.mqtt.client.Callback类的典型用法代码示例。如果您正苦于以下问题:Java Callback类的具体用法?Java Callback怎么用?Java Callback使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Callback类属于org.fusesource.mqtt.client包,在下文中一共展示了Callback类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: publish
import org.fusesource.mqtt.client.Callback; //导入依赖的package包/类
public void publish(String queue, byte[] msg, QoS qos, Callback<Void> callback) {
if (qos == null) {
qos = QoS.AT_LEAST_ONCE;
}
if (callback == null) {
callback = new Callback<Void>() {
@Override
public void onSuccess(Void value) {
}
@Override
public void onFailure(Throwable value) {
LOGGER.info("Error sending message:", value);
}
};
}
connection.publish(queue, msg, qos, false, callback);
}
示例2: publish
import org.fusesource.mqtt.client.Callback; //导入依赖的package包/类
/** {@inheritDoc} */
@Override
public void publish(final String channel, final KuraPayload payload) {
if (this.connection != null) {
final KuraPayloadEncoder encoder = new KuraPayloadEncoder(payload);
try {
this.connection.publish(channel, encoder.getBytes(), QoS.AT_MOST_ONCE, false, new Callback<Void>() {
@Override
public void onFailure(final Throwable throwable) {
System.out.println("Impossible to publish message to channel " + channel);
logTracker.log("Impossible to publish message to channel " + channel);
}
@Override
public void onSuccess(final Void aVoid) {
System.out.println("Successfully published");
logTracker.log("Successfully published");
}
});
} catch (final IOException e) {
System.out.println("I/O Exception Occurred: " + e.getMessage());
logTracker.log("I/O Exception Occurred: " + e.getMessage());
}
}
}
示例3: safelyDisconnect
import org.fusesource.mqtt.client.Callback; //导入依赖的package包/类
/**
* Disconnects the client in a thread safe way
*/
private void safelyDisconnect() {
if (this.connection != null) {
this.connection.disconnect(new Callback<Void>() {
@Override
public void onFailure(final Throwable throwable) {
System.out.println("Error while disconnecting");
}
@Override
public void onSuccess(final Void aVoid) {
System.out.println("Successfully disconnected");
}
});
}
}
示例4: unsubscribe
import org.fusesource.mqtt.client.Callback; //导入依赖的package包/类
/** {@inheritDoc} */
@Override
public void unsubscribe(final String channel) {
if (this.connection != null) {
this.channels.remove(channel);
final UTF8Buffer[] topic = { UTF8Buffer.utf8(channel) };
this.connection.unsubscribe(topic, new Callback<Void>() {
@Override
public void onFailure(final Throwable throwable) {
System.out.println("Exception occurred while unsubscribing: " + throwable.getMessage());
logTracker.log("Exception occurred while unsubscribing");
}
@Override
public void onSuccess(final Void aVoid) {
System.out.println("Successfully unsubscribed");
logTracker.log("Successfully unsubscribed");
}
});
}
}
示例5: postMessage
import org.fusesource.mqtt.client.Callback; //导入依赖的package包/类
private boolean postMessage(String message) {
if (!initialized)
return true;
connection.publish(Config.mqttPostingTopic, message.getBytes(), QoS.AT_LEAST_ONCE, false, new Callback<Void>() {
public void onSuccess(Void v) {
LOGGER.finer("Message send to " + Config.mqttPostingTopic);
}
public void onFailure(Throwable value) {
LOGGER.finer("Message failed to send to " + Config.mqttPostingTopic);
}
});
return false;
}
示例6: disconnect
import org.fusesource.mqtt.client.Callback; //导入依赖的package包/类
public static boolean disconnect() {
if (!initialized)
return true;
initialized = false;
connection.disconnect(new Callback<Void>() {
public void onSuccess(Void v) {
LOGGER.fine("Disconnected from MQTT broker");
}
public void onFailure(Throwable value) {
LOGGER.warning("Error disconnecting from MQTT broker");
}
});
return false;
}
示例7: subscribe
import org.fusesource.mqtt.client.Callback; //导入依赖的package包/类
public void subscribe(String subscrTopic, QoS quality) {
Topic[] topics = { new Topic (subscrTopic, quality) };
connection.subscribe(topics, new Callback<byte[]>() {
public void onSuccess(byte[] qoses) { LOGGER.info("Subscribe success for topic {} with quality {}", subscrTopic, quality); }
public void onFailure(Throwable value) {
LOGGER.info("Subscribe failure for topic {}", subscrTopic, value);
}
});
}
示例8: subscribe
import org.fusesource.mqtt.client.Callback; //导入依赖的package包/类
/** {@inheritDoc} */
@Override
public void subscribe(final String channel, final MessageListener callback) {
if (this.connection != null) {
if (this.channels.containsKey(channel)) {
return;
}
final CountDownLatch l = new CountDownLatch(1);
final Topic[] topic = { new Topic(channel, QoS.AT_MOST_ONCE) };
this.connection.subscribe(topic, new Callback<byte[]>() {
@Override
public void onFailure(final Throwable throwable) {
System.out.println("Impossible to SUBSCRIBE to channel \"" + channel + "\"");
logTracker.log("Impossible to SUBSCRIBE to channel \"" + channel + "\"");
l.countDown();
}
@Override
public void onSuccess(final byte[] bytes) {
KuraMQTTClient.this.channels.put(channel, callback);
l.countDown();
logTracker.log("Successfully subscribed to " + channel);
System.out.println("Successfully subscribed to " + channel);
}
});
try {
l.await();
} catch (final InterruptedException e) {
System.out.println("Impossible to SUBSCRIBE to channel \"" + channel + "\"");
logTracker.log("Impossible to SUBSCRIBE to channel \"" + channel + "\"");
}
}
}
示例9: start
import org.fusesource.mqtt.client.Callback; //导入依赖的package包/类
@Start
public void start() throws URISyntaxException {
if (host != null && !host.isEmpty() && port != null) {
// create the MQTT client
client = new MQTT();
client.setHost(host, port);
connection = client.callbackConnection();
connection.listener(this);
Log.info("{} is trying to connect to {}:{} ...", context.getInstanceName(), host, port);
connection.connect(new org.fusesource.mqtt.client.Callback<Void>() {
public void onFailure(Throwable value) {
Log.error("{} unable to connect to {}:{}", context.getInstanceName(), host, port);
}
public void onSuccess(Void v) {
Log.info("{} connected to {}:{}", context.getInstanceName(), host, port);
Topic[] topics = new Topic[] { new Topic(topic, QoS.EXACTLY_ONCE) };
connection.subscribe(topics, new Callback<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Log.info("{} subscribed to topic {}", context.getInstanceName(), topic);
}
@Override
public void onFailure(Throwable throwable) {
Log.error(context.getInstanceName()+" failed to subscribe to topic "+topic, throwable);
}
});
}
});
}
}
示例10: process
import org.fusesource.mqtt.client.Callback; //导入依赖的package包/类
@Override
public boolean process(final Exchange exchange, final AsyncCallback callback) {
byte[] body = exchange.getIn().getBody(byte[].class);
if (body != null) {
MQTTConfiguration configuration = mqttEndpoint.getConfiguration();
boolean retain = exchange.getProperty(configuration.getMqttRetainPropertyName(), configuration.isByDefaultRetain(), Boolean.class);
QoS qoS = configuration.getQoS();
Object qoSValue = exchange.getProperty(configuration.getMqttQosPropertyName());
if (qoSValue != null) {
qoS = MQTTConfiguration.getQoS(qoSValue.toString());
}
// where should we publish to
String topicName = configuration.getPublishTopicName();
// get the topic name by using the header of MQTT_PUBLISH_TOPIC
Object topicValue = exchange.getIn().getHeader(MQTTConfiguration.MQTT_PUBLISH_TOPIC);
if (topicValue != null) {
topicName = topicValue.toString();
}
final String name = topicName;
try {
log.debug("Publishing to {}", name);
mqttEndpoint.publish(name, body, qoS, retain, new Callback<Void>() {
@Override
public void onSuccess(Void aVoid) {
log.trace("onSuccess from {}", name);
callback.done(false);
}
@Override
public void onFailure(Throwable throwable) {
log.trace("onFailure from {}", name);
exchange.setException(throwable);
callback.done(false);
}
});
} catch (Exception e) {
exchange.setException(e);
callback.done(true);
return true;
}
// we continue async, as the mqtt endpoint will invoke the callback when its done
return false;
} else {
// no data to send so we are done
log.trace("No data to publish");
callback.done(true);
return true;
}
}