options.setKeepAliveInterval( Integer.parseInt( keepAliveInterval ) );
/** * Set the keepAliveInterval. * @param keepAliveInterval the keepAliveInterval to set. * @deprecated use {@link #setConnectionOptions(MqttConnectOptions)} instead. */ @Deprecated public void setKeepAliveInterval(Integer keepAliveInterval) { this.options.setKeepAliveInterval(keepAliveInterval); }
protected void onScheduled(final ProcessContext context){ broker = context.getProperty(PROP_BROKER_URI).getValue(); clientID = context.getProperty(PROP_CLIENTID).getValue(); connOpts = new MqttConnectOptions(); connOpts.setCleanSession(context.getProperty(PROP_CLEAN_SESSION).asBoolean()); connOpts.setKeepAliveInterval(context.getProperty(PROP_KEEP_ALIVE_INTERVAL).asInteger()); connOpts.setMqttVersion(context.getProperty(PROP_MQTT_VERSION).asInteger()); connOpts.setConnectionTimeout(context.getProperty(PROP_CONN_TIMEOUT).asInteger()); PropertyValue sslProp = context.getProperty(PROP_SSL_CONTEXT_SERVICE); if (sslProp.isSet()) { Properties sslProps = transformSSLContextService((SSLContextService) sslProp.asControllerService()); connOpts.setSSLProperties(sslProps); } PropertyValue lastWillTopicProp = context.getProperty(PROP_LAST_WILL_TOPIC); if (lastWillTopicProp.isSet()){ String lastWillMessage = context.getProperty(PROP_LAST_WILL_MESSAGE).getValue(); PropertyValue lastWillRetain = context.getProperty(PROP_LAST_WILL_RETAIN); Integer lastWillQOS = context.getProperty(PROP_LAST_WILL_QOS).asInteger(); connOpts.setWill(lastWillTopicProp.getValue(), lastWillMessage.getBytes(), lastWillQOS, lastWillRetain.isSet() ? lastWillRetain.asBoolean() : false); } PropertyValue usernameProp = context.getProperty(PROP_USERNAME); if(usernameProp.isSet()) { connOpts.setUserName(usernameProp.getValue()); connOpts.setPassword(context.getProperty(PROP_PASSWORD).getValue().toCharArray()); } }
opts.setKeepAliveInterval(keepAlive); opts.setConnectionTimeout(timeout);
private MqttConnectOptions optionsFromModel(ConnectionModel model){ MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(model.isCleanSession()); connOpts.setConnectionTimeout(model.getTimeout()); connOpts.setKeepAliveInterval(model.getKeepAlive()); if(!model.getUsername().equals(ActivityConstants.empty)){ connOpts.setUserName(model.getUsername()); } if(!model.getPassword().equals(ActivityConstants.empty)){ connOpts.setPassword(model.getPassword().toCharArray()); } if(!model.getLwtTopic().equals(ActivityConstants.empty) && !model.getLwtMessage().equals(ActivityConstants.empty)){ connOpts.setWill(model.getLwtTopic(), model.getLwtMessage().getBytes(), model.getLwtQos(), model.isLwtRetain()); } // if(tlsConnection){ // // TODO Add Keys to conOpts here // //connOpts.setSocketFactory(); // } return connOpts; }
/** * Connection Keep alive. * <p> * Optional. 0 disables keepalive processing. default: 60 seconds. * @param keepAliveSec the interval in seconds */ public void setKeepAliveInterval(int keepAliveSec) { options.setKeepAliveInterval(keepAliveSec); }
connectOptions.setCleanSession(false); connectOptions.setConnectionTimeout(23); connectOptions.setKeepAliveInterval(45); connectOptions.setPassword("pass".toCharArray()); MemoryPersistence persistence = new MemoryPersistence();
options.setKeepAliveInterval(userKeepAlive);
connectOptions.setCleanSession(false); connectOptions.setConnectionTimeout(23); connectOptions.setKeepAliveInterval(45); connectOptions.setPassword("pass".toCharArray()); MemoryPersistence persistence = new MemoryPersistence();
options.setKeepAliveInterval(userKeepAlive);
connectOptions.setCleanSession(false); connectOptions.setConnectionTimeout(23); connectOptions.setKeepAliveInterval(45); connectOptions.setPassword("pass".toCharArray()); MemoryPersistence persistence = new MemoryPersistence();
... MqttConnectOptions options = new MqttConnectOptions(); options.setCleanSession(true); options.setKeepAliveInterval(30); ...
String userName="Ohelig"; String password="Pojke"; MqttClient client = new MqttClient("tcp://192.168.1.4:1883","Sending"); MqttConnectOptions authen = new MqttConnectOptions(); authen.setKeepAliveInterval(30); authen.setUserName(userName); authen.setPassword(password.toCharArray()); authen.setKeepAliveInterval(30); authen.setConnectionTimeout(300); client.connect(authen);
/** * Generates the connection options for the mqtt broker connection. * * @param userName the user name for the mqtt broker connection. * @param userPassword the user password for the mqtt broker connection. */ private void updateConnectionOptions(String userName, String userPassword) { this.connectionOptions.setKeepAliveInterval(keepAliveInterval); this.connectionOptions.setCleanSession(setCleanSession); this.connectionOptions.setMqttVersion(mqttVersion); this.connectionOptions.setUserName(userName); this.connectionOptions.setPassword(userPassword.toCharArray()); } }
@PostConstruct private void init() { Properties config = this.configManager.getConfigurationWithDefaults(CONFIG_NAME); String brokerAddress = config.getProperty(KEY_BROKER_ADDRESS); String persitanceLocation = config.getProperty(KEY_PERSITENCE_LOCATION); Path persistencePath = this.env.getWorkingDirectory().resolve(persitanceLocation); try { this.client = new MqttAsyncClient(brokerAddress, this.info.getNodeId() + "-MainJavaApp", new MqttDefaultFilePersistence(persistencePath.toAbsolutePath().toString())); this.client.setCallback(this); } catch (MqttException e) { throw new IllegalStateException("Failed to initialize mqtt client", e); } this.options = new MqttConnectOptions(); this.options.setCleanSession(false); this.options.setConnectionTimeout((int) CONNECTION_TIMEOUT.getSeconds()); this.options.setKeepAliveInterval((int) KEEP_ALIVE_INTERVAL.getSeconds()); }
public ExternalMqttClient(String url, String clientId, String username, String password, TRUST_HOST_TYPE trustHostType) { try { MqttDefaultFilePersistence myPersistence = new MqttDefaultFilePersistence(AppProperties.getInstance() .getMqttClientPersistentStoresLocation()); mqttClient = new MqttClient(url, clientId + "_" + RandomStringUtils.randomAlphanumeric(5), myPersistence); connectOptions.setConnectionTimeout(CONNECTION_TIME_OUT); connectOptions.setKeepAliveInterval(KEEP_ALIVE); if (username != null && password.length() > 0) { connectOptions.setUserName(username); connectOptions.setPassword(password.toCharArray()); } mqttClient.connect(connectOptions); } catch (Exception ex) { _logger.error("Exception, ", ex); } }
public ExternalMqttClient(String url, String clientId, String username, String password, TRUST_HOST_TYPE trustHostType) { try { MqttDefaultFilePersistence myPersistence = new MqttDefaultFilePersistence(AppProperties.getInstance() .getMqttClientPersistentStoresLocation()); mqttClient = new MqttClient(url, clientId + "_" + RandomStringUtils.randomAlphanumeric(5), myPersistence); connectOptions.setConnectionTimeout(CONNECTION_TIME_OUT); connectOptions.setKeepAliveInterval(KEEP_ALIVE); if (username != null && password.length() > 0) { connectOptions.setUserName(username); connectOptions.setPassword(password.toCharArray()); } mqttClient.connect(connectOptions); } catch (Exception ex) { _logger.error("Exception, ", ex); } }
private static MqttClient connect(String clientId, String userName, String password) throws MqttException { MemoryPersistence persistence = new MemoryPersistence(); MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(true); connOpts.setUserName(userName); connOpts.setPassword(password.toCharArray()); connOpts.setConnectionTimeout(10); connOpts.setKeepAliveInterval(20); SSLSocketFactory socketFactory = SecureSocketSslContextFactory.getClientContext().getSocketFactory(); connOpts.setSocketFactory(socketFactory); connOpts.setWill("/test","haha".getBytes(),0,false); // String[] uris = {"tcp://10.100.124.206:1883","tcp://10.100.124.207:1883"}; // connOpts.setServerURIs(uris); //起到负载均衡和高可用的作用 MqttClient mqttClient = new MqttClient(broker, clientId, persistence); mqttClient.setCallback(new PushCallback("test")); mqttClient.connect(connOpts); return mqttClient; }
private static MqttClient connect(String clientId, String userName, String password) throws MqttException { MemoryPersistence persistence = new MemoryPersistence(); MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(false); connOpts.setUserName(userName); connOpts.setPassword(password.toCharArray()); connOpts.setConnectionTimeout(10); connOpts.setKeepAliveInterval(20); SSLSocketFactory socketFactory = SecureSocketSslContextFactory.getClientContext().getSocketFactory(); connOpts.setSocketFactory(socketFactory); // String[] uris = {"tcp://10.100.124.206:1883","tcp://10.100.124.207:1883"}; // connOpts.setServerURIs(uris); //起到负载均衡和高可用的作用 MqttClient mqttClient = new MqttClient(broker, clientId, persistence); mqttClient.setCallback(new PushCallback("test")); mqttClient.connect(connOpts); return mqttClient; }
private static MqttClient connect(String clientId, String userName, String password) throws MqttException { MemoryPersistence persistence = new MemoryPersistence(); MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(true); connOpts.setUserName(userName); connOpts.setPassword(password.toCharArray()); connOpts.setConnectionTimeout(10); connOpts.setKeepAliveInterval(20); // SSLSocketFactory socketFactory = SecureSocketSslContextFactory.getClientContext().getSocketFactory(); // connOpts.setSocketFactory(socketFactory); // String[] uris = {"tcp://10.100.124.206:1883","tcp://10.100.124.207:1883"}; // connOpts.setServerURIs(uris); //起到负载均衡和高可用的作用 MqttClient mqttClient = new MqttClient(broker, clientId, persistence); mqttClient.setCallback(new PushCallback("test1")); mqttClient.connect(connOpts); return mqttClient; }