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

The following examples show how to use org.eclipse.paho.client.mqttv3.MqttConnectOptions#setMaxInflight() . 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: 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 2
Source File: MqttPlugin.java    From iotplatform with Apache License 2.0 6 votes vote down vote up
@Override
public void init(MqttPluginConfiguration configuration) {
  retryInterval = configuration.getRetryInterval();

  mqttClientOptions = new MqttConnectOptions();
  mqttClientOptions.setCleanSession(false);
  mqttClientOptions.setMaxInflight(configuration.getMaxInFlight());
  mqttClientOptions.setAutomaticReconnect(true);
  String clientId = configuration.getClientId();
  if (StringUtils.isEmpty(clientId)) {
    clientId = UUID.randomUUID().toString();
  }
  if (!StringUtils.isEmpty(configuration.getAccessToken())) {
    mqttClientOptions.setUserName(configuration.getAccessToken());
  }
  try {
    mqttClient = new MqttAsyncClient("tcp://" + configuration.getHost() + ":" + configuration.getPort(), clientId);
  } catch (Exception e) {
    log.error("Failed to create mqtt client", e);
    throw new RuntimeException(e);
  }
  // connect();
}
 
Example 3
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 4
Source File: DeviceConfig.java    From iot-java with Eclipse Public License 1.0 6 votes vote down vote up
public MqttConnectOptions getMqttConnectOptions() throws NoSuchAlgorithmException, KeyManagementException {
	MqttConnectOptions connectOptions = new MqttConnectOptions();

	connectOptions.setConnectionTimeout(DEFAULT_CONNECTION_TIMEMOUT);

	if (getMqttPassword() != null) {
		connectOptions.setUserName(getMqttUsername());
		connectOptions.setPassword(getMqttPassword().toCharArray());
	}

	connectOptions.setCleanSession(this.options.mqtt.cleanStart);
	connectOptions.setKeepAliveInterval(this.options.mqtt.keepAlive);
	connectOptions.setMaxInflight(DEFAULT_MAX_INFLIGHT_MESSAGES);
	connectOptions.setAutomaticReconnect(true);

	if (!Arrays.asList(1883, 80).contains(options.mqtt.port)) {
		SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
		sslContext.init(null, null, null);

		connectOptions.setSocketFactory(sslContext.getSocketFactory());
	}

	return connectOptions;
}
 
Example 5
Source File: ApplicationConfig.java    From iot-java with Eclipse Public License 1.0 6 votes vote down vote up
public MqttConnectOptions getMqttConnectOptions() throws NoSuchAlgorithmException, KeyManagementException {
	MqttConnectOptions connectOptions = new MqttConnectOptions();

	connectOptions.setConnectionTimeout(DEFAULT_CONNECTION_TIMEMOUT);

	connectOptions.setUserName(getMqttUsername());
	connectOptions.setPassword(getMqttPassword().toCharArray());

	connectOptions.setCleanSession(this.options.mqtt.cleanStart);
	connectOptions.setKeepAliveInterval(this.options.mqtt.keepAlive);
	connectOptions.setMaxInflight(DEFAULT_MAX_INFLIGHT_MESSAGES);
	connectOptions.setAutomaticReconnect(true);

	SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
	sslContext.init(null, null, null);

	connectOptions.setSocketFactory(sslContext.getSocketFactory());

	return connectOptions;
}
 
Example 6
Source File: MqttClientInstance.java    From maestro-java with Apache License 2.0 5 votes vote down vote up
public static MqttConnectOptions getConnectionOptions() {
    final MqttConnectOptions connOpts = new MqttConnectOptions();

    connOpts.setCleanSession(true);
    connOpts.setAutomaticReconnect(true);

    /*
     Paho uses 10 as the default max inflight exchanges. This may be a bit too small
     when sending log files, handling stats messages, ping requests ... all at the same.
     */
    connOpts.setMaxInflight(20);

    return connOpts;
}
 
Example 7
Source File: ManagerMQTT.java    From helloiot with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void connect() {

    String[] listtopics = worktopics.toArray(new String[worktopics.size()]);
    int[] listqos = new int[workqos.size()];
    for (int i = 0; i < workqos.size(); i++) {
        listqos[i] = workqos.get(i);
    }

    try {
        mqttClient = new MqttClient(url, clientid, new MemoryPersistence());
        MqttConnectOptions options = new MqttConnectOptions();
        if (!Strings.isNullOrEmpty(username)) {
            options.setUserName(username);
            options.setPassword(password.toCharArray());
        }
        options.setConnectionTimeout(timeout);
        options.setKeepAliveInterval(keepalive);
        options.setMqttVersion(version);
        options.setCleanSession(true);
        options.setAutomaticReconnect(false);
        options.setMaxInflight(maxinflight);
        options.setSSLProperties(sslproperties);
        options.setWill(topicsys + "app/" + clientid, new StringFormatSwitch().devalue(MiniVarBoolean.FALSE), 0, true);
        mqttClient.connect(options);
        mqttClient.setCallback(this);
        if (listtopics.length > 0) {
            mqttClient.subscribe(listtopics, listqos);
        }
        statusPublish(MiniVarBoolean.TRUE);
    } catch (MqttException ex) {
        logger.log(Level.WARNING, null, ex);
        throw new RuntimeException(String.format(resources.getString("exception.mqtt"), url), ex);
    }
}
 
Example 8
Source File: MqttPahoClient.java    From joynr with Apache License 2.0 5 votes vote down vote up
private MqttConnectOptions getConnectOptions() {
    MqttConnectOptions options = new MqttConnectOptions();
    if (username != null && !username.isEmpty()) {
        if (password == null || password.isEmpty()) {
            throw new JoynrIllegalStateException("MQTT password not configured or empty");
        }
        options.setUserName(username);
        options.setPassword(password.toCharArray());
    }
    options.setAutomaticReconnect(false);
    options.setConnectionTimeout(connectionTimeoutSec);
    options.setKeepAliveInterval(keepAliveTimerSec);
    options.setMaxInflight(maxMsgsInflight);
    options.setCleanSession(cleanSession);

    if (isSecureConnection) {
        // Set global SSL properties for all Joynr SSL clients
        Properties sslClientProperties = new Properties();
        sslClientProperties.setProperty(SSLSocketFactoryFactory.KEYSTORETYPE, keyStoreType);
        sslClientProperties.setProperty(SSLSocketFactoryFactory.KEYSTORE, keyStorePath);
        sslClientProperties.setProperty(SSLSocketFactoryFactory.KEYSTOREPWD, keyStorePWD);
        sslClientProperties.setProperty(SSLSocketFactoryFactory.TRUSTSTORETYPE, trustStoreType);
        sslClientProperties.setProperty(SSLSocketFactoryFactory.TRUSTSTORE, trustStorePath);
        sslClientProperties.setProperty(SSLSocketFactoryFactory.TRUSTSTOREPWD, trustStorePWD);
        options.setSSLProperties(sslClientProperties);
    }

    return options;
}
 
Example 9
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;
}