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 2 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 @@ -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
@@ -0,0 +1,63 @@
/*
* 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
/**
* @author Silvio Giebl
*/
public class RequestResponse {

public static void main(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(publish -> Mqtt5Publish.builder()
SgtSilvio marked this conversation as resolved.
Show resolved Hide resolved
.topic(publish.getResponseTopic().get())
.qos(publish.getQos())
.payload("response".getBytes())
.correlationData(publish.getCorrelationData().orElse(null))
.build()))
.subscribe();
SgtSilvio marked this conversation as resolved.
Show resolved Hide resolved

requester.toAsync()
.subscribeWith()
.topicFilter("response/topic")
.callback(publish -> System.out.println("received response"))
SgtSilvio marked this conversation as resolved.
Show resolved Hide resolved
.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