org.fusesource.mqtt.client.MQTT.blockingConnection()方法的使用及代码示例

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

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

MQTT.blockingConnection介绍

暂无

代码示例

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

public void connectMqtt(String clientId) throws Exception {
    MQTT client = MqttUtils.configureClient(this.options, clientId, this.keyStoreLoader);
    this.connection = client.blockingConnection();
    this.connection.connect();
  }
}

代码示例来源: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: andsel/moquette

mqtt.setWillMessage(willTestamentMsg);
mqtt.setWillTopic(willTestamentTopic);
m_publisher = mqtt.blockingConnection();
m_publisher.connect();
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: org.apache.apex/malhar-contrib

private void initializeConnection() throws Exception
{
 connection = client.blockingConnection();
 connection.connect();
 if (!topicMap.isEmpty()) {
  Topic[] topics = new Topic[topicMap.size()];
  int i = 0;
  for (Map.Entry<String, QoS> entry : topicMap.entrySet()) {
   topics[i++] = new Topic(entry.getKey(), entry.getValue());
  }
  connection.subscribe(topics);
 }
}

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

private void initializeConnection() throws Exception
{
 connection = client.blockingConnection();
 connection.connect();
 if (!topicMap.isEmpty()) {
  Topic[] topics = new Topic[topicMap.size()];
  int i = 0;
  for (Map.Entry<String, QoS> entry : topicMap.entrySet()) {
   topics[i++] = new Topic(entry.getKey(), entry.getValue());
  }
  connection.subscribe(topics);
 }
}

代码示例来源: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: 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

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

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

@Test(timeout = 60 * 1000)
public void testReuseConnection() throws Exception {
 MQTT mqtt = createMQTTConnection();
 mqtt.setClientId("Test-Client");
 {
   BlockingConnection connection = mqtt.blockingConnection();
   connection.connect();
   connection.disconnect();
   Thread.sleep(1000);
 }
 {
   BlockingConnection connection = mqtt.blockingConnection();
   connection.connect();
   connection.disconnect();
   Thread.sleep(1000);
 }
}

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

@Override
public void connect(String host) throws Exception {
 mqtt.setHost(host);
 mqtt.setVersion("3.1.1");
 // shut off connect retry
 mqtt.setConnectAttemptsMax(0);
 mqtt.setReconnectAttemptsMax(0);
 connection = mqtt.blockingConnection();
 connection.connect();
}

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

@Test(timeout = 60 * 1000)
public void testMQTT311Connection() throws Exception {
 MQTT mqtt = createMQTTConnection();
 mqtt.setClientId("foo");
 mqtt.setVersion("3.1.1");
 final BlockingConnection connection = mqtt.blockingConnection();
 connection.connect();
 connection.disconnect();
}

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

@Test(timeout = 30 * 1000)
public void testValidZeroLengthClientId() throws Exception {
 MQTT mqtt = createMQTTConnection();
 mqtt.setClientId("");
 mqtt.setCleanSession(true);
 BlockingConnection connection = mqtt.blockingConnection();
 connection.connect();
 connection.disconnect();
}

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

@Test(timeout = 60 * 1000)
public void testDuplicateClientId() throws Exception {
 final String clientId = "duplicateClient";
 MQTT mqtt = createMQTTConnection(clientId, false);
 mqtt.setKeepAlive((short) 2);
 final BlockingConnection connection = mqtt.blockingConnection();
 connection.connect();
 final String TOPICA = "TopicA";
 connection.publish(TOPICA, TOPICA.getBytes(), QoS.EXACTLY_ONCE, true);
 MQTT mqtt1 = createMQTTConnection(clientId, false);
 mqtt1.setKeepAlive((short) 2);
 final BlockingConnection connection1 = mqtt1.blockingConnection();
 connection1.connect();
 assertTrue("Duplicate client disconnected", Wait.waitFor(() -> connection1.isConnected()));
 assertTrue("Old client still connected", Wait.waitFor(() -> !connection.isConnected()));
 connection1.publish(TOPICA, TOPICA.getBytes(), QoS.EXACTLY_ONCE, true);
 connection1.disconnect();
}

代码示例来源: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

@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: apache/activemq-artemis

@Test(timeout = 30000)
public void testConnection() 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(fullPass);
    mqtt.setConnectAttemptsMax(1);
    mqtt.setVersion(version);
    connection = mqtt.blockingConnection();
    connection.connect();
    BlockingConnection finalConnection = connection;
    assertTrue("Should be connected", Wait.waitFor(() -> finalConnection.isConnected(), 5000, 100));
   } finally {
    if (connection != null && connection.isConnected()) connection.disconnect();
   }
 }
}

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

@Test(timeout = 30 * 1000)
public void testDefaultKeepAliveWhenClientSpecifiesZero() throws Exception {
 stopBroker();
 protocolConfig = "transport.defaultKeepAlive=2000";
 startBroker();
 MQTT mqtt = createMQTTConnection();
 mqtt.setClientId("foo");
 mqtt.setKeepAlive((short) 0);
 final BlockingConnection connection = mqtt.blockingConnection();
 connection.connect();
 assertTrue("KeepAlive didn't work properly", Wait.waitFor(() -> connection.isConnected()));
}

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

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

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

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

代码示例来源: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;
}

相关文章