org.eclipse.paho.client.mqttv3.MqttTopic Java Examples

The following examples show how to use org.eclipse.paho.client.mqttv3.MqttTopic. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SubscriptionActivity.java    From EMQ-Android-Toolkit with Apache License 2.0 6 votes vote down vote up
@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();

}
 
Example #2
Source File: PublicationActivity.java    From EMQ-Android-Toolkit with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #3
Source File: MQTT_PVConn.java    From phoebus with Eclipse Public License 1.0 6 votes vote down vote up
public void publishTopic(String topicStr, String pubMsg, int pubQoS, boolean retained) throws Exception
{
    if (!connect())
    {
        PV.logger.log(Level.WARNING, "Could not publish to mqtt topic \"" + topicStr
                + "\" due to no broker connection");
        throw new Exception("MQTT publish failed: no broker connection");
    }

    MqttTopic topic = myClient.getTopic(topicStr);
    MqttMessage message = new MqttMessage(pubMsg.getBytes());
    message.setQos(pubQoS);
    message.setRetained(retained);

    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 ex) {
        throw new Exception("Failed to publish message to broker", ex);
    }
}
 
Example #4
Source File: DashboardActivity.java    From EMQ-Android-Toolkit with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #5
Source File: ApplicationTopicsManager.java    From helloiot with GNU General Public License v3.0 5 votes vote down vote up
private void distributeMessage(EventMessage message) {
    for (Map.Entry<String, List<TopicSubscription>> entry: subscriptions.entrySet()) {
        if (MqttTopic.isMatched(entry.getKey(), message.getTopic())) {
            List<TopicSubscription> subs = entry.getValue();
            if (subs != null) {
                for (TopicSubscription s : subs) {
                    s.consume(message);
                }
            }
        }
    }
}
 
Example #6
Source File: MqttService.java    From android-mqtt-service with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example #7
Source File: MqttBrokerConnection.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * 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.warn(
                            "Did not receive completion message within timeout limit while publishing to topic '{}'",
                            topic);
                }
            }

        }
    });

}
 
Example #8
Source File: MqttTestClient.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public MqttTopic getTopic(String topic) {
    return null;
}
 
Example #9
Source File: MqttClientFactory.java    From enmasse with Apache License 2.0 4 votes vote down vote up
@Override
public MqttTopic getTopic(String topic) {
    return this.mqttClient.getTopic(topic);
}
 
Example #10
Source File: MqttTestClient.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public MqttTopic getTopic(String topic) {
    return null;
}