Java Code Examples for org.eclipse.paho.client.mqttv3.MqttConnectOptions#setServerURIs()

The following examples show how to use org.eclipse.paho.client.mqttv3.MqttConnectOptions#setServerURIs() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MqttAutoConfiguration.java    From mqtt-spring-boot with MIT License 6 votes vote down vote up
@Bean
public DefaultMqttPahoClientFactory clientFactory() {

    MqttConnectOptions connectOptions = new MqttConnectOptions();
    String username = mqttProperties.getUsername();
    String password = mqttProperties.getPassword();
    if(username != null) {
        connectOptions.setUserName(username);
    }
    if (password != null) {
        connectOptions.setPassword(password.toCharArray());
    }
    String[] serverURIs = mqttProperties.getServerURIs();
    if (serverURIs == null || serverURIs.length == 0) {
        throw new NullPointerException("serverURIs can not be null");
    }
    connectOptions.setCleanSession(mqttProperties.getCleanSession());
    connectOptions.setKeepAliveInterval(mqttProperties.getKeepAliveInterval());
    connectOptions.setServerURIs(serverURIs);

    DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
    factory.setConnectionOptions(connectOptions);

    return factory;
}
 
Example 2
Source File: VenvyMqttClientHelper.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
private MqttConnectOptions initMqttOption() {
    // MQTT的连接设置

    MqttConnectOptions connectOption = new MqttConnectOptions();// MQTT参数
    // 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接
    connectOption.setCleanSession(false);
    // 设置连接的用户名
    connectOption.setUserName(socketKey);
    connectOption.setServerURIs(new String[]{serverUrl});
    // 设置连接的密码
    connectOption.setPassword(socketPassword.toCharArray());
    // 设置超时时间 单位为秒
    connectOption.setConnectionTimeout(15);
    // 设置会话心跳时间 单位为秒 服务器会每隔2*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制
    connectOption.setKeepAliveInterval(40);
    return connectOption;
}
 
Example 3
Source File: AbstractMqttClient.java    From xian with Apache License 2.0 6 votes vote down vote up
/**
 * 连接mqtt server,并返回一个客户端对象,如果连接失败,那么返回null
 */
public MqttAsyncClient connectBroker() {
    LOG.info(String.format("mqtt=======客户端%s与rabbitMQ server: %s 准备建立连接,userName = %s", getMqttClientId(), JSON.toJSONString(serverURIs), userName));
    try {
        sampleClient = new MqttAsyncClient("tcp://overriddenByMqttConnectOptions.setServerURIs:1883", getMqttClientId(), persistence);
        connOpts = new MqttConnectOptions();
        connOpts.setAutomaticReconnect(true);
        connOpts.setServerURIs(serverURIs);
        connOpts.setUserName(userName);
        connOpts.setPassword(getPwd());
        connOpts.setCleanSession(cleanSession);
        connOpts.setMaxInflight(1000 /**默认的值是10,对于我们来说这个值太小!*/);
        connOpts.setKeepAliveInterval(keepAliveInterval);
        sampleClient.setCallback(getCallback(this));
        sampleClient.connect(connOpts).waitForCompletion(60 * 1000);
        LOG.info(String.format("mqtt=======客户端%s与rabbitMQ server: %s 建立连接完成,userName = %s", getMqttClientId(), JSON.toJSONString(serverURIs), userName));
        return sampleClient;
    } catch (MqttException me) {
        throw new RuntimeException(String.format("mqtt=======客户端%s与rabbitMQ server: %s 连接失败!!! userName = %s", getMqttClientId(), JSON.toJSONString(serverURIs), userName), me);
    }
}
 
Example 4
Source File: AwsIotMqttConnection.java    From aws-iot-device-sdk-java with Apache License 2.0 6 votes vote down vote up
private MqttConnectOptions buildMqttConnectOptions(AbstractAwsIotClient client, SocketFactory socketFactory) {
    MqttConnectOptions options = new MqttConnectOptions();

    options.setSocketFactory(socketFactory);
    options.setCleanSession(client.isCleanSession());
    options.setConnectionTimeout(client.getConnectionTimeout() / 1000);
    options.setKeepAliveInterval(client.getKeepAliveInterval() / 1000);
    if(client.isClientEnableMetrics()) {
        options.setUserName(USERNAME_METRIC_STRING);
    }

    Set<String> serverUris = getServerUris();
    if (serverUris != null && !serverUris.isEmpty()) {
        String[] uriArray = new String[serverUris.size()];
        serverUris.toArray(uriArray);
        options.setServerURIs(uriArray);
    }

    if (client.getWillMessage() != null) {
        AWSIotMessage message = client.getWillMessage();

        options.setWill(message.getTopic(), message.getPayload(), message.getQos().getValue(), false);
    }

    return options;
}
 
Example 5
Source File: MqttClientService.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() throws MqttException {
   final String serverURI = "tcp://localhost:1883";
   final MqttConnectOptions options = new MqttConnectOptions();
   options.setAutomaticReconnect(true);
   options.setCleanSession(false);
   options.setMaxInflight(1000);
   options.setServerURIs(new String[] {serverURI});
   options.setMqttVersion(MQTT_VERSION_3_1_1);

   final ThreadFactory threadFactory = new DefaultThreadFactory("mqtt-client-exec");
   executorService = new ScheduledThreadPoolExecutor(corePoolSize, threadFactory, new ThreadPoolExecutor.CallerRunsPolicy());
   mqttClient = new MqttClient(serverURI, clientId, persistence, executorService);
   mqttClient.setTimeToWait(-1);
   mqttClient.connect(options);
   mqttClient.setCallback(this);
   log.debugf("[MQTT][Connected][client: %s]", clientId);
}
 
Example 6
Source File: MqttConfig.java    From iot-dc3 with Apache License 2.0 5 votes vote down vote up
@Bean
public MqttConnectOptions getMqttConnectOptions() {
    MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
    mqttConnectOptions.setUserName(mqttProperty.getUsername());
    mqttConnectOptions.setPassword(mqttProperty.getPassword().toCharArray());
    mqttConnectOptions.setServerURIs(new String[]{mqttProperty.getUrl()});
    mqttConnectOptions.setKeepAliveInterval(mqttProperty.getKeepAlive());
    return mqttConnectOptions;
}
 
Example 7
Source File: MQTTClientBuilder.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
private MqttConnectOptions getOptions() {
  MqttConnectOptions options = new MqttConnectOptions();

  if ( isSecure ) {
    setSSLProps( options );
  }
  if ( !StringUtil.isEmpty( username ) ) {
    options.setUserName( username );
  }
  if ( !StringUtil.isEmpty( password ) ) {
    options.setPassword( password.toCharArray() );
  }

  if ( !StringUtil.isEmpty( keepAliveInterval ) ) {
    options.setKeepAliveInterval( Integer.parseInt( keepAliveInterval ) );
  }

  if ( !StringUtil.isEmpty( maxInflight ) ) {
    options.setMaxInflight( Integer.parseInt( maxInflight ) );
  }

  if ( !StringUtil.isEmpty( connectionTimeout ) ) {
    options.setConnectionTimeout( Integer.parseInt( connectionTimeout ) );
  }

  if ( !StringUtil.isEmpty( cleanSession ) ) {
    options.setCleanSession( BooleanUtils.toBoolean( cleanSession ) );
  }

  if ( !StringUtil.isEmpty( serverUris ) ) {
    options.setServerURIs(
      Arrays.stream( serverUris.split( ";" ) ).map( uri -> getProtocol() + uri ).toArray( String[]::new ) );
  }

  if ( !StringUtil.isEmpty( mqttVersion ) ) {
    options.setMqttVersion( Integer.parseInt( mqttVersion ) );
  }

  if ( !StringUtil.isEmpty( automaticReconnect ) ) {
    options.setAutomaticReconnect( BooleanUtils.toBoolean( automaticReconnect ) );
  }

  return options;
}