clientId = MqttClient.generateClientId();
public MQTTConnection(String location) { this(MqttClient.generateClientId(), location); }
/** * Create a new connector. * * @param config connector configuration. */ public MqttConnector(Supplier<MqttConfig> config) { this.configFn = config; String cid = configFn.get().getClientId(); if (cid == null) cid = MqttClient.generateClientId(); clientId = cid; } @Override
String newClientID = clientId; if (newClientID == null) { newClientID = MqttClient.generateClientId(); } else if (newClientID.length() > 65535) { throw new IllegalArgumentException("Client ID cannot be longer than 65535 characters");
@Override public boolean initialize() { TreeMap<String, String> params = getVirtualSensorConfiguration().getMainClassInitialParams(); serverURI = params.get("uri"); if (serverURI == null) { logger.error("Parameter uri not provided in Virtual Sensor file."); return false; } clientID = params.get("client_id"); if (clientID == null) { logger.warn("Parameter client_id not provided in Virtual Sensor file, using random one."); clientID = MqttClient.generateClientId(); } topic = params.get("topic"); if (topic == null) { logger.warn("Parameter topic not provided in Virtual Sensor file, using virtual sensor name."); topic = getVirtualSensorConfiguration().getName(); } try{ client = new MqttClient(serverURI, clientID); options.setAutomaticReconnect(true); client.connect(options); }catch (Exception e){ logger.error("Unable to connect to MQTT server.", e); return false; } return true; }
MqttClient.generateClientId(), new MemoryPersistence() );
public void connect(ConnectionCallback connectionCallback) throws ConnectionUnavailableException { try { MqttDefaultFilePersistence persistence = new MqttDefaultFilePersistence(); if (clientId.equals(MqttConstants.EMPTY_STRING)) { clientId = MqttClient.generateClientId(); } client = new MqttClient(brokerURL, clientId, persistence); MqttConnectOptions connectionOptions = new MqttConnectOptions(); connectionOptions.setUserName(userName); connectionOptions.setPassword(userPassword.toCharArray()); connectionOptions.setCleanSession(cleanSession); connectionOptions.setKeepAliveInterval(keepAlive); connectionOptions.setConnectionTimeout(connectionTimeout); client.connect(connectionOptions); int qos = Integer.parseInt(String.valueOf(qosOption)); mqttConsumer.subscribe(topicOption, qos, client); } catch (MqttException e) { throw new ConnectionUnavailableException( "Error while connecting with the Mqtt server. Check the url = " + brokerURL + " defined in Siddhi App: " + siddhiAppName , e); } }
public MqttTestClient(String mqttUrl) throws MqttException { mqttClient = new MqttClient(mqttUrl, MqttClient.generateClientId(), new MemoryPersistence()); mqttClient.setCallback(this); MqttConnectOptions con_opts = new MqttConnectOptions(); con_opts.setAutomaticReconnect(true); con_opts.setCleanSession(true); mqttClient.connect(con_opts); Logger.debug("Connected to {}", mqttUrl); lock = new ReentrantLock(); conditions = new HashMap<>(); responses = new HashMap<>(); // Subscribe Logger.debug("Subscribing to {}...", MqttProviderConstants.RESPONSE_TOPIC); mqttClient.subscribe(MqttProviderConstants.RESPONSE_TOPIC); Logger.debug("Subscribed"); }
@Override public void connect() throws ConnectionUnavailableException { try { MqttDefaultFilePersistence persistence = new MqttDefaultFilePersistence(); if (clientId.isEmpty()) { clientId = MqttClient.generateClientId(); } client = new MqttClient(brokerURL, clientId, persistence); MqttConnectOptions connectionOptions = new MqttConnectOptions(); connectionOptions.setUserName(userName); connectionOptions.setPassword(userPassword.toCharArray()); connectionOptions.setCleanSession(cleanSession); connectionOptions.setKeepAliveInterval(keepAlive); connectionOptions.setConnectionTimeout(connectionTimeout); client.connect(connectionOptions); } catch (MqttException e) { throw new ConnectionUnavailableException( "Error while connecting with the Mqtt server, Check the broker url = " + brokerURL + " defined in " + streamDefinition, e); } }
MqttClient client = new MqttClient(brokerUrl, MqttClient.generateClientId()); client.connect(); client.disconnect(); } else { MqttClient client = new MqttClient(brokerUrl, MqttClient.generateClientId()); MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setUserName(username);
@Test @Ignore public void clientTopicListener() throws Exception { MqttClient client = new MqttClient(BROKER_URI, MqttClient.generateClientId(), new MemoryPersistence()); MqttConnectOptions options = new MqttConnectOptions(); options.setUserName(BROKER_USER); options.setPassword(BROKER_PASSWORD.toCharArray()); client.setCallback(new MqttCallback() { @Override public void connectionLost(Throwable cause) { } @Override public void messageArrived(String topic, MqttMessage message) throws Exception { String fileName = topicTranslator(topic); writeMessageToFile(fileName, message.getPayload()); } @Override public void deliveryComplete(IMqttDeliveryToken token) { } }); client.connect(options); client.subscribe(EDC_TOPIC_FILTER, DEFAULT_QOS); Thread.sleep(WAIT_FOR_MESSAGE_MILLIS); }
/** * Prepare client and server part of mocked mqtt. */ private void mqttClientSetup() { /* * mqttClient is meant to simulate Kura device for sending messages, * while subscribedClient is meant to receive messages from Kura device side. */ try { mqttClient = new MqttClient(BROKER_URI, CLIENT_ID, new MemoryPersistence()); subscribedClient = new MqttClient(BROKER_URI, MqttClient.generateClientId(), new MemoryPersistence()); } catch (MqttException e) { e.printStackTrace(); } subscribedClient.setCallback(this); }
: MqttClient.generateClientId();
super.setConfiguration(settings); serverURI = Utils.getString("mqtt-server-url", settings, "tcp://localhost:1883"); clientid = Utils.getString("mqtt-clientid", settings, MqttClient.generateClientId()); version = Utils.getInt("mqtt-version", settings, MqttConnectOptions.MQTT_VERSION_DEFAULT); topic = Utils.getString("mqtt-topic", settings, topic);
mqttClient = new MqttClient(broker, MqttClient.generateClientId(), persistence); MqttConnectOptions opts = new MqttConnectOptions(); opts.setMaxInflight(200);
client = new MqttAsyncClient( MqttClient.generateClientId(), //ClientId new MemoryPersistence()); //Persistence MqttConnectOptions options = new MqttConnectOptions();
String clientId = cf.getClientId(); if (clientId == null) { clientId = MqttClient.generateClientId();
String clientId = aspec.getClientId(); if (clientId == null) { clientId = MqttClient.generateClientId();
/** * 同步阻塞客户端, 然后它不支持通过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; }
/** * 异步客户端 */ @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; } }