本文整理汇总了Java中io.netty.handler.codec.mqtt.MqttQoS类的典型用法代码示例。如果您正苦于以下问题:Java MqttQoS类的具体用法?Java MqttQoS怎么用?Java MqttQoS使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MqttQoS类属于io.netty.handler.codec.mqtt包,在下文中一共展示了MqttQoS类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: updateSubscription
import io.netty.handler.codec.mqtt.MqttQoS; //导入依赖的package包/类
@Override
public void updateSubscription(String clientId, List<String> topicLevels, MqttQoS qos) {
if (Topics.isTopicFilter(topicLevels)) {
boolean b1 = hash().hset(RedisKey.subscription(clientId), String.join("/", topicLevels), String.valueOf(qos.value()));
boolean b2 = hash().hset(RedisKey.topicFilter(topicLevels), clientId, String.valueOf(qos.value()));
if (b1 && b2) {
List<String> keys = new ArrayList<>();
List<String> argv = new ArrayList<>();
// topic filter tree
for (int i = 0; i < topicLevels.size(); i++) {
keys.add(RedisKey.topicFilterChild(topicLevels.subList(0, i)));
argv.add(topicLevels.get(i));
}
script().eval("local length = table.getn(KEYS)\n" +
"for i = 1, length do\n" +
" redis.call('HINCRBY', KEYS[i], ARGV[i], 1)\n" +
"end\n" +
"return redis.status_reply('OK')",
ScriptOutputType.STATUS, keys.toArray(new String[keys.size()]), argv.toArray(new String[argv.size()]));
}
} else {
hash().hset(RedisKey.subscription(clientId), String.join("/", topicLevels), String.valueOf(qos.value()));
hash().hset(RedisKey.topicName(topicLevels), clientId, String.valueOf(qos.value()));
}
}
示例2: start
import io.netty.handler.codec.mqtt.MqttQoS; //导入依赖的package包/类
@Override
public void start() throws Exception {
MqttClient client = MqttClient.create(vertx);
client.rxConnect(PORT, HOST)
.flatMapPublisher(ack ->
Flowable.interval(1, TimeUnit.SECONDS)
.flatMapSingle(l -> {
JsonObject payload = new JsonObject()
.put("uuid", id)
.put("data",
random.nextInt(100));
return client
.rxPublish("/data",
Buffer.buffer(payload.encode()),
MqttQoS.AT_MOST_ONCE, false, false);
}))
.subscribe();
}
示例3: put
import io.netty.handler.codec.mqtt.MqttQoS; //导入依赖的package包/类
public static void put(String topic, String clientId, MqttQoS qos) {
Message message = new Message();
message.setClientId(clientId);
message.setQoS(qos);
LinkedList<Message> messages;
if (!container.containsKey(topic)) {
messages = new LinkedList<>();
messages.add(message);
container.put(topic, messages);
} else {
messages = container.get(topic);
if (!messages.contains(message)) {
messages.add(message);
}
}
}
示例4: testOnUnauthenticatedMessageDoesNotWaitForAcceptedOutcome
import io.netty.handler.codec.mqtt.MqttQoS; //导入依赖的package包/类
/**
* Verifies that the adapter does not wait for a telemetry message being settled and accepted
* by a downstream peer.
*
* @param ctx The vert.x test context.
*/
@Test
public void testOnUnauthenticatedMessageDoesNotWaitForAcceptedOutcome(final TestContext ctx) {
// GIVEN an adapter with a downstream telemetry consumer
final Future<ProtonDelivery> outcome = Future.succeededFuture(mock(ProtonDelivery.class));
givenATelemetrySenderForOutcome(outcome);
MqttServer server = getMqttServer(false);
AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = getAdapter(server);
// WHEN a device publishes a telemetry message
final Buffer payload = Buffer.buffer("some payload");
final MqttEndpoint endpoint = mock(MqttEndpoint.class);
final MqttPublishMessage messageFromDevice = mock(MqttPublishMessage.class);
when(messageFromDevice.topicName()).thenReturn("telemetry/my-tenant/4712");
when(messageFromDevice.qosLevel()).thenReturn(MqttQoS.AT_MOST_ONCE);
when(messageFromDevice.payload()).thenReturn(payload);
adapter.onUnauthenticatedMessage(endpoint, messageFromDevice).setHandler(ctx.asyncAssertSuccess());
}
示例5: testGetDownstreamMessageMapsKuraControlMessagesToTelemetryApi
import io.netty.handler.codec.mqtt.MqttQoS; //导入依赖的package包/类
/**
* Verifies that the adapter maps control messages with QoS 0 published from a Kura gateway to
* the Telemetry endpoint.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testGetDownstreamMessageMapsKuraControlMessagesToTelemetryApi(final TestContext ctx) {
// GIVEN an adapter configured to use the standard topic.control-prefix $EDC
// and a custom control message content type
config.setCtrlMsgContentType("control-msg");
// WHEN a message is published to a topic with the Kura $EDC prefix as endpoint
final MqttPublishMessage message = newMessage(MqttQoS.AT_MOST_ONCE, "$EDC/my-scope/4711");
final Async determineAddressSuccess = ctx.async();
Future<Message> msgTracker = adapter.getDownstreamMessage(message).map(msg -> {
determineAddressSuccess.complete();
return msg;
});
// THEN the message is forwarded to the telemetry API
// and has the custom control message content type
determineAddressSuccess.await(2000);
assertMessageProperties(msgTracker.result(), config.getCtrlMsgContentType(),
TelemetryConstants.TELEMETRY_ENDPOINT, "my-scope", "4711");
}
示例6: testGetDownstreamMessageRecognizesControlMessagesWithCustomControlPrefix
import io.netty.handler.codec.mqtt.MqttQoS; //导入依赖的package包/类
/**
* Verifies that the adapter recognizes control messages published to a topic with a custom control prefix.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testGetDownstreamMessageRecognizesControlMessagesWithCustomControlPrefix(final TestContext ctx) {
// GIVEN an adapter configured to use a custom topic.control-prefix
config.setControlPrefix("bumlux");
// WHEN a message is published to a topic with the custom prefix as endpoint
final MqttPublishMessage message = newMessage(MqttQoS.AT_MOST_ONCE, "bumlux/my-scope/4711");
final Async determineAddressSuccess = ctx.async();
Future<Message> msgTracker = adapter.getDownstreamMessage(message).map(msg -> {
determineAddressSuccess.complete();
return msg;
});
// THEN the message is recognized as a control message and forwarded to the event API
determineAddressSuccess.await(2000);
assertMessageProperties(msgTracker.result(), config.getCtrlMsgContentType(),
TelemetryConstants.TELEMETRY_ENDPOINT, "my-scope", "4711");
}
示例7: testGetDownstreamMessageMapsKuraDataMessagesToTelemetryApi
import io.netty.handler.codec.mqtt.MqttQoS; //导入依赖的package包/类
/**
* Verifies that the adapter forwards data messages with QoS 0 published from a Kura gateway to
* the Telemetry endpoint.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testGetDownstreamMessageMapsKuraDataMessagesToTelemetryApi(final TestContext ctx) {
// GIVEN an adapter configured with a custom data message content type
config.setDataMsgContentType("data-msg");
// WHEN a message is published to an application topic with QoS 0
final MqttPublishMessage message = newMessage(MqttQoS.AT_MOST_ONCE, "my-scope/4711");
final Async determineAddressSuccess = ctx.async();
Future<Message> msgTracker = adapter.getDownstreamMessage(message).map(msg -> {
determineAddressSuccess.complete();
return msg;
});
// THEN the message is forwarded to the telemetry API
// and has the configured data message content type
determineAddressSuccess.await(2000);
assertMessageProperties(msgTracker.result(), config.getDataMsgContentType(),
TelemetryConstants.TELEMETRY_ENDPOINT, "my-scope", "4711");
}
示例8: testGetDownstreamMessageMapsKuraDataMessagesToEventApi
import io.netty.handler.codec.mqtt.MqttQoS; //导入依赖的package包/类
/**
* Verifies that the adapter forwards application messages with QoS 1 published from a Kura gateway to
* the Event endpoint.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testGetDownstreamMessageMapsKuraDataMessagesToEventApi(final TestContext ctx) {
// GIVEN an adapter
// WHEN a message is published to an application topic with QoS 1
final MqttPublishMessage message = newMessage(MqttQoS.AT_LEAST_ONCE, "my-scope/4711");
final Async determineAddressSuccess = ctx.async();
Future<Message> msgTracker = adapter.getDownstreamMessage(message).map(msg -> {
determineAddressSuccess.complete();
return msg;
});
// THEN the message is forwarded to the event API
determineAddressSuccess.await(2000);
assertMessageProperties(msgTracker.result(), config.getDataMsgContentType(),
EventConstants.EVENT_ENDPOINT, "my-scope", "4711");
}
示例9: testGetDownstreamMessageFailsForUnknownEndpoint
import io.netty.handler.codec.mqtt.MqttQoS; //导入依赖的package包/类
/**
* Verifies that the adapter rejects messages published to topics containing an endpoint
* other than <em>telemetry</em> or <em>event</em>.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testGetDownstreamMessageFailsForUnknownEndpoint(final TestContext ctx) {
// GIVEN an adapter
// WHEN a device publishes a message to a topic with an unknown endpoint
final MqttPublishMessage message = newMessage(MqttQoS.AT_MOST_ONCE, "unknown");
final Async determineAddressFailure = ctx.async();
adapter.getDownstreamMessage(message).recover(t -> {
determineAddressFailure.complete();
return Future.failedFuture(t);
});
// THEN no downstream sender can be created for the message
determineAddressFailure.await(2000);
}
示例10: testGetDownstreamMessageFailsForQoS1TelemetryMessage
import io.netty.handler.codec.mqtt.MqttQoS; //导入依赖的package包/类
/**
* Verifies that the adapter rejects QoS 1 messages published to the <em>telemetry</em> endpoint.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testGetDownstreamMessageFailsForQoS1TelemetryMessage(final TestContext ctx) {
// GIVEN an adapter
// WHEN a device publishes a message with QoS 1 to a "telemetry" topic
final MqttPublishMessage message = newMessage(MqttQoS.AT_LEAST_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT);
final Async determineAddressFailure = ctx.async();
adapter.getDownstreamMessage(message).recover(t -> {
determineAddressFailure.complete();
return Future.failedFuture(t);
});
// THEN no downstream sender can be created for the message
determineAddressFailure.await(2000);
}
示例11: testGetDownstreamMessageFailsForQoS0EventMessage
import io.netty.handler.codec.mqtt.MqttQoS; //导入依赖的package包/类
/**
* Verifies that the adapter rejects QoS 1 messages published to the <em>telemetry</em> endpoint.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testGetDownstreamMessageFailsForQoS0EventMessage(final TestContext ctx) {
// GIVEN an adapter
// WHEN a device publishes a message with QoS 0 to an "event" topic
final MqttPublishMessage message = newMessage(MqttQoS.AT_MOST_ONCE, EventConstants.EVENT_ENDPOINT);
final Async messageFailure = ctx.async();
adapter.getDownstreamMessage(message).recover(t -> {
messageFailure.complete();
return Future.failedFuture(t);
});
// THEN no downstream sender can be created for the message
messageFailure.await(2000);
}
示例12: testGetDownstreamMessageFailsForMissingTenant
import io.netty.handler.codec.mqtt.MqttQoS; //导入依赖的package包/类
/**
* Verifies that the adapter fails to map a topic without a tenant ID received from an anonymous device.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testGetDownstreamMessageFailsForMissingTenant(final TestContext ctx) {
// GIVEN an adapter
// WHEN an anonymous device publishes a message to a topic that does not contain a tenant ID
final MqttPublishMessage message = newMessage(MqttQoS.AT_MOST_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT);
final Async determineAddressFailure = ctx.async();
adapter.getDownstreamMessage(message).recover(t -> {
determineAddressFailure.complete();
return Future.failedFuture(t);
});
// THEN the message cannot be mapped to an address
determineAddressFailure.await(2000);
}
示例13: testGetDownstreamMessageFailsForMissingDeviceId
import io.netty.handler.codec.mqtt.MqttQoS; //导入依赖的package包/类
/**
* Verifies that the adapter fails to map a topic without a device ID received from an anonymous device.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testGetDownstreamMessageFailsForMissingDeviceId(final TestContext ctx) {
// GIVEN an adapter
// WHEN an anonymous device publishes a message to a topic that does not contain a device ID
final MqttPublishMessage message = newMessage(MqttQoS.AT_MOST_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT + "/my-tenant");
final Async determineAddressFailure = ctx.async();
adapter.getDownstreamMessage(message).recover(t -> {
determineAddressFailure.complete();
return Future.failedFuture(t);
});
// THEN the message cannot be mapped to an address
determineAddressFailure.await(2000);
}
示例14: testGetDownstreamMessageUsesDeviceIdentityForTopicWithoutTenant
import io.netty.handler.codec.mqtt.MqttQoS; //导入依赖的package包/类
/**
* Verifies that the adapter uses an authenticated device's identity when mapping a topic without tenant ID.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testGetDownstreamMessageUsesDeviceIdentityForTopicWithoutTenant(final TestContext ctx) {
// GIVEN an adapter
// WHEN an authenticated device publishes a message to a topic that does not contain a tenant ID
final MqttPublishMessage message = newMessage(MqttQoS.AT_MOST_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT);
final Async determineAddressSuccess = ctx.async();
Future<Message> downstreamMessage = adapter.getDownstreamMessage(message, new Device("my-tenant", "4711")).map(msg -> {
determineAddressSuccess.complete();
return msg;
});
// THEN the mapped address contains the authenticated device's tenant and device ID
determineAddressSuccess.await(2000);
final ResourceIdentifier downstreamAddress = ResourceIdentifier.fromString(downstreamMessage.result().getAddress());
assertThat(downstreamAddress.getEndpoint(), is(TelemetryConstants.TELEMETRY_ENDPOINT));
assertThat(downstreamAddress.getTenantId(), is("my-tenant"));
assertThat(MessageHelper.getDeviceId(downstreamMessage.result()), is("4711"));
}
示例15: connect
import io.netty.handler.codec.mqtt.MqttQoS; //导入依赖的package包/类
public static MqttConnectMessage connect(ConnectOptions options) {
MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, false, MqttQoS.AT_MOST_ONCE, false,
10);
MqttConnectVariableHeader variableHeader = new MqttConnectVariableHeader(options.version().protocolName(),
options.version().protocolLevel(), options.userName() != null, options.password() != null,
options.will() == null ? false : options.will().isRetain(),
options.will() == null ? 0 : options.will().qos().value(), options.will() != null,
options.cleanSession(), options.keepAliveTimeSeconds());
MqttConnectPayload payload = new MqttConnectPayload(Strings.nullToEmpty(options.clientId()),
options.will() == null ? "" : options.will().topicName(),
options.will() == null ? "" : new String(options.will().message(), CharsetUtil.UTF_8),
Strings.nullToEmpty(options.userName()), Strings.nullToEmpty(options.password()));
return new MqttConnectMessage(fixedHeader, variableHeader, payload);
}