Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples/request response #337

Merged
merged 3 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.concurrent.TimeUnit;

/**
* Small completely asynchronous example.
*
* @author Silvio Giebl
*/
public class AsyncDemo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.util.concurrent.CountDownLatch;

/**
* Shows MQTT 5 features like session expiry, message expiry, user properties, topic aliases, flow control
* Shows MQTT 5 features like session expiry, message expiry, user properties, topic aliases, flow control.
*
* @author Silvio Giebl
*/
Expand Down Expand Up @@ -90,7 +90,7 @@ public static void main(final String[] args)throws InterruptedException {
.retain(true)
.contentType("text/plain") // our payload is text
.messageExpiryInterval(120) // not so important, expire message after 2min if can not be delivered
.userProperties() // add some user properties to the will message
.userProperties() // add some user properties to the message
.add("sender", "demo-sender-1")
.add("receiver", "you")
.applyUserProperties()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
public class ReconnectStrategy {

public static void main(final String[] args) throws InterruptedException {
// defaultReconnect();
// customizedReconnect();
completelyCustom();
}
Expand Down Expand Up @@ -103,7 +104,7 @@ private static CompletableFuture<byte[]> getOAuthToken() {
TimeUnit.SECONDS.sleep(1);
System.out.println("OAuth server is slow to respond ...");
}
} catch (InterruptedException e) {
} catch (final InterruptedException e) {
e.printStackTrace();
}
return new byte[] {1, 2, 3};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2018 dc-square and the HiveMQ MQTT Client Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.hivemq.client.mqtt.examples;

import com.hivemq.client.mqtt.datatypes.MqttQos;
import com.hivemq.client.mqtt.mqtt5.Mqtt5Client;
import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5Publish;

SgtSilvio marked this conversation as resolved.
Show resolved Hide resolved
/**
* Shows how to implement a request/response pattern using response topic and correlation data.
*
* @author Silvio Giebl
*/
public class RequestResponse {

public static void main(final String[] args) {
final Mqtt5Client requester = Mqtt5Client.builder().serverHost("broker.hivemq.com").build();
final Mqtt5Client responder = Mqtt5Client.builder().serverHost("broker.hivemq.com").build();

requester.toBlocking().connect();
responder.toBlocking().connect();

responder.toRx()
.publish(responder.toRx()
.subscribeStreamWith()
.topicFilter("request/topic")
.applySubscribe()
.map(requestPublish -> Mqtt5Publish.builder()
.topic(requestPublish.getResponseTopic().get())
.qos(requestPublish.getQos())
.payload("response".getBytes())
.correlationData(requestPublish.getCorrelationData().orElse(null))
.build()))
.subscribe(); // this call is a reactive streams subscribe call, not an MQTT subscribe

requester.toAsync()
.subscribeWith()
.topicFilter("response/topic")
.callback(responsePublish -> System.out.println("received response"))
.send()
.thenCompose(subAck -> requester.toAsync()
.publishWith()
.topic("request/topic")
.responseTopic("response/topic")
.correlationData("1234".getBytes())
.qos(MqttQos.EXACTLY_ONCE)
.payload("request".getBytes())
.send());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public long getRawMessageExpiryInterval() {
((payloadFormatIndicator == null) ? "" : ", payloadFormatIndicator=" + payloadFormatIndicator) +
((contentType == null) ? "" : ", contentType=" + contentType) +
((responseTopic == null) ? "" : ", responseTopic=" + responseTopic) +
((correlationData == null) ? "" : ", correlationData=" + correlationData) +
((correlationData == null) ? "" : ", correlationData=" + correlationData.remaining() + "byte") +
StringUtil.prepend(", ", super.toAttributeString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public MqttQos2Result(

@Override
public @NotNull String toString() {
return "MqttQos2PublishResult{" + toAttributeString() + '}';
return "MqttQos2Result{" + toAttributeString() + '}';
}

@Override
Expand Down