|
|
@ -101,5 +101,84 @@ public class MqttGatewayPublish { |
|
|
|
throw new CloudSDKException(CloudSDKErrorEnum.MQTT_PUBLISH_ABNORMAL, "No message reply received."); |
|
|
|
} |
|
|
|
|
|
|
|
public void publishMegaphone(String topic, int qos, MegaphoneTopicRequest request) { |
|
|
|
try { |
|
|
|
log.debug("send topic: {}, payload: {}", topic, request.toString()); |
|
|
|
byte[] payload = Common.getObjectMapper().writeValueAsBytes(request); |
|
|
|
messageGateway.publish(topic, payload, qos); |
|
|
|
} catch (JsonProcessingException e) { |
|
|
|
log.error("Failed to publish the message. {}", request.toString()); |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public <T> CommonTopicResponse<T> publishPlayTextWithReply(Class<T> clazz, String topic, MegaphoneTopicRequest request, int retryCount, long timeout) { |
|
|
|
AtomicInteger time = new AtomicInteger(0); |
|
|
|
boolean hasBid = StringUtils.hasText(request.getBid()); |
|
|
|
request.setBid(hasBid ? request.getBid() : UUID.randomUUID().toString()); |
|
|
|
// RetryServicesReplyReceiver
|
|
|
|
while (time.getAndIncrement() <= retryCount) { |
|
|
|
this.publishMegaphone(topic, DEFAULT_RETRY_COUNT,request); |
|
|
|
|
|
|
|
// If the message is not received in 3 seconds then resend it again.
|
|
|
|
CommonTopicResponse<T> receiver = Chan.getInstance(request.getTid(), true).get(request.getTid(), timeout); |
|
|
|
// Need to match tid and bid.
|
|
|
|
if (Objects.nonNull(receiver) |
|
|
|
&& receiver.getTid().equals(request.getTid()) |
|
|
|
&& receiver.getBid().equals(request.getBid())) { |
|
|
|
if (clazz.isAssignableFrom(receiver.getData().getClass())) { |
|
|
|
return receiver; |
|
|
|
} |
|
|
|
throw new TypeMismatchException(receiver.getData(), clazz); |
|
|
|
} |
|
|
|
// It must be guaranteed that the tid and bid of each message are different.
|
|
|
|
if (!hasBid) { |
|
|
|
request.setBid(UUID.randomUUID().toString()); |
|
|
|
} |
|
|
|
request.setTid(UUID.randomUUID().toString()); |
|
|
|
} |
|
|
|
throw new CloudSDKException(CloudSDKErrorEnum.MQTT_PUBLISH_ABNORMAL, "No message reply received."); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void publishPlayText(String topic, String reply, int qos, CommonTopicRequest request) { |
|
|
|
try { |
|
|
|
log.debug("send topic: {}, payload: {}", topic, request.toString()); |
|
|
|
byte[] payload = Common.getObjectMapper().writeValueAsBytes(request); |
|
|
|
messageGateway.publish(topic, payload, qos); |
|
|
|
} catch (JsonProcessingException e) { |
|
|
|
log.error("Failed to publish the message. {}", request.toString()); |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void publishPlayText(String topic, int qos, CommonTopicResponse response) { |
|
|
|
try { |
|
|
|
log.debug("send topic: {}, payload: {}", topic, response.toString()); |
|
|
|
byte[] payload = Common.getObjectMapper().writeValueAsBytes(response); |
|
|
|
messageGateway.publish(topic, payload, qos); |
|
|
|
} catch (JsonProcessingException e) { |
|
|
|
log.error("Failed to publish the message. {}", response.toString()); |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void publishPlayText(String topic, String reply, CommonTopicRequest request, int publishCount) { |
|
|
|
AtomicInteger time = new AtomicInteger(0); |
|
|
|
while (time.getAndIncrement() < publishCount) { |
|
|
|
this.publishPlayText(topic, reply, DEFAULT_QOS, request); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void publishPlayText(String topic, String reply, CommonTopicRequest request) { |
|
|
|
this.publishPlayText(topic, reply, DEFAULT_QOS, request); |
|
|
|
} |
|
|
|
|
|
|
|
public void publishPlayTextReply(CommonTopicResponse response, String reply, MessageHeaders headers) { |
|
|
|
this.publishPlayText(headers.get(MqttHeaders.RECEIVED_TOPIC) + reply, 2, response); |
|
|
|
} |
|
|
|
} |
|
|
|