本文整理汇总了Java中org.eclipse.paho.client.mqttv3.MqttCallback类的典型用法代码示例。如果您正苦于以下问题:Java MqttCallback类的具体用法?Java MqttCallback怎么用?Java MqttCallback使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MqttCallback类属于org.eclipse.paho.client.mqttv3包,在下文中一共展示了MqttCallback类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getMqttClient
import org.eclipse.paho.client.mqttv3.MqttCallback; //导入依赖的package包/类
/**
* get MqttClient by clientKey
* @param clientKey
* @return
* @throws MqttException
*/
public static MqttClient getMqttClient(String serverURI, String clientId,StringRedisTemplate redisTemplate)
throws MqttException{
String clientKey=serverURI.concat(clientId);
if(clientMap.get(clientKey)==null){
lock.lock();
if(clientMap.get(clientKey)==null){
MqttClientPersistence persistence = new MemoryPersistence();
MqttClient client = new MqttClient(serverURI, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
MqttCallback callback = new IMMqttCallBack(client,redisTemplate);
client.setCallback(callback);
connOpts.setCleanSession(true);
client.connect(connOpts);
clientMap.put(clientKey, client);
}
lock.unlock();
}
return clientMap.get(clientKey);
}
示例2: messageArrived
import org.eclipse.paho.client.mqttv3.MqttCallback; //导入依赖的package包/类
@Override
public void messageArrived(final String topic, final MqttMessage message) throws Exception {
logger.info("Message arrived at '{}' with payload {}", topic, message);
final Collection<MqttCallback> callbacks = this.listeners.get(topic);
if (callbacks != null) {
for (final MqttCallback mqttCallback : callbacks) {
try {
mqttCallback.messageArrived(topic, message);
} catch (Exception e) {
logger.warn("Exception in message processing", e);
}
}
}
/*
* Delegates, but cuts-off the prefix.
*/
if (deliverSignals) {
signalReceiver.messageArrived(topic.substring(topicPrefix.length()), message);
}
}
示例3: mqttCallback
import org.eclipse.paho.client.mqttv3.MqttCallback; //导入依赖的package包/类
protected void mqttCallback() {
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
msg("Connection lost...");
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
TextView tvMessage = (TextView) findViewById(R.id.tvMessage);
tvMessage.setText(message.toString());
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
}
});
}
示例4: addListener
import org.eclipse.paho.client.mqttv3.MqttCallback; //导入依赖的package包/类
/**
* Registers a callback for receiving MQTT messages.
*
* @param topic
* topic to listen for messages.
* @param callback
* listener to register.
*/
public void addListener(final String topic, final MqttCallback callback) {
logger.info("Adding listener {} to topic '{}'", callback.getClass().getSimpleName(), topic);
final String fullTopic = this.topicPrefix + topic;
Collection<MqttCallback> callbacksForTopic = this.listeners.get(fullTopic);
if (callbacksForTopic == null) {
callbacksForTopic = new ArrayList<MqttCallback>();
try {
this.listeners.put(fullTopic, callbacksForTopic);
this.client.subscribe(fullTopic);
logger.info("Subscribed to topic '{}'", fullTopic);
} catch (MqttException e) {
logger.error("Error registering listener", e);
}
}
callbacksForTopic.add(callback);
}
示例5: attachCallback
import org.eclipse.paho.client.mqttv3.MqttCallback; //导入依赖的package包/类
/** Attaches the callback used when configuration changes occur. */
public static void attachCallback(MqttClient client, String deviceId) throws MqttException {
mCallback = new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
// Do nothing...
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
String payload = new String(message.getPayload());
System.out.println("Payload : " + payload);
// TODO: Insert your parsing / handling of the configuration message here.
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
// Do nothing;
}
};
String configTopic = String.format("/devices/%s/config", deviceId);
client.subscribe(configTopic, 1);
client.setCallback(mCallback);
}
示例6: testMqtt
import org.eclipse.paho.client.mqttv3.MqttCallback; //导入依赖的package包/类
@Test
public void testMqtt() throws MqttException, InterruptedException {
final CountDownLatch messageReceived = new CountDownLatch(1);
MqttClient client = new MqttClient(mqttServer.getConnectString(), UUID.randomUUID().toString(), new MemoryPersistence());
client.connect();
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
messageReceived.countDown();
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
}
});
client.subscribe(TOPIC_NAME_TEST);
client.publish(TOPIC_NAME_TEST, UUID.randomUUID().toString().getBytes(), 0, false);
assertTrue(messageReceived.await(MQTT_TIMEOUT_MS, TimeUnit.MILLISECONDS));
client.disconnect();
}
示例7: createCallback
import org.eclipse.paho.client.mqttv3.MqttCallback; //导入依赖的package包/类
private MqttCallback createCallback() {
return new MqttCallback() {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
messageIds.add(message.getId());
messageArrived = true;
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
}
@Override
public void connectionLost(Throwable cause) {
}
};
}
示例8: GUIApplication
import org.eclipse.paho.client.mqttv3.MqttCallback; //导入依赖的package包/类
public GUIApplication() throws MqttException {
empf = builder.uri("tcp://" + BarometerApplication.BROKER + ":1883").clientUID("ch.quantasy.knr1.GUIApplication").build();
empf.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable throwable) {
}
@Override
public void messageArrived(String str, MqttMessage mqttMessage)
throws Exception {
byte[] payload = mqttMessage.getPayload();
ByteBuffer bb = ByteBuffer.wrap(payload);
AltitudeProfileView.addBarometricAltitudeData(bb.getInt());
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(true);
empf.connect(options);
empf.subscribe(BarometerApplication.TOPIC + "+", 0);
}
示例9: subscribe
import org.eclipse.paho.client.mqttv3.MqttCallback; //导入依赖的package包/类
private void subscribe(MqttClient client) throws Exception {
client.subscribe(TOPIC);
client.setCallback(new MqttCallback() {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
setResponse(new String(message.getPayload()));
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
}
@Override
public void connectionLost(Throwable ex) {
setError(ex);
}
});
}
示例10: subscribe
import org.eclipse.paho.client.mqttv3.MqttCallback; //导入依赖的package包/类
private void subscribe(MqttClient client) throws Exception {
client.subscribe(TOPIC);
client.setCallback(new MqttCallback() {
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
System.out.println(new String(message.getPayload()));
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
}
@Override
public void connectionLost(Throwable ex) {
ex.printStackTrace();
}
});
}
示例11: createClient
import org.eclipse.paho.client.mqttv3.MqttCallback; //导入依赖的package包/类
public MqttAndroidClient createClient(String id, String serverURI, String clientId) {
MqttClientPersistence mqttClientPersistence = new MemoryPersistence();
MqttAndroidClient client = new MqttAndroidClient(MyApplication.getContext(), serverURI, clientId, mqttClientPersistence);
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
LogUtil.e("connectionLost");
EventBus.getDefault().post(new MQTTActionEvent(Constant.MQTTStatusConstant.CONNECTION_LOST, null, cause));
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
LogUtil.d("topic is " + topic + ",message is " + message.toString() + ", qos is " + message.getQos());
EventBus.getDefault().postSticky(new MessageEvent(new EmqMessage(topic, message)));
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
LogUtil.d("deliveryComplete");
}
});
mClients.put(id, client);
return client;
}
示例12: setupMqtt
import org.eclipse.paho.client.mqttv3.MqttCallback; //导入依赖的package包/类
private void setupMqtt ( final Hive hive, final Session session ) throws MqttException
{
this.client.setCallback ( new MqttCallback () {
@Override
public void messageArrived ( final String topic, final MqttMessage message ) throws Exception
{
logger.trace ( "received message '{}' on topic '{}'", message, topic );
MqttExporter.this.executor.submit ( new Callable<Void> () {
@Override
public Void call () throws Exception
{
final String itemId = MqttExporter.this.itemsToWriteTopics.inverse ().get ( topic );
if ( itemId != null )
{
writeMessageToItem ( hive, session, itemId, message );
}
else
{
logger.warn ( "received message '{}' on topic '{}' but no corresponding item found", message, topic );
}
return null;
}
} );
}
@Override
public void deliveryComplete ( final IMqttDeliveryToken token )
{
}
@Override
public void connectionLost ( final Throwable th )
{
// TODO: implement this
logger.warn ( "connectionLost", th );
}
} );
}
示例13: itCanSubscribeMultipleMessages
import org.eclipse.paho.client.mqttv3.MqttCallback; //导入依赖的package包/类
@Test
public void itCanSubscribeMultipleMessages() throws Throwable {
AsyncPahoUtils.connect(this.asyncClient);
CountDownLatch latch = new CountDownLatch(4);
AtomicInteger messageCount = new AtomicInteger(0);
// Callback to monitor delivery completion
this.asyncClient.setCallback(new MqttCallback() {
@Override
public void messageArrived(String topic, org.eclipse.paho.client.mqttv3.MqttMessage m) throws Exception {
}
@Override
public void deliveryComplete(IMqttDeliveryToken t) {
latch.countDown();
}
@Override
public void connectionLost(Throwable cause) {
}
});
// Subscribe
this.observableClient.subscribe(TOPIC, 1).subscribe(r -> {
messageCount.incrementAndGet();
latch.countDown();
});
// Publish a test message
AsyncPahoUtils.publish(asyncClient, TOPIC, new byte[] { 'a', 'b', 'c' });
AsyncPahoUtils.publish(asyncClient, TOPIC, new byte[] { 'd', 'e', 'f' });
// Await for async completion
latch.await();
Assert.assertEquals(2, messageCount.get());
}
示例14: setCallback
import org.eclipse.paho.client.mqttv3.MqttCallback; //导入依赖的package包/类
/**
* Set the callback to trigger on message arrival
*
* @param listener the listener implementation
*/
public void setCallback(MessageEventListener listener) {
if (listener == null) {
logger.debug("Clear callback");
getMqttClient().setCallback(null);
return;
}
logger.debug("Set callback");
getMqttClient().setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable thrwbl) {
logger.warn("Connection to MQTT server lost, reconnecting", thrwbl);
if (!getMqttClient().isConnected()) {
mqttClient = null;
getMqttClient();
}
}
@Override
public void messageArrived(String mqttTopic, MqttMessage mqttMessage) throws Exception {
MessageEventListener.Message message = new MessageEventListener.Message();
message.topic = mqttTopic;
message.content = new String(mqttMessage.getPayload());
logger.debug("New message received on {}", message.topic, message.content);
listener.onMessage(message);
}
@Override
public void deliveryComplete(IMqttDeliveryToken imdt) {
}
});
}
示例15: MQTTconect
import org.eclipse.paho.client.mqttv3.MqttCallback; //导入依赖的package包/类
public MQTTconect(Context contexto,MqttCallback mqttcallback) {
//gera um código randômico que serve como identificação do cliente
clientID = MqttClient.generateClientId()+"circularUFPAapp";
//cria um objeto MQTTClient android entregando como parametro o endereço o servidor e o id do cliente
mqttAndroidClient = new MqttAndroidClient(contexto, serverAndress, clientID);
//configura um objeto CallBack (objeto de chamada caso haja alteração)
mqttAndroidClient.setCallback(mqttcallback);
}