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


Java MqttTopic.publish方法代码示例

本文整理汇总了Java中org.eclipse.paho.client.mqttv3.MqttTopic.publish方法的典型用法代码示例。如果您正苦于以下问题:Java MqttTopic.publish方法的具体用法?Java MqttTopic.publish怎么用?Java MqttTopic.publish使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.eclipse.paho.client.mqttv3.MqttTopic的用法示例。


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

示例1: repeatedlyPub

import org.eclipse.paho.client.mqttv3.MqttTopic; //导入方法依赖的package包/类
void repeatedlyPub() {
  String methodName = Utility.getMethodName();

  int i = 0;
  while (mqttClient.isConnected()) {
    try {
      if (i > 999999) {
        i = 0;
      }
      byte[] payload = ("Message payload " + getClass().getName() + ".publish" + (i++)).getBytes();
      MqttTopic mqttTopic = mqttClient.getTopic(FirstSubTopicString);
      log.info("Publishing to..." + FirstSubTopicString);
      mqttTopic.publish(payload, 1, false);

    }
    catch (Exception exception) {
      log.fine("Caught exception:" + exception);
      // Don't fail - we are going to get an exception as we disconnected during takeOver
      // Its likely the publish rate is too high i.e. inflight window is full
    }
  }
}
 
开发者ID:gulliverrr,项目名称:hestia-engine-dev,代码行数:23,代码来源:LiveTakeOverTest.java

示例2: pubMsg

import org.eclipse.paho.client.mqttv3.MqttTopic; //导入方法依赖的package包/类
public static void pubMsg(String tcpUrl, String clientId, String topicName,
			String message) throws MqttException, UnsupportedEncodingException {
		MqttClient client = new MqttClient(tcpUrl, clientId);
		MqttConnectOptions mqcConf = new MqttConnectOptions();
		mqcConf.setConnectionTimeout(300);
		mqcConf.setKeepAliveInterval(1200);
		client.connect(mqcConf);

		MqttTopic topic = client.getTopic(topicName);
		topic.publish(message.getBytes("utf8"), 1, false);

//		client.close();
	}
 
开发者ID:osswangxining,项目名称:mqttserver,代码行数:14,代码来源:PubWebMessage.java

示例3: pubMsg

import org.eclipse.paho.client.mqttv3.MqttTopic; //导入方法依赖的package包/类
public static void pubMsg(String tcpUrl, String clientId, String topicName)
    throws MqttException, UnsupportedEncodingException {
  MqttClient client = new MqttClient(tcpUrl, clientId);
  MqttConnectOptions mqcConf = new MqttConnectOptions();
  mqcConf.setConnectionTimeout(300);
  mqcConf.setKeepAliveInterval(1200);
  client.connect(mqcConf);

  MqttTopic topic = client.getTopic(topicName);
  for (int i = 0; i < 10; i++) {
    String message = "{\"id\":" + (i+1) + ",\"temp\":12}";
    topic.publish(message.getBytes("utf8"), 1, false);
  }
  client.disconnect();
}
 
开发者ID:osswangxining,项目名称:mqttserver,代码行数:16,代码来源:PubMessage.java

示例4: publish

import org.eclipse.paho.client.mqttv3.MqttTopic; //导入方法依赖的package包/类
public void publish(String topicName, int qos, byte[] payload)
		throws MqttException {
	final MqttTopic topic = client.getTopic(topicName);

	final MqttMessage message = new MqttMessage(payload);
	topic.publish(message);

	System.out.println("Published data. Topic: " + topic.getName()
			+ "  Message: " + payload);
}
 
开发者ID:chauhansaurabhb,项目名称:EndUserInteractioion_RequestResponse_Command,代码行数:11,代码来源:MQTTPublisher.java

示例5: publish

import org.eclipse.paho.client.mqttv3.MqttTopic; //导入方法依赖的package包/类
@Override
public void publish() {
	if(mqttClient == null || !mqttClient.isConnected()) {
		log.debug("MQTT client unavailable");
		stateService.setRabbitDown();
		return;
	}

	Date now = new Date();
	String messageId = getMessageId();
	String messagePayload = getMessageBody(messageId, now);
	
	try {

		MqttTopic topic = mqttClient.getTopic(rabbitQueueName);
		MqttMessage mqttMessage = new MqttMessage(messagePayload.getBytes());
		mqttMessage.setQos(mqttQos);
		MqttDeliveryToken token = topic.publish(mqttMessage);
		if(mqttQos > 0) {
			token.waitForCompletion();
		}
		
		log.info("{} [{}] {}", instanceName, 
				messageId, messagePayload);
		
		stateService.setRabbitUp();
		
	}
	catch(MqttException ex) {
		log.warn("({}) Publish of MQTT message [{}] to RabbitMQ has failed",
				utils.getPublishedKey(consistencyChecker.getIndex()), messageId);
		
		if( ex.getReasonCode() == 32109 ) {
			log.warn("Connection lost (unsupported QoS mode?)");
		}
		
		stateService.setRabbitDown();
	}
	
}
 
开发者ID:sshcherbakov,项目名称:cf-service-tester,代码行数:41,代码来源:MqttTestMessagePublisher.java

示例6: sendMessageToSensor

import org.eclipse.paho.client.mqttv3.MqttTopic; //导入方法依赖的package包/类
public void sendMessageToSensor(String data) {

        if(!isConnected()){
            System.out.println("Not connected, aborting");
            return;
        }

        // setup topic
        MqttTopic topic = mClient.getTopic(sharedPref.getString("pref_sensor", ""));

        int pubQoS = 2;
        MqttMessage message = new MqttMessage(data.getBytes());
        message.setQos(pubQoS);
        message.setRetained(false);

        // Publish the message
        System.out.println("Publishing to topic \"" + topic + "\" qos " + pubQoS + " with message " + message.toString());
        MqttDeliveryToken token = null;
        try {
            // publish message to broker
            token = topic.publish(message);
            // Wait until the message has been delivered to the broker
            token.waitForCompletion();
            Thread.sleep(100);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
 
开发者ID:tchapi,项目名称:NodeMCU-Energy-Monitor,代码行数:30,代码来源:MQTTClient.java

示例7: publish

import org.eclipse.paho.client.mqttv3.MqttTopic; //导入方法依赖的package包/类
public void publish(String topicName, int qos, byte[] payload)
		throws MqttException {
	final MqttTopic topic = client.getTopic(topicName);

	final MqttMessage message = new MqttMessage(payload);
	topic.publish(message);

	System.out.println("Published data. Topic: "
			+ topic.getName() + "  Message: " + payload);
}
 
开发者ID:pankeshlinux,项目名称:IoTSuite,代码行数:11,代码来源:MQTTPublisher.java

示例8: testPubSub

import org.eclipse.paho.client.mqttv3.MqttTopic; //导入方法依赖的package包/类
/**
 * @throws Exception 
 */
@Test
public void testPubSub() throws Exception {
  String methodName = Utility.getMethodName();
  LoggingUtilities.banner(log, cclass, methodName);

  IMqttClient client = null;
  try {
    String topicStr = "topic" + "_02";
    String clientId = methodName;
    client = clientFactory.createMqttClient(serverURI, clientId);

    log.info("Assigning callback...");
    MessageListener listener = new MessageListener();
    client.setCallback(listener);

    log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + clientId);
    client.connect();

    log.info("Subscribing to..." + topicStr);
    client.subscribe(topicStr);

    log.info("Publishing to..." + topicStr);
    MqttTopic topic = client.getTopic(topicStr);
    MqttMessage message = new MqttMessage("foo".getBytes());
    topic.publish(message);

    log.info("Checking msg");
    MqttMessage msg = listener.getNextMessage();
    Assert.assertNotNull(msg);
    Assert.assertEquals("foo", msg.toString());

    log.info("getTopic name");
    String topicName = topic.getName();
    log.info("topicName = " + topicName);
    Assert.assertEquals(topicName, topicStr);

    log.info("Disconnecting...");
    client.disconnect();
  }
  finally {
    if (client != null) {
      log.info("Close...");
      client.close();
    }
  }
}
 
开发者ID:gulliverrr,项目名称:hestia-engine-dev,代码行数:50,代码来源:BasicTest.java

示例9: testRemoteConnect

import org.eclipse.paho.client.mqttv3.MqttTopic; //导入方法依赖的package包/类
/**
 * Test connection using a remote host name for the local host.
 * @throws Exception 
 */
@Test
public void testRemoteConnect() throws Exception {
  final String methodName = Utility.getMethodName();
  LoggingUtilities.banner(log, cclass, methodName);
  log.entering(className, methodName);

  IMqttClient mqttClient = null;
  try {
    mqttClient = clientFactory.createMqttClient(serverURI, methodName);
    log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + methodName);
    mqttClient.connect();
    log.info("Disconnecting...");
    mqttClient.disconnect();

    MqttV3Receiver mqttV3Receiver = new MqttV3Receiver(mqttClient, LoggingUtilities.getPrintStream());
    log.info("Assigning callback...");
    mqttClient.setCallback(mqttV3Receiver);
    MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
    mqttConnectOptions.setCleanSession(false);
    log.info("Connecting...(serverURI:" + serverURI + ", ClientId:" + methodName + ", cleanSession: false");
    mqttClient.connect(mqttConnectOptions);

    String[] topicNames = new String[]{methodName + "/Topic"};
    int[] topicQos = {0};
    log.info("Subscribing to..." + topicNames[0]);
    mqttClient.subscribe(topicNames, topicQos);

    byte[] payload = ("Message payload " + className + "." + methodName).getBytes();
    MqttTopic mqttTopic = mqttClient.getTopic(topicNames[0]);
    log.info("Publishing to..." + topicNames[0]);
    mqttTopic.publish(payload, 1, false);
    boolean ok = mqttV3Receiver.validateReceipt(topicNames[0], 0, payload);
    if (!ok) {
      Assert.fail("Receive failed");
    }
    log.info("Disconnecting...");
    mqttClient.disconnect();
  }
  catch (Exception exception) {
    log.log(Level.SEVERE, "caught exception:", exception);
    Assert.fail("Failed:" + methodName + " exception=" + exception);
  }
  finally {
    if (mqttClient != null) {
  	log.info("Close...");
      mqttClient.close();
    }
  }

  log.exiting(className, methodName);
}
 
开发者ID:gulliverrr,项目名称:hestia-engine-dev,代码行数:56,代码来源:SendReceiveTest.java

示例10: testPubSub

import org.eclipse.paho.client.mqttv3.MqttTopic; //导入方法依赖的package包/类
/**
 * @throws Exception 
 */
public void testPubSub() throws Exception {

  IMqttClient client = null;
  try {
    String topicStr = "topic" + "_02";
    String clientId = "testPubSub";
    client = new MqttClient(serverURI, clientId);

    System.out.println("Assigning callback...");
    MessageListener listener = new MessageListener();
    client.setCallback(listener);

    System.out.println("Connecting...(serverURI:" + serverURI + ", ClientId:" + clientId);
    client.connect();

    System.out.println("Subscribing to..." + topicStr);
    client.subscribe(topicStr);

    System.out.println("Publishing to..." + topicStr);
    MqttTopic topic = client.getTopic(topicStr);
    MqttMessage message = new MqttMessage("foo".getBytes());
    topic.publish(message);

    System.out.println("Checking msg");
    MqttMessage msg = listener.getNextMessage();
    
    if (msg == null) throw new Exception("message should not be null");
    if (!msg.toString().equals("foo")) throw new Exception("message should equal foo");

    System.out.println("getTopic name");
    String topicName = topic.getName();
    System.out.println("topicName = " + topicName);
    if (!topicName.equals(topicStr)) throw new Exception ("topicName should equal topicStr");

    System.out.println("Disconnecting...");
    client.disconnect();
    System.out.println("testPubSub completed successfully");
  }
  finally {
    if (client != null) {
  	System.out.println("Close...");
      client.close();
    }
  }
}
 
开发者ID:gulliverrr,项目名称:hestia-engine-dev,代码行数:49,代码来源:BasicSyncTestCaseMIDP.java

示例11: send

import org.eclipse.paho.client.mqttv3.MqttTopic; //导入方法依赖的package包/类
public void send(String message) throws MqttException {
    
	final MqttTopic notificationTopic = client.getTopic(VAADIN_DESKTOP_TOPIC_NOTIFICATION);

    notificationTopic.publish(new MqttMessage(message.getBytes()));
    System.out.println("Published data. Topic: " + notificationTopic.getName() + "   Message: " + message);
}
 
开发者ID:carlos-salinas,项目名称:vaadin.m2m,代码行数:8,代码来源:Publisher.java


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