当前位置: 首页>>代码示例>>Java>>正文


Java MQTT类代码示例

本文整理汇总了Java中org.fusesource.mqtt.client.MQTT的典型用法代码示例。如果您正苦于以下问题:Java MQTT类的具体用法?Java MQTT怎么用?Java MQTT使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


MQTT类属于org.fusesource.mqtt.client包,在下文中一共展示了MQTT类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import org.fusesource.mqtt.client.MQTT; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
	   	  System.out.println("Connecting to Broker1 using MQTT");
	      MQTT mqtt = new MQTT();
	      mqtt.setHost(BROKER_URL);
	      BlockingConnection connection = mqtt.blockingConnection();
	      connection.connect();
	      System.out.println("Connected to Broker1");
	      // Subscribe to  fidelityAds topic
	      Topic[] topics = { new Topic(FIDELITY_ADS_TOPIC, QoS.AT_LEAST_ONCE)};
	      connection.subscribe(topics);
	      // Publish Ads
	      String ads1 = "Discount on transfert fees up to -50% with coupon code JBOSSDOCTOR.  www.beosbank.com";
	      long index=0;
	      while(true){
	    	  connection.publish(FIDELITY_ADS_TOPIC, (index+":"+ads1).getBytes(), QoS.AT_LEAST_ONCE, false);
			  System.out.println("Sent messages with index="+index);
		      Thread.sleep(10000);
		      index++;
	      }
}
 
开发者ID:PacktPublishing,项目名称:JBoss-Developers-Guide,代码行数:21,代码来源:AdsProducer.java

示例2: main

import org.fusesource.mqtt.client.MQTT; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
	   System.out.println("Connecting to Broker1 using MQTT");
	      MQTT mqtt = new MQTT();
	      mqtt.setHost(BROKER_URL);
	      BlockingConnection connection = mqtt.blockingConnection();
	      connection.connect();
	      System.out.println("Connected to Artemis");

	      // Subscribe to  fidelityAds topic
	      Topic[] topics = {new Topic(FIDELITY_ADS_TOPIC, QoS.AT_LEAST_ONCE)};
	      connection.subscribe(topics);

	      // Get Ads Messages

	      while(true){
	    	  Message message = connection.receive(5, TimeUnit.SECONDS);
	    	  if(message!=null){
	    		  System.out.println("Received messages. "+new String(message.getPayload()));
	    	  }
		      
	      }
	    
	      
	      
	      
}
 
开发者ID:PacktPublishing,项目名称:JBoss-Developers-Guide,代码行数:27,代码来源:BeosBankIotDevice.java

示例3: testConsumeMultipleTopicsWithWildcards

import org.fusesource.mqtt.client.MQTT; //导入依赖的package包/类
@Test
public void testConsumeMultipleTopicsWithWildcards() throws Exception {
    MQTT mqtt = new MQTT();
    BlockingConnection publisherConnection = mqtt.blockingConnection();
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMinimumMessageCount(numberOfMessages * (PUBLISH_TOPICS.length - 1));

    publisherConnection.connect();
    String payload;
    for (String topic : PUBLISH_TOPICS) {
        for (int i = 0; i < numberOfMessages; i++) {
            payload = "Topic " + topic + ", Message " + i;
            publisherConnection.publish(topic, payload.getBytes(), QoS.AT_LEAST_ONCE, false);
        }
    }

    mock.await(5, TimeUnit.SECONDS);
    mock.assertIsSatisfied();
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:20,代码来源:MQTTConsumerWildcardTopicsTest.java

示例4: canDetectTheMQTTProtocol

import org.fusesource.mqtt.client.MQTT; //导入依赖的package包/类
@Test// (timeout=60 * 1000)
    public void canDetectTheMQTTProtocol() throws Exception {

        DetectingGateway gateway = createGateway();

        // Lets establish a connection....
        MQTT mqtt = new MQTT();
        mqtt.setHost("localhost", gateway.getBoundPort());
        mqtt.setClientId("myclientid");
//        mqtt.setVersion("3.1.1");
        mqtt.setUserName("broker0/chirino");
        mqtt.setConnectAttemptsMax(1);

        org.fusesource.mqtt.client.BlockingConnection connection = mqtt.blockingConnection();
        connection.connect();

        assertEquals(1, gateway.getSuccessfulConnectionAttempts());
        assertEquals(1, gateway.getConnectedClients().length);
        assertConnectedToBroker(0);
        connection.kill();
    }
 
开发者ID:nkasvosve,项目名称:beyondj,代码行数:22,代码来源:DetectingGatewayTest.java

示例5: sslClient

import org.fusesource.mqtt.client.MQTT; //导入依赖的package包/类
public MQTT sslClient(String... ca) throws Exception {
    if (mqtt.getHost() == null) {
        throw new IllegalStateException("No host configured.");
    }

    if (mqtt.getUserName() == null) {
        throw new IllegalStateException("No device key provided.");
    }

    if (ca != null && ca.length == 1 && ca[0] != null) {
        final Certificate certificate = getCertificate(ca[0]);
        final TrustManagerFactory trustManagerFactory = getTrustManagerFactory(certificate, ca[0]);
        final SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
        mqtt.setSslContext(sslContext);
    }

    mqtt.setConnectAttemptsMax(-1);        // No limit on number of connection attempts

    mqtt.setReconnectDelay(5000);          // Delay in milliseconds
    mqtt.setReconnectAttemptsMax(-1);      // No limit on number of reconnection attempts
    mqtt.setReconnectBackOffMultiplier(2); // Do not use exponential back off for reconnect

    return mqtt;
}
 
开发者ID:Wolkabout,项目名称:WolkConnect-Java-,代码行数:26,代码来源:MqttFactory.java

示例6: newClient

import org.fusesource.mqtt.client.MQTT; //导入依赖的package包/类
private MQTT newClient(MqttLinkConfig config) {
	MQTT client = new MQTT();
	client.setClientId(config.getClientId());
	client.setHost(URIs.newURI(connectionPrefix(config) + "://"
			+ config.getHost() + ":" + config.getPort()));
	String user = config.getUser();
	if (!Strings.nullOrEmpty(user)) {
		client.setUserName(user);
	}
	String password = config.getPassword();
	if (!Strings.nullOrEmpty(password)) {
		client.setPassword(password);
	}
	return client;

}
 
开发者ID:Ardulink,项目名称:Ardulink-2,代码行数:17,代码来源:MqttLink.java

示例7: testConsumeMultipleTopics

import org.fusesource.mqtt.client.MQTT; //导入依赖的package包/类
@Test
public void testConsumeMultipleTopics() throws Exception {
    MQTT mqtt = new MQTT();
    BlockingConnection publisherConnection = mqtt.blockingConnection();
    Topic topic1 = new Topic(TEST_TOPIC, QoS.AT_MOST_ONCE);
    Topic topic2 = new Topic(TEST_TOPIC_2, QoS.AT_MOST_ONCE);
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMinimumMessageCount(numberOfMessages * 2);

    publisherConnection.connect();
    String payload;
    for (int i = 0; i < numberOfMessages; i++) {
        payload = "Topic 1, Message " + i;
        publisherConnection.publish(topic1.name().toString(), payload.getBytes(), QoS.AT_LEAST_ONCE, false);
        payload = "Topic 2, Message " + i;
        publisherConnection.publish(topic2.name().toString(), payload.getBytes(), QoS.AT_LEAST_ONCE, false);
    }

    mock.await(5, TimeUnit.SECONDS);
    mock.assertIsSatisfied();
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:22,代码来源:MQTTConsumerMultipleTopicsTest.java

示例8: testConsume

import org.fusesource.mqtt.client.MQTT; //导入依赖的package包/类
@Test
public void testConsume() throws Exception {
    MQTT mqtt = new MQTT();
    BlockingConnection publisherConnection = mqtt.blockingConnection();
    Topic topic = new Topic(TEST_TOPIC, QoS.AT_MOST_ONCE);
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMinimumMessageCount(numberOfMessages);

    publisherConnection.connect();
    for (int i = 0; i < numberOfMessages; i++) {
        String payload = "Message " + i;
        publisherConnection.publish(topic.name().toString(), payload.getBytes(), QoS.AT_LEAST_ONCE, false);
    }

    mock.await(5, TimeUnit.SECONDS);
    mock.assertIsSatisfied();
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:18,代码来源:MQTTConsumerTest.java

示例9: main

import org.fusesource.mqtt.client.MQTT; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
        MQTT client = new MQTT();
        client.setHost("tcp://raspberrypi.local:1883");
        client.setClientId("fusesourcepublisher");
        BlockingConnection connection = client.blockingConnection();

        connection.connect();

        connection.publish("testack", "Hello".getBytes(), QoS.AT_LEAST_ONCE, false);

    while(true){
        Thread.sleep(2000);
    }
        //connection.disconnect();

}
 
开发者ID:ptgoetz,项目名称:storm-mqtt,代码行数:17,代码来源:MqttPublisher.java

示例10: testConnection

import org.fusesource.mqtt.client.MQTT; //导入依赖的package包/类
@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();
      }
   }
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:21,代码来源:MQTTSecurityTest.java

示例11: testConnectionWithNullPassword

import org.fusesource.mqtt.client.MQTT; //导入依赖的package包/类
@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();
      }
   }
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:20,代码来源:MQTTSecurityTest.java

示例12: createMQTTSslConnection

import org.fusesource.mqtt.client.MQTT; //导入依赖的package包/类
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;
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:17,代码来源:MQTTTestSupport.java

示例13: testConnectWithLargePassword

import org.fusesource.mqtt.client.MQTT; //导入依赖的package包/类
@Test
public void testConnectWithLargePassword() throws Exception {
   for (String version : Arrays.asList("3.1", "3.1.1")) {
      String longString = new String(new char[65535]);

      BlockingConnection connection = null;
      try {
         MQTT mqtt = createMQTTConnection("test-" + version, true);
         mqtt.setUserName(longString);
         mqtt.setPassword(longString);
         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();
      }
   }
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:22,代码来源:MQTTTest.java

示例14: testTurnOffInactivityMonitor

import org.fusesource.mqtt.client.MQTT; //导入依赖的package包/类
@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();
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:17,代码来源:MQTTTest.java

示例15: testDuplicateClientId

import org.fusesource.mqtt.client.MQTT; //导入依赖的package包/类
@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();
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:23,代码来源:MQTTTest.java


注:本文中的org.fusesource.mqtt.client.MQTT类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。