• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java Topic类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: main

import org.fusesource.mqtt.client.Topic; //导入依赖的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.Topic; //导入依赖的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: connect

import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
public AnotherMqttClient connect() throws IOException {
	connection = mqttClient.futureConnection();
	exec(connection.connect());
	new Thread() {
		@Override
		public void run() {
			while (true) {
				try {
					org.fusesource.mqtt.client.Message message = exec(connection
							.receive());
					messages.add(new Message(message.getTopic(),
							new String(message.getPayload())));
					message.ack();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}.start();
	exec(connection
			.subscribe(new Topic[] { new Topic("#", AT_LEAST_ONCE) }));
	return this;
}
 
开发者ID:Ardulink,项目名称:Ardulink-2,代码行数:25,代码来源:AnotherMqttClient.java


示例4: start

import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
@Override
public boolean start() throws IOException {
  LOG.debug("Starting MQTT reader ...");
  Read spec = source.spec;
  try {
    client = spec.connectionConfiguration().createClient();
    LOG.debug("Reader client ID is {}", client.getClientId());
    checkpointMark.clientId = client.getClientId().toString();
    connection = client.blockingConnection();
    connection.connect();
    connection.subscribe(new Topic[]{
        new Topic(spec.connectionConfiguration().getTopic(), QoS.AT_LEAST_ONCE)});
    return advance();
  } catch (Exception e) {
    throw new IOException(e);
  }
}
 
开发者ID:apache,项目名称:beam,代码行数:18,代码来源:MqttIO.java


示例5: testConsumeMultipleTopics

import org.fusesource.mqtt.client.Topic; //导入依赖的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


示例6: testConsume

import org.fusesource.mqtt.client.Topic; //导入依赖的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


示例7: run

import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
@Override
public void run()
{
  try {
    int i = 0;
    Topic[] topics = new Topic[sendingData.size()];
    for (String key : sendingData.keySet()) {
      topics[i++] = new Topic(key, QoS.AT_MOST_ONCE);
    }
    connection.subscribe(topics);
    while (receivedData.size() < sendingData.size()) {
      Message msg = connection.receive();
      receivedData.put(msg.getTopic(), new String(msg.getPayload()));
    }
  } catch (Exception ex) {
    throw new RuntimeException(ex);
  }
}
 
开发者ID:apache,项目名称:apex-malhar,代码行数:19,代码来源:MqttOutputOperatorTest.java


示例8: onSubscribe

import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
void onSubscribe(SUBSCRIBE command) throws MQTTProtocolException {
    checkConnected();
    Topic[] topics = command.topics();
    if (topics != null) {
        byte[] qos = new byte[topics.length];
        for (int i = 0; i < topics.length; i++) {
            qos[i] = (byte) onSubscribe(command, topics[i]).ordinal();
        }
        SUBACK ack = new SUBACK();
        ack.messageId(command.messageId());
        ack.grantedQos(qos);
        try {
            getMQTTTransport().sendToMQTT(ack.encode());
        } catch (IOException e) {
            LOG.warn("Couldn't send SUBACK for " + command, e);
        }
    } else {
        LOG.warn("No topics defined for Subscription " + command);
    }
}
 
开发者ID:DiamondLightSource,项目名称:daq-eclipse,代码行数:21,代码来源:MQTTProtocolConverter.java


示例9: callBroker

import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
private static void callBroker(String truststorePath, String truststorePass, String keystorePath, String keystorePass) throws Exception {
   BlockingConnection connection = null;

   try {
      connection = retrieveMQTTConnection("ssl://localhost:1883", truststorePath, truststorePass, keystorePath, keystorePass);
      // Subscribe to topics
      Topic[] topics = {new Topic("test/+/some/#", QoS.AT_MOST_ONCE)};
      connection.subscribe(topics);

      // Publish Messages
      String payload = "This is message 1";

      connection.publish("test/1/some/la", payload.getBytes(), QoS.AT_LEAST_ONCE, false);

      Message message = connection.receive(5, TimeUnit.SECONDS);
      System.out.println("Message received: " + new String(message.getPayload()));

   } catch (Exception e) {
      throw e;
   } finally {
      if (connection != null) {
         connection.disconnect();
      }
   }
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:26,代码来源:MqttCrlEnabledExample.java


示例10: doTestSendJMSReceiveMQTT

import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
public void doTestSendJMSReceiveMQTT(String destinationName) throws Exception {
   final MQTTClientProvider provider = getMQTTClientProvider();
   initializeConnection(provider);
   provider.subscribe("foo/+", AT_MOST_ONCE);

   Connection connection = cf.createConnection();
   connection.start();

   Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   javax.jms.Topic topic = s.createTopic(destinationName);
   MessageProducer producer = s.createProducer(topic);

   // send retained message from JMS
   final byte[] bytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
   BytesMessage bytesMessage = s.createBytesMessage();
   bytesMessage.writeBytes(bytes);
   producer.send(bytesMessage);

   byte[] message = provider.receive(10000);
   assertNotNull("Should get retained message", message);
   assertArrayEquals(bytes, message);

   provider.disconnect();
   connection.close();
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:26,代码来源:MQTTTest.java


示例11: testClientDisconnectedOnMaxConsumerLimitReached

import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
@Test(timeout = 60 * 1000)
public void testClientDisconnectedOnMaxConsumerLimitReached() throws Exception {
   Exception peerDisconnectedException = null;
   try {
      String clientId = "test.client";
      SimpleString coreAddress = new SimpleString("foo.bar");
      Topic[] mqttSubscription = new Topic[]{new Topic("foo/bar", QoS.AT_LEAST_ONCE)};

      getServer().createQueue(coreAddress, RoutingType.MULTICAST, new SimpleString(clientId + "." + coreAddress), null, false, true, 0, false, true);

      MQTT mqtt = createMQTTConnection();
      mqtt.setClientId(clientId);
      mqtt.setKeepAlive((short) 2);
      final BlockingConnection connection = mqtt.blockingConnection();
      connection.connect();
      connection.subscribe(mqttSubscription);
   } catch (EOFException e) {
      peerDisconnectedException = e;
   }
   assertNotNull(peerDisconnectedException);
   assertTrue(peerDisconnectedException.getMessage().contains("Peer disconnected"));
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:23,代码来源:MQTTTest.java


示例12: testReferenceBinding

import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
@Test
public void testReferenceBinding() throws Exception {
    MQTT mqtt = new MQTT();
    Topic outputTopic = new Topic(TOPIC_OUTPUT, QoS.AT_LEAST_ONCE);
    BlockingConnection connection = mqtt.blockingConnection();
    try {
        connection.connect();
        connection.subscribe(new Topic[]{outputTopic});

        _greet.sendInOnly(MESSAGE_INPUT);
        Message message = connection.receive(1000, TimeUnit.MILLISECONDS);
        Assert.assertNotNull("No output message from " + TOPIC_OUTPUT, message);
        Assert.assertEquals(MESSAGE_OUTPUT, new String(message.getPayload()));
        Assert.assertNull("More than one message received from " + TOPIC_OUTPUT,
                connection.receive(1000, TimeUnit.MILLISECONDS));
    } finally {
        connection.disconnect();
    }
}
 
开发者ID:jboss-switchyard,项目名称:switchyard,代码行数:20,代码来源:CamelMQTTBindingTest.java


示例13: startMqttServerMock

import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
private void startMqttServerMock() {
	
	String broker = "tcp://appliance4.uniquid.co:1883";
	String topic = "test";
	Topic[] topics = {new Topic(topic, QoS.AT_LEAST_ONCE)};
	BlockingConnection connection = null;
	
	try{
		MQTT mqtt = new MQTT();
		mqtt.setHost(broker);
		connection = mqtt.blockingConnection();
		connection.connect();
		connection.subscribe(topics);
		// blocks!!!
		Message message = connection.receive();
		
		byte[] payload = message.getPayload();

		message.ack();
		
		Assert.assertNotNull(message);

		FunctionRequestMessage rpcProviderRequest = (FunctionRequestMessage) new JSONMessageSerializer().deserialize(payload);
		Assert.assertNotNull(rpcProviderRequest);
		
		FunctionResponseMessage rpcProviderResponse = new FunctionResponseMessage();
		rpcProviderResponse.setProvider("test");
		rpcProviderResponse.setError(0);
		rpcProviderResponse.setResult("result");
		rpcProviderResponse.setId(rpcProviderRequest.getId());
		
		connection.publish(rpcProviderRequest.getUser(), new JSONMessageSerializer().serialize(rpcProviderResponse), QoS.AT_LEAST_ONCE, false);
		
		connection.disconnect();			
		
	} catch (Throwable t) {
		Assert.fail();
	}
}
 
开发者ID:uniquid,项目名称:uniquid-utils,代码行数:40,代码来源:MQTTUserClientTest.java


示例14: startMqttServerMockException

import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
private void startMqttServerMockException() {
	
	String broker = "tcp://appliance4.uniquid.co:1883";
	String topic = "test";
	Topic[] topics = {new Topic(topic, QoS.AT_LEAST_ONCE)};
	BlockingConnection connection = null;
	
	try{
		MQTT mqtt = new MQTT();
		mqtt.setHost(broker);
		connection = mqtt.blockingConnection();
		connection.connect();
		connection.subscribe(topics);
		// blocks!!!
		Message message = connection.receive();
		
		byte[] payload = message.getPayload();

		message.ack();
		
		Assert.assertNotNull(message);
					
		FunctionRequestMessage functionRequestMessage = (FunctionRequestMessage) new JSONMessageSerializer().deserialize(payload);
		Assert.assertNotNull(functionRequestMessage);

		FunctionResponseMessage functionResponseMessage = new FunctionResponseMessage();
		functionResponseMessage.setProvider("sender");
		functionResponseMessage.setResult("result");
		functionResponseMessage.setError(0);
		
		connection.disconnect();			
		
	} catch (Throwable t) {
		Assert.fail();
	}
}
 
开发者ID:uniquid,项目名称:uniquid-utils,代码行数:37,代码来源:MQTTUserClientTest.java


示例15: subscribe

import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
public void subscribe(String subscrTopic, QoS quality) {
    Topic[] topics = { new Topic (subscrTopic, quality) };
    connection.subscribe(topics, new Callback<byte[]>() {
        public void onSuccess(byte[] qoses) { LOGGER.info("Subscribe success for topic {} with quality {}", subscrTopic, quality); }
        public void onFailure(Throwable value) {
            LOGGER.info("Subscribe failure for topic {}", subscrTopic, value);
        }
    });
}
 
开发者ID:CROW-NDOV,项目名称:displaydirect,代码行数:10,代码来源:MqttConnection.java


示例16: TTNMqttClient

import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
public TTNMqttClient(String broker, String appEUI, String accessKey, String devEUI) {
    mGson = new GsonBuilder()
            .registerTypeAdapter(DateTime.class, new DateTimeConverter())
            .create();
    mTopic = new Topic(appEUI + "/devices/" + devEUI + "/up", QoS.AT_LEAST_ONCE);
    try {
        mMqtt.setHost("tcp://" + broker + ":" + MQTT_HOST_PORT);
        mMqtt.setUserName(appEUI);
        mMqtt.setPassword(accessKey);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}
 
开发者ID:ticofab,项目名称:The-Things-Network-Android-SDK,代码行数:14,代码来源:TTNMqttClient.java


示例17: subscribe

import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
/** {@inheritDoc} */
@Override
public void subscribe(final String channel, final MessageListener callback) {
	if (this.connection != null) {
		if (this.channels.containsKey(channel)) {
			return;
		}
		final CountDownLatch l = new CountDownLatch(1);
		final Topic[] topic = { new Topic(channel, QoS.AT_MOST_ONCE) };
		this.connection.subscribe(topic, new Callback<byte[]>() {
			@Override
			public void onFailure(final Throwable throwable) {
				System.out.println("Impossible to SUBSCRIBE to channel \"" + channel + "\"");
				logTracker.log("Impossible to SUBSCRIBE to channel \"" + channel + "\"");
				l.countDown();
			}

			@Override
			public void onSuccess(final byte[] bytes) {
				KuraMQTTClient.this.channels.put(channel, callback);
				l.countDown();
				logTracker.log("Successfully subscribed to " + channel);
				System.out.println("Successfully subscribed to " + channel);
			}
		});
		try {
			l.await();
		} catch (final InterruptedException e) {
			System.out.println("Impossible to SUBSCRIBE to channel \"" + channel + "\"");
			logTracker.log("Impossible to SUBSCRIBE to channel \"" + channel + "\"");
		}
	}
}
 
开发者ID:amitjoy,项目名称:Kura-MQTT-Client-Utility,代码行数:34,代码来源:KuraMQTTClient.java


示例18: testProduce

import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
@Test
public void testProduce() throws Exception {
    MQTT mqtt = new MQTT();
    final BlockingConnection subscribeConnection = mqtt.blockingConnection();
    subscribeConnection.connect();
    Topic topic = new Topic(TEST_TOPIC, QoS.AT_MOST_ONCE);
    Topic[] topics = {topic};
    subscribeConnection.subscribe(topics);
    final CountDownLatch latch = new CountDownLatch(numberOfMessages);

    Thread thread = new Thread(new Runnable() {
        public void run() {
            for (int i = 0; i < numberOfMessages; i++) {
                try {
                    Message message = subscribeConnection.receive();
                    message.ack();
                    latch.countDown();
                } catch (Exception e) {
                    e.printStackTrace();
                    break;
                }
            }
        }
    });
    thread.start();

    Producer producer = context.getEndpoint("direct:foo").createProducer();
    for (int i = 0; i < numberOfMessages; i++) {
        Exchange exchange = producer.createExchange();
        exchange.getIn().setBody("test message " + i);
        producer.process(exchange);
    }
    latch.await(20, TimeUnit.SECONDS);
    assertTrue("Messages not consumed = " + latch.getCount(), latch.getCount() == 0);
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:36,代码来源:MQTTProducerTest.java


示例19: initializeConnection

import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
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);
  }
}
 
开发者ID:apache,项目名称:apex-malhar,代码行数:14,代码来源:AbstractMqttInputOperator.java


示例20: main

import org.fusesource.mqtt.client.Topic; //导入依赖的package包/类
public static void main(final String[] args) throws Exception {
   // Create a new MQTT connection to the broker.  We are not setting the client ID.  The broker will pick one for us.
   System.out.println("Connecting to Artemis using MQTT");
   MQTT mqtt = new MQTT();
   mqtt.setHost("tcp://localhost:1883");
   BlockingConnection connection = mqtt.blockingConnection();
   connection.connect();
   System.out.println("Connected to Artemis");

   // Subscribe to topics
   Topic[] topics = {new Topic("mqtt/example/publish", QoS.AT_LEAST_ONCE), new Topic("test/#", QoS.EXACTLY_ONCE), new Topic("foo/+/bar", QoS.AT_LEAST_ONCE)};
   connection.subscribe(topics);
   System.out.println("Subscribed to topics.");

   // Publish Messages
   String payload1 = "This is message 1";
   String payload2 = "This is message 2";
   String payload3 = "This is message 3";

   connection.publish("mqtt/example/publish", payload1.getBytes(), QoS.AT_LEAST_ONCE, false);
   connection.publish("test/test", payload2.getBytes(), QoS.AT_MOST_ONCE, false);
   connection.publish("foo/1/bar", payload3.getBytes(), QoS.AT_MOST_ONCE, false);
   System.out.println("Sent messages.");

   Message message1 = connection.receive(5, TimeUnit.SECONDS);
   Message message2 = connection.receive(5, TimeUnit.SECONDS);
   Message message3 = connection.receive(5, TimeUnit.SECONDS);
   System.out.println("Received messages.");

   System.out.println(new String(message1.getPayload()));
   System.out.println(new String(message2.getPayload()));
   System.out.println(new String(message3.getPayload()));
}
 
开发者ID:apache,项目名称:activemq-artemis,代码行数:34,代码来源:MQTTBasicPubSubExample.java



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java JDBCDriver类代码示例发布时间:2022-05-21
下一篇:
Java MetadataTrackRenderer类代码示例发布时间:2022-05-21
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap