本文整理汇总了Java中org.eclipse.paho.client.mqttv3.MqttTopic类的典型用法代码示例。如果您正苦于以下问题:Java MqttTopic类的具体用法?Java MqttTopic怎么用?Java MqttTopic使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MqttTopic类属于org.eclipse.paho.client.mqttv3包,在下文中一共展示了MqttTopic类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onViewClicked
import org.eclipse.paho.client.mqttv3.MqttTopic; //导入依赖的package包/类
@OnClick(R.id.btn_subscribe)
public void onViewClicked() {
String topic = mTopic.getText().toString().trim();
try {
MqttTopic.validate(topic, true/*allow wildcards*/);
} catch (IllegalArgumentException e) {
TipUtil.showSnackbar(mLinearLayout, e.getMessage());
return;
}
int qos = mQoSLayout.getQoS();
Subscription subscription = new Subscription(topic, qos);
Intent intent = new Intent();
intent.putExtra(Constant.ExtraConstant.EXTRA_SUBSCRIPTION, subscription);
setResult(RESULT_OK, intent);
finish();
}
示例2: onViewClicked
import org.eclipse.paho.client.mqttv3.MqttTopic; //导入依赖的package包/类
@OnClick(R.id.btn_publish)
public void onViewClicked() {
String topic = mTopic.getText().toString().trim();
try{
MqttTopic.validate(topic, false/*wildcards NOT allowed*/);
}catch (IllegalArgumentException e){
TipUtil.showSnackbar(mLinearLayout, e.getMessage());
return;
}
int qos = mQoSChooseLayout.getQoS();
String payload = mPayload.getText().toString().trim();
boolean isRetained = mRetainedSwitch.isChecked();
Publication publication = new Publication(topic, payload, qos, isRetained);
Intent intent = new Intent();
intent.putExtra(Constant.ExtraConstant.EXTRA_PUBLICATION, publication);
setResult(RESULT_OK, intent);
finish();
}
示例3: testValidTopicFilterWildcards
import org.eclipse.paho.client.mqttv3.MqttTopic; //导入依赖的package包/类
@Test
public void testValidTopicFilterWildcards() throws Exception {
String methodName = Utility.getMethodName();
LoggingUtilities.banner(log, cclass, methodName);
String[] topics = new String[] {
"+", "+/+", "+/foo",
"+/tennis/#",
"foo/+", "foo/+/bar",
"/+", "/+/sport/+/player1",
"#", "/#",
"sport/#",
"sport/tennis/#"
};
for(String topic:topics){
MqttTopic.validate(topic, true);
}
}
示例4: 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
}
}
}
示例5: onAddSubscription
import org.eclipse.paho.client.mqttv3.MqttTopic; //导入依赖的package包/类
@Override
public void onAddSubscription(Subscription subscription) {
try {
MqttTopic.validate(subscription.getTopic(),true);
}catch (IllegalArgumentException e){
TipUtil.showSnackbar(mCoordinatorLayout,e.getMessage());
return;
}
subscription.setConnectionId(mConnection.getId());
mSubscription = subscription;
RealmHelper.getInstance().addSubscription(mSubscription);
subscribe(subscription);
}
示例6: 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();
}
示例7: 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();
}
示例8: 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
示例9: 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();
}
}
示例10: 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();
}
}
示例11: 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);
}
示例12: messageArrived
import org.eclipse.paho.client.mqttv3.MqttTopic; //导入依赖的package包/类
/**
* Received Message from broker
*/
@Override
public void messageArrived(MqttTopic topic, MqttMessage message)
throws Exception {
Log.i(DEBUG_TAG," Topic:\t" + topic.getName() +
" Message:\t" + new String(message.getPayload()) +
" QoS:\t" + message.getQos());
}
示例13: startProducer
import org.eclipse.paho.client.mqttv3.MqttTopic; //导入依赖的package包/类
/**
* Start a registered producer, so that it can start sending messages.
*
* @param publisher
* to start.
*/
private void startProducer(MqttMessageProducer publisher) {
logger.trace("Starting message producer for broker '{}'", name);
publisher.setSenderChannel(new MqttSenderChannel() {
@Override
public void publish(String topic, byte[] payload) throws Exception {
if (!started) {
logger.warn("Broker connection not started. Cannot publish message to topic '{}'", topic);
return;
}
// Create and configure a message
MqttMessage message = new MqttMessage(payload);
message.setQos(qos);
message.setRetained(retain);
// publish message asynchronously
MqttTopic mqttTopic = client.getTopic(topic);
MqttDeliveryToken deliveryToken = mqttTopic.publish(message);
logger.debug("Publishing message {} to topic '{}'", deliveryToken.getMessageId(), topic);
if (!async) {
// wait for publish confirmation
deliveryToken.waitForCompletion(10000);
if (!deliveryToken.isComplete()) {
logger.error(
"Did not receive completion message within timeout limit whilst publishing to topic '{}'",
topic);
}
}
}
});
}
示例14: messageArrived
import org.eclipse.paho.client.mqttv3.MqttTopic; //导入依赖的package包/类
@Override
public void messageArrived(MqttTopic topic, MqttMessage message) throws Exception {
System.out.println("Message arrived. Topic: " + topic.getName() + " Message: " + message.toString());
if(field != null)
field.setValue(new String(message.getPayload()));
}
示例15: messageArrived
import org.eclipse.paho.client.mqttv3.MqttTopic; //导入依赖的package包/类
@Override
public void messageArrived(MqttTopic topic, MqttMessage message)
throws Exception {
pubsub.receiveEvent(topic.toString(), message);
}
开发者ID:chauhansaurabhb,项目名称:EndUserInteractioion_RequestResponse_Command,代码行数:7,代码来源:PushCallback.java