org.fusesource.mqtt.client.MQTT类的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(11.3k)|赞(0)|评价(0)|浏览(136)

本文整理了Java中org.fusesource.mqtt.client.MQTT类的一些代码示例,展示了MQTT类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MQTT类的具体详情如下:
包路径:org.fusesource.mqtt.client.MQTT
类名称:MQTT

MQTT介绍

暂无

代码示例

代码示例来源:origin: apache/storm

public static MQTT configureClient(MqttOptions options, String clientId, KeyStoreLoader keyStoreLoader)
  throws Exception {
  MQTT client = new MQTT();
  URI uri = URI.create(options.getUrl());
  client.setHost(uri);
  if (!uri.getScheme().toLowerCase().equals("tcp")) {
    client.setSslContext(SslUtils.sslContext(uri.getScheme(), keyStoreLoader));
  client.setClientId(clientId);
  LOG.info("MQTT ClientID: {}", client.getClientId().toString());
  client.setCleanSession(options.isCleanConnection());
  client.setReconnectDelay(options.getReconnectDelay());
  client.setReconnectDelayMax(options.getReconnectDelayMax());
  client.setReconnectBackOffMultiplier(options.getReconnectBackOffMultiplier());
  client.setConnectAttemptsMax(options.getConnectAttemptsMax());
  client.setReconnectAttemptsMax(options.getReconnectAttemptsMax());
  client.setUserName(options.getUserName());
  client.setPassword(options.getPassword());
  client.setTracer(new MqttLogger());
    client.setWillQos(qos);
    client.setWillTopic(options.getWillTopic());
    client.setWillMessage(options.getWillPayload());
    client.setWillRetain(options.getWillRetain());

代码示例来源:origin: andsel/moquette

String willTestamentMsg = "Bye bye";
MQTT mqtt = new MQTT();
mqtt.setHost("localhost", 1883);
mqtt.setClientId("WillTestamentPublisher");
mqtt.setWillRetain(false);
mqtt.setWillMessage(willTestamentMsg);
mqtt.setWillTopic(willTestamentTopic);
m_publisher = mqtt.blockingConnection();
m_publisher.connect();
m_mqtt.setHost("localhost", 1883);
m_mqtt.setCleanSession(false);
m_mqtt.setClientId("Subscriber");
m_subscriber = m_mqtt.blockingConnection();
m_subscriber.connect();
Topic[] topics = new Topic[]{new Topic(willTestamentTopic, QoS.AT_MOST_ONCE)};

代码示例来源:origin: fusesource/mqtt-client

public FutureConnection futureConnection() {
  return new FutureConnection(callbackConnection());
}
public BlockingConnection blockingConnection() {

代码示例来源:origin: fusesource/mqtt-client

public CallbackConnection callbackConnection() {
  if( !isCleanSession() && ( getClientId()==null || getClientId().length==0 )) {
    throw new IllegalArgumentException("The client id MUST be configured when clean session is set to false");
  }
  return new CallbackConnection(new MQTT(this));
}
public FutureConnection futureConnection() {

代码示例来源:origin: apache/storm

/**
 * Initializes {@code connection}.
 * @throws Exception if an exception during connecting to connector occurs
 */
public static void startPublisher() throws Exception {
  MQTT client = new MQTT();
  client.setTracer(new MqttLogger());
  client.setHost("tcp://localhost:1883");
  client.setClientId("MqttBrokerPublisher");
  connection = client.blockingConnection();
  Runtime.getRuntime().addShutdownHook(new Thread() {
    @Override
    public void run() {
      try {
        LOG.info("Shutting down MQTT client...");
        connection.disconnect();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  });
  connection.connect();
}

代码示例来源:origin: apache/apex-malhar

@Override
public void setup(OperatorContext context)
{
 try {
  client = new MQTT();
  if (mqttClientConfig.getClientId() != null) {
   client.setClientId(mqttClientConfig.getClientId());
  }
  client.setCleanSession(mqttClientConfig.isCleanSession());
  client.setConnectAttemptsMax(mqttClientConfig.getConnectAttemptsMax());
  client.setHost(mqttClientConfig.getHost(), mqttClientConfig.getPort());
  client.setKeepAlive(mqttClientConfig.getKeepAliveInterval());
  if (mqttClientConfig.getPassword() != null) {
   client.setPassword(mqttClientConfig.getPassword());
  }
  if (mqttClientConfig.getUserName() != null) {
   client.setUserName(mqttClientConfig.getUserName());
  }
  if (mqttClientConfig.getWillMessage() != null) {
   client.setWillMessage(mqttClientConfig.getWillMessage());
   client.setWillQos(mqttClientConfig.getWillQos());
   client.setWillRetain(mqttClientConfig.isWillRetain());
   client.setWillTopic(mqttClientConfig.getWillTopic());
  }
  connection = client.blockingConnection();
  connection.connect();
 } catch (Throwable t) {
  throw new RuntimeException(t);
 }
}

代码示例来源:origin: tuanhiep/mqtt-jmeter

private CallbackConnection createConnection(String host, String clientId,
    boolean durable, String user, String password)
    throws URISyntaxException {
  MQTT client = new MQTT();
  client.setHost(host);
  client.setClientId(clientId);
  client.setUserName(user);
  client.setPassword(password);
  client.setCleanSession(!durable);
  return client.callbackConnection();
}

代码示例来源:origin: fusesource/mqtt-client

displayHelpAndExit(0);
} else if ("-v".equals(arg)) {
  main.mqtt.setVersion(shift(argl));
} else if ("-h".equals(arg)) {
  main.mqtt.setHost(shift(argl));
} else if ("-k".equals(arg)) {
  main.mqtt.setKeepAlive(Short.parseShort(shift(argl)));
} else if ("-c".equals(arg)) {
  main.mqtt.setCleanSession(false);
} else if ("-i".equals(arg)) {
  main.mqtt.setClientId(shift(argl));
} else if ("-u".equals(arg)) {
  main.mqtt.setUserName(shift(argl));
} else if ("-p".equals(arg)) {
  main.mqtt.setPassword(shift(argl));
} else if ("--will-topic".equals(arg)) {
  main.mqtt.setWillTopic(shift(argl));
} else if ("--will-payload".equals(arg)) {
  main.mqtt.setWillMessage(shift(argl));
} else if ("--will-qos".equals(arg)) {
  int v = Integer.parseInt(shift(argl));
  main.mqtt.setWillQos(QoS.values()[v]);
} else if ("--will-retain".equals(arg)) {
  main.mqtt.setWillRetain(true);
} else if ("-d".equals(arg)) {
  main.debug = true;

代码示例来源:origin: apache/activemq-artemis

private static BlockingConnection retrieveMQTTConnection(String host) throws Exception {
   MQTT mqtt = new MQTT();
   mqtt.setHost(host);
   BlockingConnection connection = mqtt.blockingConnection();
   connection.connect();
   return connection;
  }
}

代码示例来源:origin: PerfCake/PerfCake

topicName = targetUri.getPath().substring(1);
try {
 final MQTT mqttClient = new MQTT();
 mqttClient.setHost(protocol + "://" + host + ":" + port);
 mqttClient.setConnectAttemptsMax(0);
 mqttClient.setReconnectAttemptsMax(0);
 if (userName != null) {
   mqttClient.setUserName(userName);
   mqttClient.setPassword(password);
 mqttConnection = mqttClient.blockingConnection();
 mqttConnection.connect();
    responseTopicName = responseTargetUri.getPath().substring(1);
    final MQTT mqttResponseClient = new MQTT();
    mqttResponseClient.setHost(responseProtocol + "://" + responseHost + ":" + responsePort);
    mqttResponseClient.setConnectAttemptsMax(0);
    mqttResponseClient.setReconnectAttemptsMax(0);
    if (responseUserName != null) {
      mqttResponseClient.setUserName(responseUserName);
      mqttResponseClient.setPassword(responsePassword);
    mqttResponseConnection = mqttResponseClient.blockingConnection();
    mqttResponseConnection.connect();
   } else {

代码示例来源:origin: stackoverflow.com

try{
  MqttMessage message2 = new MqttMessage();
  MQTT mqtt_connect = new MQTT();
  mqtt_connect.setHost(Host_Address, Integer.parseInt(port));
  String topic = "/call/MQTT_Config";
  mqtt_connect.setClientId("MQTT_Config");
  mqtt_connect.setWillRetain(false);
  mqtt_connect.isWillRetain();
  mqtt_connect.setWillTopic(topic);
  BlockingConnection m_publisher = mqtt_connect.blockingConnection();
  m_publisher.connect();

}
catch(Exception e){
  add message for connection not established
}

代码示例来源:origin: apache/activemq-artemis

@Test(timeout = 30000, expected = EOFException.class)
  public void testConnectionWithNullPassword() throws Exception {
   for (String version : Arrays.asList("3.1", "3.1.1")) {

     BlockingConnection connection = null;
     try {
      MQTT mqtt = createMQTTConnection("test-" + version, true);
      mqtt.setUserName(fullUser);
      mqtt.setPassword((String) null);
      mqtt.setConnectAttemptsMax(1);
      mqtt.setVersion(version);
      connection = mqtt.blockingConnection();
      connection.connect();
      fail("Connect should fail");
     } finally {
      if (connection != null && connection.isConnected()) connection.disconnect();
     }
   }
  }
}

代码示例来源:origin: apache/activemq-artemis

private MQTT createMQTTSslConnection(String clientId, boolean clean) throws Exception {
 MQTT mqtt = new MQTT();
 mqtt.setConnectAttemptsMax(1);
 mqtt.setReconnectAttemptsMax(0);
 mqtt.setTracer(createTracer());
 mqtt.setHost("ssl://localhost:" + port);
 if (clientId != null) {
   mqtt.setClientId(clientId);
 }
 mqtt.setCleanSession(clean);
 SSLContext ctx = SSLContext.getInstance("TLS");
 ctx.init(new KeyManager[0], new TrustManager[]{new DefaultTrustManager()}, new SecureRandom());
 mqtt.setSslContext(ctx);
 return mqtt;
}

代码示例来源:origin: apache/activemq-artemis

private MQTT createMQTTTcpConnection(String clientId, boolean clean) throws Exception {
 MQTT mqtt = new MQTT();
 mqtt.setConnectAttemptsMax(1);
 mqtt.setReconnectAttemptsMax(0);
 mqtt.setTracer(createTracer());
 mqtt.setVersion("3.1.1");
 if (clientId != null) {
   mqtt.setClientId(clientId);
 }
 mqtt.setCleanSession(clean);
 mqtt.setHost("localhost", port);
 return mqtt;
}

代码示例来源:origin: apache/activemq-artemis

@Test(timeout = 60 * 1000)
public void testUniqueMessageIds() throws Exception {
 MQTT mqtt = createMQTTConnection();
 mqtt.setClientId("foo");
 mqtt.setKeepAlive((short) 2);
 mqtt.setCleanSession(true);
 mqtt.setTracer(new Tracer() {
   @Override
   public void onReceive(MQTTFrame frame) {
 final BlockingConnection connection = mqtt.blockingConnection();
 connection.connect();

代码示例来源:origin: com.sitewhere/sitewhere-core

MQTT mqtt = new MQTT();
if ((component.getProtocol().startsWith("ssl")) || (component.getProtocol().startsWith("tls"))) {
  if ((component.getTrustStorePath() != null) && (component.getTrustStorePassword() != null)) {
    tmf.init(ks);
    sslContext.init(null, tmf.getTrustManagers(), null);
    mqtt.setSslContext(sslContext);
    LOGGER.info("Created SSL context for MQTT receiver.");
  } catch (Exception e) {
  mqtt.setUserName(component.getUsername());
  mqtt.setPassword(component.getPassword());
  mqtt.setHost(component.getProtocol() + "://" + component.getHostname() + ":" + component.getPort());
  return mqtt;
} catch (URISyntaxException e) {

代码示例来源:origin: apache/activemq-artemis

@Test(timeout = 60 * 1000)
public void testPingKeepsInactivityMonitorAlive() throws Exception {
 MQTT mqtt = createMQTTConnection();
 mqtt.setClientId("foo");
 mqtt.setKeepAlive((short) 2);
 final BlockingConnection connection = mqtt.blockingConnection();
 connection.connect();
 assertTrue("KeepAlive didn't work properly", Wait.waitFor(() -> connection.isConnected()));
 connection.disconnect();
}

代码示例来源:origin: tuanhiep/mqtt-jmeter

private CallbackConnection createConnection(String host, String clientId,
    boolean durable) throws URISyntaxException {
  MQTT client = new MQTT();
  client.setHost(host);
  client.setClientId(clientId);
  client.setCleanSession(!durable);
  return client.callbackConnection();
}

代码示例来源:origin: com.sitewhere/sitewhere-core

@Override
public void start(ILifecycleProgressMonitor monitor) throws SiteWhereException {
try {
  this.mqtt = new MQTT();
  mqtt.setHost(getHostname(), getPort());
} catch (URISyntaxException e) {
  throw new SiteWhereException("Invalid hostname for MQTT server.", e);
connection = mqtt.callbackConnection();
createListener();
connection.connect(new Callback<Void>() {

代码示例来源:origin: apache/activemq-artemis

private BlockingConnection retrieveMQTTConnection(String host, String truststorePath, String truststorePass, String keystorePath, String keystorePass) throws Exception {
 MQTT mqtt = new MQTT();
 mqtt.setConnectAttemptsMax(1);
 mqtt.setReconnectAttemptsMax(0);
 mqtt.setHost(host);
 SSLContext sslContext = new SSLSupport()
   .setKeystorePath(keystorePath)
   .setKeystorePassword(keystorePass)
   .setTruststorePath(truststorePath)
   .setTruststorePassword(truststorePass)
   .createContext();
 mqtt.setSslContext(sslContext);
 BlockingConnection connection = mqtt.blockingConnection();
 connection.connect();
 return connection;
}

相关文章