public MQTTPublisher(String address, String clientId, boolean cleanSession, int qos) throws MqttException { this.clientId = clientId; this.qos = qos; this.client = new MqttClient(address, clientId, persistence); MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(cleanSession); this.client.connect(); }
static void checkVersion( List<CheckResultInterface> remarks, StepMeta stepMeta, VariableSpace space, String value ) { String version = space.environmentSubstitute( value ); if ( !StringUtil.isEmpty( version ) ) { try { ( new MqttConnectOptions() ).setMqttVersion( Integer.parseInt( version ) ); } catch ( Exception e ) { remarks.add( new CheckResult( CheckResultInterface.TYPE_RESULT_ERROR, getString( PKG, "MQTTMeta.CheckResult.NotCorrectVersion", getString( PKG, "MQTTDialog.Options." + MQTT_VERSION ), version ), stepMeta ) ); } } } }
MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(true); log.info("Connecting to broker: " + broker);
MqttConnectOptions connectOptions = new MqttConnectOptions(); connectOptions.setCleanSession(true); log.info("Connecting to broker: " + broker);
public MQTTHandler(NamedIP aConfig) { super(); log.info("Setting up handler for name: " + aConfig.getName()); MemoryPersistence persistence = new MemoryPersistence(); myConfig = aConfig; try { myClient = new MqttClient("tcp://" + myConfig.getIp(), myConfig.getName(), persistence); } catch (MqttException e) { log.error("Could not create MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage()); } MqttConnectOptions connOpts = new MqttConnectOptions(); connOpts.setCleanSession(true); connOpts.setAutomaticReconnect(true); if(aConfig.getUsername() != null && aConfig.getUsername().trim().length() > 0) { if(aConfig.getPassword() != null && aConfig.getPassword().trim().length() > 0) { connOpts.setUserName(aConfig.getUsername().trim()); connOpts.setPassword(aConfig.getPassword().trim().toCharArray()); } } try { myClient.connect(connOpts); } catch (MqttException e) { log.error("Could not connect MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage()); } }
private MqttConnectOptions getOptions() { MqttConnectOptions options = new MqttConnectOptions();
@Test public void connectWithValidCredentials() throws Exception { LOG.info("*** connectWithCredentials ***"); m_client = new MqttClient("tcp://localhost:1883", "Publisher", s_pubDataStore); MqttConnectOptions options = new MqttConnectOptions(); options.setUserName("dbuser"); options.setPassword("password".toCharArray()); m_client.connect(options); assertTrue(true); }
@Test(expected = MqttException.class) public void checkClientAuthenticationFail() throws Exception { LOG.info("*** checkClientAuthenticationFail ***"); SSLSocketFactory ssf = configureSSLSocketFactory("unsignedclientkeystore.jks"); MqttConnectOptions options = new MqttConnectOptions(); options.setSocketFactory(ssf); // actual a "Broken pipe" is thrown, this is not very specific. try { m_client.connect(options); } catch (MqttException e) { e.printStackTrace(); throw e; } }
@Test public void checkSupportSSL() throws Exception { LOG.info("*** checkSupportSSL ***"); SSLSocketFactory ssf = configureSSLSocketFactory(); MqttConnectOptions options = new MqttConnectOptions(); options.setSocketFactory(ssf); m_client.connect(options); m_client.subscribe("/topic", 0); m_client.disconnect(); }
@Test public void checkClientAuthentication() throws Exception { LOG.info("*** checkClientAuthentication ***"); SSLSocketFactory ssf = configureSSLSocketFactory("signedclientkeystore.jks"); MqttConnectOptions options = new MqttConnectOptions(); options.setSocketFactory(ssf); m_client.connect(options); m_client.subscribe("/topic", 0); m_client.disconnect(); }
@Test public void checkSupportSSLForMultipleClient() throws Exception { LOG.info("*** checkSupportSSLForMultipleClient ***"); SSLSocketFactory ssf = configureSSLSocketFactory(); MqttConnectOptions options = new MqttConnectOptions(); options.setSocketFactory(ssf); m_client.connect(options); m_client.subscribe("/topic", 0); MqttClient secondClient = new MqttClient("ssl://localhost:8883", "secondTestClient", new MemoryPersistence()); MqttConnectOptions secondClientOptions = new MqttConnectOptions(); secondClientOptions.setSocketFactory(ssf); secondClient.connect(secondClientOptions); secondClient.publish("/topic", new MqttMessage("message".getBytes(UTF_8))); secondClient.disconnect(); m_client.disconnect(); }
@Test public void connectWithWrongCredentials() { LOG.info("*** connectWithWrongCredentials ***"); try { m_client = new MqttClient("tcp://localhost:1883", "Publisher", s_pubDataStore); MqttConnectOptions options = new MqttConnectOptions(); options.setUserName("dbuser"); options.setPassword("wrongPassword".toCharArray()); m_client.connect(options); } catch (MqttException e) { if (e instanceof MqttSecurityException) { assertTrue(true); return; } else { assertTrue(e.getMessage(), false); return; } } assertTrue("must not be connected. cause : wrong password given to client", false); }
@Ignore("This test hasn't any meaning using in memory storage service") @Test public void testCleanSession_maintainClientSubscriptions_withServerRestart() throws Exception { LOG.info("*** testCleanSession_maintainClientSubscriptions_withServerRestart ***"); MqttConnectOptions options = new MqttConnectOptions(); options.setCleanSession(false); m_client.connect(options); m_client.subscribe("/topic", 0); m_client.disconnect(); m_server.stopServer(); m_server.startServer(IntegrationUtils.prepareTestProperties()); // reconnect and publish m_client.connect(options); m_client.publish("/topic", "Test my payload".getBytes(UTF_8), 0, false); assertEquals("/topic", m_messagesCollector.getTopic()); }
public IMqttToken connect(Object userContext, IMqttActionListener callback) throws MqttException, MqttSecurityException { return this.connect(new MqttConnectOptions(), userContext, callback); }
public void connect() throws MqttSecurityException, MqttException { this.connect(new MqttConnectOptions()); }
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()); } }
LOG.info("*** shouldNotInternalPublishOnReadBlockedSubscriptionTopic ***"); MqttConnectOptions options = new MqttConnectOptions(); options.setCleanSession(false); m_client.connect(options);
@Bean public DefaultMqttPahoClientFactory pahoClientFactory() { DefaultMqttPahoClientFactory pahoClientFactory = new DefaultMqttPahoClientFactory(); MqttConnectOptions connectionOptions = new MqttConnectOptions(); connectionOptions.setServerURIs(new String[] { "tcp://localhost:1883" }); pahoClientFactory.setConnectionOptions(connectionOptions); return pahoClientFactory; }
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; }
private MqttPahoMessageHandler buildAdapterOut(final IMqttAsyncClient client) throws MqttException { DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory() { @Override public IMqttAsyncClient getAsyncClientInstance(String uri, String clientId) throws MqttException { return client; } }; MqttConnectOptions connectOptions = new MqttConnectOptions(); connectOptions.setServerURIs(new String[] { "tcp://localhost:1883" }); factory.setConnectionOptions(connectOptions); MqttPahoMessageHandler adapter = new MqttPahoMessageHandler("client", factory); adapter.setDefaultTopic("foo"); adapter.setApplicationEventPublisher(mock(ApplicationEventPublisher.class)); adapter.afterPropertiesSet(); return adapter; }