本文整理汇总了Java中io.netty.handler.codec.mqtt.MqttQoS.AT_LEAST_ONCE属性的典型用法代码示例。如果您正苦于以下问题:Java MqttQoS.AT_LEAST_ONCE属性的具体用法?Java MqttQoS.AT_LEAST_ONCE怎么用?Java MqttQoS.AT_LEAST_ONCE使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类io.netty.handler.codec.mqtt.MqttQoS
的用法示例。
在下文中一共展示了MqttQoS.AT_LEAST_ONCE属性的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testMatches
@Test
public void testMatches() throws Exception {
String testTopic = "testTopic/test";
Session session = new Session("1", "1", 1, 50, true, null);
TopicSubscription ts0 = new TopicSubscription(session.clientId(), "testTopic/#", MqttQoS.AT_MOST_ONCE);
TopicSubscription ts1 = new TopicSubscription(session.clientId(), "testTopic/+", MqttQoS.AT_LEAST_ONCE);
TopicSubscription ts2 = new TopicSubscription(session.clientId(), testTopic, MqttQoS.EXACTLY_ONCE);
TopicSubscription.NEXUS.put(ts0);
TopicSubscription.NEXUS.put(ts1);
TopicSubscription.NEXUS.put(ts2);
Assert.assertEquals(3, TopicSubscription.NEXUS.topicFiltersOf(session.clientId()).size());
TopicSubscription target = session.matches(testTopic);
Assert.assertTrue(target.topicFilter().equals(testTopic));
}
示例2: mapToInternal
/**
* Convert Map to InternalMessage
*
* @param map Map
* @return InternalMessage
*/
public static InternalMessage mapToInternal(Map<String, String> map) {
if (map == null || map.isEmpty()) return null;
int type = Integer.parseInt(map.get("type"));
if (type == MqttMessageType.PUBLISH.value()) {
byte[] payload = null;
if (map.get("payload") != null) try {
payload = map.get("payload").getBytes("ISO-8859-1");
} catch (UnsupportedEncodingException ignore) {
}
return new InternalMessage<>(
MqttMessageType.PUBLISH,
BooleanUtils.toBoolean(map.getOrDefault("dup", "0"), "1", "0"),
MqttQoS.valueOf(Integer.parseInt(map.getOrDefault("qos", "0"))),
BooleanUtils.toBoolean(map.getOrDefault("retain", "0"), "1", "0"),
MqttVersion.valueOf(map.getOrDefault("version", MqttVersion.MQTT_3_1_1.toString())),
map.get("clientId"),
map.get("userName"),
null,
new Publish(
map.get("topicName"),
Integer.parseInt(map.getOrDefault("packetId", "0")),
payload
));
} else if (type == MqttMessageType.PUBREL.value()) {
return new InternalMessage<>(
MqttMessageType.PUBREL,
false,
MqttQoS.AT_LEAST_ONCE,
false,
MqttVersion.valueOf(map.getOrDefault("version", MqttVersion.MQTT_3_1_1.toString())),
map.get("clientId"),
map.get("userName"),
null,
new PacketId(Integer.parseInt(map.getOrDefault("packetId", "0"))));
} else {
throw new IllegalArgumentException("Invalid in-flight MQTT message type: " + MqttMessageType.valueOf(type));
}
}
示例3: createMqttPublishMsg
private MqttPublishMessage createMqttPublishMsg(DeviceSessionCtx ctx, String topic, JsonElement json) {
MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(MqttMessageType.PUBLISH, false, MqttQoS.AT_LEAST_ONCE, false,
0);
MqttPublishVariableHeader header = new MqttPublishVariableHeader(topic, ctx.nextMsgId());
ByteBuf payload = ALLOCATOR.buffer();
payload.writeBytes(GSON.toJson(json).getBytes(UTF8));
return new MqttPublishMessage(mqttFixedHeader, header, payload);
}
示例4: pubrel
public static MqttMessage pubrel(int messageId) {
MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBREL, false, MqttQoS.AT_LEAST_ONCE, false,
2);
MqttMessageIdVariableHeader variableHeader = MqttMessageIdVariableHeader.from(messageId);
return new MqttMessage(fixedHeader, variableHeader);
}
示例5: subscribe
public static MqttSubscribeMessage subscribe(int messageId, MqttTopicSubscription... topicSubscriptions) {
int topicNameSize = 0;
int topicCount = topicSubscriptions.length;
for (MqttTopicSubscription item : topicSubscriptions) {
topicNameSize += item.topicName().getBytes(CharsetUtil.UTF_8).length;
}
MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.SUBSCRIBE, false, MqttQoS.AT_LEAST_ONCE,
false, 2 + topicNameSize + topicCount);
MqttMessageIdVariableHeader variableHeader = MqttMessageIdVariableHeader.from(messageId);
MqttSubscribePayload payload = new MqttSubscribePayload(Lists.newArrayList(topicSubscriptions));
return new MqttSubscribeMessage(fixedHeader, variableHeader, payload);
}
示例6: channelRead0
@Override
protected void channelRead0(ChannelHandlerContext ctx, MqttMessage msg) throws Exception {
switch (msg.fixedHeader().messageType()) {
case PUBLISH:
if (receiver != null) {
receiver.messageReceived(Message.newMessage(client.clientId(), (MqttPublishMessage) msg));
}
int messageId = ((MqttPublishMessage) msg).variableHeader().messageId();
if (((MqttPublishMessage) msg).fixedHeader().qosLevel() == MqttQoS.AT_LEAST_ONCE) {
client.send(MqttMessageFactory.puback(messageId));
}
else if (((MqttPublishMessage) msg).fixedHeader().qosLevel() == MqttQoS.EXACTLY_ONCE) {
client.send(MqttMessageFactory.pubrec(messageId));
}
break;
case CONNACK:
sharedObject.receivedMessage(msg);
synchronized (sharedObject.locker()) {
sharedObject.locker().notify();
}
break;
case PUBREC:
client.send(MqttMessageFactory.pubrel(((MqttMessageIdVariableHeader) msg.variableHeader()).messageId()));
break;
case SUBACK:
case PUBACK:
case PUBCOMP:
default:
break;
}
}
示例7: publishRelease
/**
* Sends the PUBREL message to server
*
* @param publishMessageId identifier of the PUBLISH message to acknowledge
*/
void publishRelease(int publishMessageId) {
MqttFixedHeader fixedHeader =
new MqttFixedHeader(MqttMessageType.PUBREL, false, MqttQoS.AT_LEAST_ONCE, false, 0);
MqttMessageIdVariableHeader variableHeader =
MqttMessageIdVariableHeader.from(publishMessageId);
io.netty.handler.codec.mqtt.MqttMessage pubrel = MqttMessageFactory.newMessage(fixedHeader, variableHeader, null);
qos2outbound.put(publishMessageId, pubrel);
this.write(pubrel);
}
示例8: unsubscribe
@PermitAll
@POST
/**
* Handle MQTT Un-Subscribe Request in RESTful style
*/
public ResultEntity<Boolean> unsubscribe(@PathParam("clientId") String clientId, @Auth UserPrincipal user, @QueryParam("protocol") @DefaultValue("4") byte protocol,
@QueryParam("packetId") @DefaultValue("0") int packetId,
List<String> topics) {
String userName = user.getName();
MqttVersion version = MqttVersion.fromProtocolLevel(protocol);
// HTTP interface require valid Client Id
if (!this.validator.isClientIdValid(clientId)) {
logger.debug("Protocol violation: Client id {} not valid based on configuration", clientId);
throw new ValidateException(new ErrorEntity(ErrorCode.INVALID));
}
// Validate Topic Filter based on configuration
for (String topic : topics) {
if (!this.validator.isTopicFilterValid(topic)) {
logger.debug("Protocol violation: Client {} un-subscription {} is not valid based on configuration", clientId, topic);
throw new ValidateException(new ErrorEntity(ErrorCode.INVALID));
}
}
logger.debug("Message received: Received UNSUBSCRIBE message from client {} user {} topics {}", clientId, userName, ArrayUtils.toString(topics));
// The Topic Filters (whether they contain wildcards or not) supplied in an UNSUBSCRIBE packet MUST be
// compared character-by-character with the current set of Topic Filters held by the Server for the Client. If
// any filter matches exactly then its owning Subscription is deleted, otherwise no additional processing
// occurs
// If a Server deletes a Subscription:
// It MUST stop adding any new messages for delivery to the Client.
//1 It MUST complete the delivery of any QoS 1 or QoS 2 messages which it has started to send to
// the Client.
// It MAY continue to deliver any existing messages buffered for delivery to the Client.
topics.forEach(topic -> {
logger.trace("Remove subscription: Remove client {} subscription with topic {}", clientId, topic);
this.redis.removeSubscription(clientId, Topics.sanitize(topic));
});
// Pass message to 3rd party application
Unsubscribe us = new Unsubscribe(packetId, topics);
InternalMessage<Unsubscribe> m = new InternalMessage<>(MqttMessageType.UNSUBSCRIBE, false, MqttQoS.AT_LEAST_ONCE, false, version, clientId, null, null, us);
this.communicator.sendToApplication(m);
return new ResultEntity<>(true);
}
示例9: from
/**
* Return an AMQP_WILL message from the raw AMQP one
*
* @param message raw AMQP message
* @return AMQP_WILL message
*/
public static AmqpWillMessage from(Message message) {
if (!message.getSubject().equals(AMQP_SUBJECT)) {
throw new IllegalArgumentException(String.format("AMQP message subject is no s%", AMQP_SUBJECT));
}
MessageAnnotations messageAnnotations = message.getMessageAnnotations();
if (messageAnnotations == null) {
throw new IllegalArgumentException("AMQP message has no annotations");
} else {
boolean isRetain = false;
if (messageAnnotations.getValue().containsKey(Symbol.valueOf(AMQP_RETAIN_ANNOTATION))) {
isRetain = (boolean) messageAnnotations.getValue().get(Symbol.valueOf(AMQP_RETAIN_ANNOTATION));
}
MqttQoS qos;
if (messageAnnotations.getValue().containsKey(Symbol.valueOf(AMQP_QOS_ANNOTATION))) {
int value = (int) messageAnnotations.getValue().get(Symbol.valueOf(AMQP_QOS_ANNOTATION));
qos = MqttQoS.valueOf(value);
} else {
if (message.getHeader() != null) {
// if qos annotation isn't present, fallback to "durable" header field
qos = ((message.getHeader().getDurable() == null) || !message.getHeader().getDurable())
? MqttQoS.AT_MOST_ONCE : MqttQoS.AT_LEAST_ONCE;
} else {
qos = MqttQoS.AT_MOST_ONCE;
}
}
String topic = message.getAddress();
Section section = message.getBody();
if ((section != null) && (section instanceof Data)) {
Buffer payload = Buffer.buffer(((Data) section).getValue().getArray());
return new AmqpWillMessage(isRetain, topic, qos, payload);
} else {
throw new IllegalArgumentException("AMQP message wrong body type");
}
}
}
示例10: from
/**
* Return an AMQP_PUBLISH message from the raw AMQP one
*
* @param message raw AMQP message
* @return AMQP_PUBLISH message
*/
public static AmqpPublishMessage from(Message message) {
boolean isRetain = false;
MqttQoS qos = MqttQoS.AT_MOST_ONCE;
// raw AMQP messages published from native AMQP clients could not have annotations
MessageAnnotations messageAnnotations = message.getMessageAnnotations();
if (messageAnnotations == null) {
if (message.getHeader() != null) {
// if qos annotation isn't present, fallback to "durable" header field
qos = ((message.getHeader().getDurable() == null) || !message.getHeader().getDurable())
? MqttQoS.AT_MOST_ONCE : MqttQoS.AT_LEAST_ONCE;
}
} else {
if (messageAnnotations.getValue().containsKey(Symbol.valueOf(AMQP_RETAIN_ANNOTATION))) {
isRetain = (boolean) messageAnnotations.getValue().get(Symbol.valueOf(AMQP_RETAIN_ANNOTATION));
}
if (messageAnnotations.getValue().containsKey(Symbol.valueOf(AMQP_QOS_ANNOTATION))) {
int value = (int) messageAnnotations.getValue().get(Symbol.valueOf(AMQP_QOS_ANNOTATION));
qos = MqttQoS.valueOf(value);
} else {
if (message.getHeader() != null) {
// if qos annotation isn't present, fallback to "durable" header field
qos = ((message.getHeader().getDurable() == null) || !message.getHeader().getDurable())
? MqttQoS.AT_MOST_ONCE : MqttQoS.AT_LEAST_ONCE;
}
}
}
boolean isDup = (message.getDeliveryCount() > 0);
String topic = message.getAddress();
// TODO: to remove
// workaround for the Artemis broker which change the original "To" property
// in the AMQP message when message-id is null
ApplicationProperties applicationProperties = message.getApplicationProperties();
if (applicationProperties != null) {
Object amqOrigAddress = applicationProperties.getValue().get("_AMQ_ORIG_ADDRESS");
topic = (amqOrigAddress != null) ? amqOrigAddress.toString() : topic;
}
Section section = message.getBody();
if ((section != null) && (section instanceof Data)) {
Buffer payload = Buffer.buffer(((Data) section).getValue().getArray());
return new AmqpPublishMessage(message.getMessageId(), qos, isDup, isRetain, topic, payload);
} else {
throw new IllegalArgumentException("AMQP message wrong body type");
}
}
示例11: publishRelease
public MqttEndpointImpl publishRelease(int publishMessageId) {
this.checkConnected();
MqttFixedHeader fixedHeader =
new MqttFixedHeader(MqttMessageType.PUBREL, false, MqttQoS.AT_LEAST_ONCE, false, 0);
MqttMessageIdVariableHeader variableHeader =
MqttMessageIdVariableHeader.from(publishMessageId);
io.netty.handler.codec.mqtt.MqttMessage pubrel = MqttMessageFactory.newMessage(fixedHeader, variableHeader, null);
this.write(pubrel);
return this;
}
示例12: createPublishMessage
private MqttPublishMessage createPublishMessage() {
MqttFixedHeader mqttFixedHeader =
new MqttFixedHeader(MqttMessageType.PUBLISH, false, MqttQoS.AT_LEAST_ONCE, true, 0);
MqttPublishVariableHeader mqttPublishVariableHeader = new MqttPublishVariableHeader(MQTT_TOPIC, 1);
ByteBuf payload = ALLOCATOR.buffer();
payload.writeBytes(MQTT_MESSAGE.getBytes(CharsetUtil.UTF_8));
return new MqttPublishMessage(mqttFixedHeader, mqttPublishVariableHeader, payload);
}
示例13: createPublishMessage
private MqttPublishMessage createPublishMessage() {
MqttFixedHeader mqttFixedHeader =
new MqttFixedHeader(MqttMessageType.PUBLISH, false, MqttQoS.AT_LEAST_ONCE, true, 0);
MqttPublishVariableHeader mqttPublishVariableHeader = new MqttPublishVariableHeader(MQTT_TOPIC, 1);
ByteBuf payload = ALLOCATOR.buffer();
payload.writeBytes(MQTT_MESSAGE.getBytes(CharsetUtil.UTF_8));
return new MqttPublishMessage(mqttFixedHeader, mqttPublishVariableHeader, payload);
}