public void disconnect(long quiesceTimeout) throws MqttException { aClient.disconnect(quiesceTimeout, null, null).waitForCompletion(); }
public void subscribe(String[] topicFilters, int[] qos) throws MqttException { IMqttToken tok = aClient.subscribe(topicFilters, qos, null, null); tok.waitForCompletion(getTimeToWait()); int[] grantedQos = tok.getGrantedQos(); for (int i = 0; i < grantedQos.length; ++i) { qos[i] = grantedQos[i]; } if (grantedQos.length == 1 && qos[0] == 0x80) { throw new MqttException(MqttException.REASON_CODE_SUBSCRIBE_FAILED); } }
@Override public MqttWireMessage getResponse() { return delegate.getResponse(); }
/** * 异步客户端 */ @IocBean(name = "mqttAsyncClient", depose = "close") public MqttAsyncClient createMqttAsyncClient(@Inject MqttConnectOptions mqttConnectOptions, @Inject MqttClientPersistence mqttClientPersistence) throws MqttException { String clientId = conf.get(PROP_CLIENT_ID); if (Strings.isBlank(clientId)) { clientId = MqttClient.generateClientId(); } log.info("Client Id = " + clientId); MqttAsyncClient client = new MqttAsyncClient(conf.get(PROP_URL, "tcp://127.0.0.1:1883"), clientId, mqttClientPersistence); if (ioc.has("mqttCallback")) { client.setCallback(ioc.get(MqttCallback.class, "mqttCallback")); } if (conf.getBoolean(PROP_CONNECT_ON_START, true)) { IMqttToken token = client.connect(mqttConnectOptions, null, null); token.waitForCompletion(conf.getLong(PROP_TIME_TO_WAIT, -1)); if (token.getException() != null) throw token.getException(); } return client; } }
@Override public void onSuccess(IMqttToken token) { final AWSIotMessage message = (AWSIotMessage) token.getUserContext(); if (message == null) { return; } boolean forceFailure = false; if (token.getResponse() instanceof MqttSuback) { MqttSuback subAck = (MqttSuback) token.getResponse(); int qos[] = subAck.getGrantedQos(); for (int i = 0; i < qos.length; i++) { if (qos[i] == SUB_ACK_RETURN_CODE_FAILURE) { LOGGER.warning("Request failed: likely due to too many subscriptions or policy violations"); forceFailure = true; break; } } } final boolean isSuccess = !forceFailure; client.scheduleTask(new Runnable() { @Override public void run() { if (isSuccess) { message.onSuccess(); } else { message.onFailure(); } } }); }
@Override public void onFailure(IMqttToken token, Throwable cause) { final AWSIotMessage message = (AWSIotMessage) token.getUserContext(); if (message == null) { LOGGER.warning("Request failed: " + token.getException()); return; } LOGGER.warning("Request failed for topic " + message.getTopic() + ": " + token.getException()); client.scheduleTask(new Runnable() { @Override public void run() { message.onFailure(); } }); }
@Override public int[] getGrantedQos() { return delegate.getGrantedQos(); }
Object service = myContext.startService(serviceStartIntent); if (service == null) { IMqttActionListener listener = token.getActionCallback(); if (listener != null) { listener.onFailure(token, new RuntimeException(
@Override public void onFailure(@Nullable IMqttToken token, @Nullable Throwable error) { cancelTimeoutFuture.run(); final Throwable throwable = (token != null && token.getException() != null) ? token.getException() : error; final MqttConnectionState connectionState = connection.connectionState(); future.complete(false); connection.connectionObservers.forEach(o -> o.connectionStateChanged(connectionState, throwable)); // If we tried to connect via start(), use the reconnect strategy to try it again if (connection.isConnecting) { connection.isConnecting = false; if (connection.reconnectStrategy != null) { connection.reconnectStrategy.lostConnection(); } } }
@Override public void onSuccess(IMqttToken token) { final AWSIotMessage message = (AWSIotMessage) token.getUserContext(); if (message == null) { return; } boolean forceFailure = false; if (token.getResponse() instanceof MqttSuback) { MqttSuback subAck = (MqttSuback) token.getResponse(); int qos[] = subAck.getGrantedQos(); for (int i = 0; i < qos.length; i++) { if (qos[i] == SUB_ACK_RETURN_CODE_FAILURE) { LOGGER.warning("Request failed: likely due to too many subscriptions or policy violations"); forceFailure = true; break; } } } final boolean isSuccess = !forceFailure; client.scheduleTask(new Runnable() { @Override public void run() { if (isSuccess) { message.onSuccess(); } else { message.onFailure(); } } }); }
@Override public void onFailure(IMqttToken token, Throwable cause) { final AWSIotMessage message = (AWSIotMessage) token.getUserContext(); if (message == null) { LOGGER.warning("Request failed: " + token.getException()); return; } LOGGER.warning("Request failed for topic " + message.getTopic() + ": " + token.getException()); client.scheduleTask(new Runnable() { @Override public void run() { message.onFailure(); } }); }
@Override public int[] getGrantedQos() { return delegate.getGrantedQos(); }
/** * Actually do the mqtt connect operation */ private void doConnect() { if (clientHandle == null) { clientHandle = mqttService.getClient(serverURI, clientId,myContext.getApplicationInfo().packageName, persistence); } mqttService.setTraceEnabled(traceEnabled); mqttService.setTraceCallbackId(clientHandle); String activityToken = storeToken(connectToken); try { mqttService.connect(clientHandle, connectOptions, null, activityToken); } catch (MqttException e) { IMqttActionListener listener = connectToken.getActionCallback(); if (listener != null) { listener.onFailure(connectToken, e); } } }
/** * 同步阻塞客户端, 然后它不支持通过MqttAsyncClient来构建,真蛋疼 */ @IocBean(name = "mqttClient", depose = "close") public MqttClient createMqttClient(@Inject MqttConnectOptions mqttConnectOptions, @Inject MqttClientPersistence mqttClientPersistence) throws MqttException { String clientId = conf.get(PROP_CLIENT_ID); if (Strings.isBlank(clientId)) { clientId = MqttClient.generateClientId(); } log.info("Client Id = " + clientId); MqttClient client = new MqttClient(conf.get(PROP_URL, "tcp://127.0.0.1:1883"), clientId, mqttClientPersistence); if (ioc.has("mqttCallback")) { client.setCallback(ioc.get(MqttCallback.class, "mqttCallback")); } client.setTimeToWait(conf.getLong(PROP_TIME_TO_WAIT, -1)); if (conf.getBoolean(PROP_CONNECT_ON_START, true)) { IMqttToken token = client.connectWithResult(mqttConnectOptions); if (token.getException() != null) throw token.getException(); } return client; }
public void disconnect() throws MqttException { aClient.disconnect().waitForCompletion(); }
public void subscribe(String[] topicFilters, int[] qos) throws MqttException { IMqttToken tok = aClient.subscribe(topicFilters, qos, null, null); tok.waitForCompletion(getTimeToWait()); int[] grantedQos = tok.getGrantedQos(); for (int i = 0; i < grantedQos.length; ++i) { qos[i] = grantedQos[i]; } if (grantedQos.length == 1 && qos[0] == 0x80) { throw new MqttException(MqttException.REASON_CODE_SUBSCRIBE_FAILED); } }
@Override public MqttWireMessage getResponse() { return delegate.getResponse(); }
@Override public int[] getGrantedQos() { return delegate.getGrantedQos(); }
Object service = myContext.startService(serviceStartIntent); if (service == null) { IMqttActionListener listener = token.getActionCallback(); if (listener != null) { listener.onFailure(token, new RuntimeException(
@Override protected void doStop() { try { IMqttAsyncClient theClient = this.client; if (theClient != null) { theClient.disconnect().waitForCompletion(this.completionTimeout); theClient.close(); this.client = null; } } catch (MqttException e) { logger.error("Failed to disconnect", e); } }