MqttConfig config = new MqttConfig(); Properties p = properties; setConfig(p, "mqtt.actionTimeToWaitMillis", val -> config.setActionTimeToWaitMillis(Long.valueOf(val))); setConfig(p, "mqtt.cleanSession", val -> config.setCleanSession(Boolean.valueOf(val))); setConfig(p, "mqtt.clientId", val -> config.setClientId(val)); setConfig(p, "mqtt.connectionTimeoutSec", val -> config.setConnectionTimeout(Integer.valueOf(val))); setConfig(p, "mqtt.idleTimeoutSec", val -> config.setIdleTimeout(Integer.valueOf(val))); setConfig(p, "mqtt.keepAliveSec", val -> config.setKeepAliveInterval(Integer.valueOf(val))); setConfig(p, "mqtt.keyStore", val -> config.setKeyStore(val)); setConfig(p, "mqtt.keyStorePassword", val -> config.setKeyStorePassword(val.toCharArray())); setConfig(p, "mqtt.password", val -> config.setPassword(val.toCharArray())); setConfig(p, "mqtt.persistence", val -> config.setPersistence(newPersistenceProvider(val))); setConfig(p, "mqtt.serverURLs", val -> config.setServerURLs(val.trim().split(","))); setConfig(p, "mqtt.subscriberIdleReconnectIntervalSec", val -> config.setSubscriberIdleReconnectInterval(Integer.valueOf(val))); setConfig(p, "mqtt.trustStore", val -> config.setTrustStore(val));
initVars(); if (mqttConfig == null) { mqttConfig = MqttConfig.fromProperties(properties); mqttConfig.setClientId(clientId);
private MqttClient newClient(MqttConfig config) throws MqttException { String url = config.getServerURLs()[0]; MqttClientPersistence persistence = config.getPersistence(); if (persistence == null) persistence = new MemoryPersistence(); long actionTimeToWaitMillis = config.getActionTimeToWaitMillis(); logger.info("{} server:{} clientId:{} actionTimeToWait:{} persistence:{}", id(), url, clientId, actionTimeToWaitMillis, persistence); MqttClient client = new MqttClient(url, clientId, persistence); client.setTimeToWait(actionTimeToWaitMillis); client.setCallback(new Callback()); return client; }
/** * Create a connector to the specified server. * <p> * A convenience function. * Connecting to the server occurs after the * topology is submitted for execution. * * @param topology the connector's associated {@code Topology}. * @param url URL of MQTT server. * @param clientId the connector's MQTT clientId. auto-generated if null. */ public MqttStreams(Topology topology, String url, String clientId) { this.topology = topology; MqttConfig config = new MqttConfig(); config.setServerURLs(new String[] {url}); config.setClientId(clientId); connector = new MqttConnector(() -> config); }
MqttConfig config = new MqttConfig(); assertEquals(null, config.getClientId()); assertArrayEquals(null, config.getServerURLs()); config = new MqttConfig(getServerURI(), clientId); assertEquals(-1, config.getActionTimeToWaitMillis()); config.setActionTimeToWaitMillis(31); assertEquals(31, config.getActionTimeToWaitMillis()); assertEquals(clientId, config.getClientId()); config.setClientId("xyzzyClient"); assertEquals("xyzzyClient", config.getClientId()); assertTrue(config.isCleanSession()); config.setCleanSession(true); assertTrue(config.isCleanSession()); config.setCleanSession(false); assertFalse(config.isCleanSession()); assertEquals(30, config.getConnectionTimeout()); config.setConnectionTimeout(11); assertEquals(11, config.getConnectionTimeout()); assertEquals(60, config.getKeepAliveInterval()); config.setKeepAliveInterval(12); assertEquals(12, config.getKeepAliveInterval()); assertEquals(null, config.getKeyStore());
() -> ((Long)configRef.get().getActionTimeToWaitMillis()).toString()); propTester.add("mqtt.cleanSession", "false", () -> ((Boolean)configRef.get().isCleanSession()).toString()); propTester.add("mqtt.clientId", "xyzzy-clientId", () -> configRef.get().getClientId()); propTester.add("mqtt.connectionTimeoutSec", "11", () -> ((Integer)configRef.get().getConnectionTimeout()).toString()); propTester.add("mqtt.idleTimeoutSec", "12", () -> ((Integer)configRef.get().getIdleTimeout()).toString()); propTester.add("mqtt.keepAliveSec", "13", () -> ((Integer)configRef.get().getKeepAliveInterval()).toString()); propTester.add("mqtt.keyStore", "some/path/keystore", () -> configRef.get().getKeyStore()); propTester.add("mqtt.keyStorePassword", "some-keystore-password", () -> new String(configRef.get().getKeyStorePassword())); () -> new String(configRef.get().getPassword())); () -> StringUtils.join(configRef.get().getServerURLs(), ",")); propTester.add("mqtt.subscriberIdleReconnectIntervalSec", "14", () -> ((Integer)configRef.get().getSubscriberIdleReconnectInterval()).toString()); propTester.add("mqtt.trustStore", "some/path/truststore", () -> configRef.get().getTrustStore()); propTester.add("mqtt.trustStorePassword", "some-truststore-password", () -> new String(configRef.get().getTrustStorePassword())); propTester.add("mqtt.userName", "xyzzy-username", () -> configRef.get().getUserName()); () -> {
private MqttConfig newConfig(String serverURL, String clientId) { MqttConfig config = new MqttConfig(serverURL, clientId); if (authInfo.get("userID") != null) config.setUserName(authInfo.get("userID")); if (authInfo.get("password") != null) config.setPassword(authInfo.get("password").toCharArray()); if (authInfo.get("trustStore") != null) config.setTrustStore(authInfo.get("trustStore")); if (authInfo.get("trustStorePassword") != null) config.setTrustStorePassword(authInfo.get("trustStorePassword").toCharArray()); if (authInfo.get("keyStore") != null) config.setKeyStore(authInfo.get("keyStore")); if (authInfo.get("keyStorePassword") != null) config.setKeyStorePassword(authInfo.get("keyStorePassword").toCharArray()); // if (authInfo.get("keyPassword") != null) // config.setKeyPassword(authInfo.get("keyPassword").toCharArray()); // if (authInfo.get("keyCertificateAlias") != null) // config.setKeyCertificateAlias(authInfo.get("keyCertificateAlias")); return config; }
return client; MqttConnectOptions options = (MqttConnectOptions) config.options(); options.getUserName(), options.getPassword() == null ? null : "*****", config.getIdleTimeout(), config.getSubscriberIdleReconnectInterval(), options.getConnectionTimeout(), options.getKeepAliveInterval(), setIdleTimeout(config.getIdleTimeout(), TimeUnit.SECONDS); setIdleReconnectInterval(config.getSubscriberIdleReconnectInterval()); sub.connected(client);
/** * 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
@Test public void testActionTime() throws Exception { Topology top = newTopology("testActionTime"); MsgGenerator mgen = new MsgGenerator(top.getName()); int qos = 0; boolean retain = false; String clientId = newClientId(top.getName()); String topic = getMqttTopics()[0]; List<String> msgs = createMsgs(mgen, topic, getMsg1(), getMsg2()); TStream<String> s = PlumbingStreams.blockingOneShotDelay( top.collection(msgs), PUB_DELAY_MSEC, TimeUnit.MILLISECONDS); // Test publish(TStream<String>, topic, qos) // Test TStream<String> subscribe(topic, qos) MqttConfig config = newConfig(getServerURI(), clientId); config.setActionTimeToWaitMillis(3*1000); MqttStreams mqtt = new MqttStreams(top, () -> config); mqtt.publish(s, topic, qos, retain); TStream<String> rcvd = mqtt.subscribe(topic, qos); completeAndValidate(clientId, top, rcvd, mgen, SEC_TIMEOUT, msgs.toArray(new String[0])); }