本文整理汇总了Java中io.netty.handler.codec.mqtt.MqttMessage类的典型用法代码示例。如果您正苦于以下问题:Java MqttMessage类的具体用法?Java MqttMessage怎么用?Java MqttMessage使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MqttMessage类属于io.netty.handler.codec.mqtt包,在下文中一共展示了MqttMessage类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handle
import io.netty.handler.codec.mqtt.MqttMessage; //导入依赖的package包/类
protected void handle(ChannelHandlerContext ctx, Session session, int messageId) {
// TODO what if the PUBREL is resented one?
Topic topic = Topic.NEXUS.get(session.clientId(), messageId, ClientType.PUBLISHER);
if (topic == null) {
logger.error("PUBREL target does not exist [clientId={}, messageId={}]", session.clientId(), messageId);
session.dispose(true); // [MQTT-3.3.5-2]
return;
}
MqttMessage toSend = MqttMessageFactory.pubcomp(messageId);
final String log = toSend.toString();
session.send(toSend, f -> {
if (!f.isSuccess()) {
logger.error("packet outgoing failed [{}] {}", log, f.cause());
return;
}
InboundMessageStatus.NEXUS.removeByKey(messageId, session.clientId());
logger.debug("Inbound message status REMOVED [clientId={}, messageId={}]", session.clientId(), messageId);
});
}
示例2: send
import io.netty.handler.codec.mqtt.MqttMessage; //导入依赖的package包/类
protected void send(MqttMessage message, GenericFutureListener<? extends Future<? super Void>> completeListener) {
if (!session.isConnected(true)) {
logger.error("Message is not sent - Channel is inactive or out of the node. [{}]", message);
return;
}
ChannelHandlerContext ctx = Session.NEXUS.channelHandlerContext(session.clientId());
String log = message.toString();
ChannelFuture cf = ctx.writeAndFlush(message).addListener(f -> {
if (f.isSuccess()) {
logger.debug("packet outgoing [{}]", log);
}
else {
logger.error("packet outgoing failed [{}] {}", log, f.cause());
}
});
if (completeListener != null) {
cf.addListener(completeListener);
}
}
示例3: onNext
import io.netty.handler.codec.mqtt.MqttMessage; //导入依赖的package包/类
/**
* Provides the Observer with a new item to observe.
* <p>
* The {@link com.caricah.iotracah.core.modules.Worker} may call this method 0 or more times.
* <p>
* The {@code Observable} will not call this method again after it calls either {@link #onCompleted} or
* {@link #onError}.
*
* @param ioTMessage the item emitted by the Observable
*/
@Override
public void onNext(IOTMessage ioTMessage) {
if(null == ioTMessage || !Protocol.MQTT.equals(ioTMessage.getProtocol())){
return;
}
log.debug(" MqttServer onNext : message outbound {}", ioTMessage);
MqttMessage mqttMessage = toServerMessage(ioTMessage);
if(null == mqttMessage){
log.debug(" MqttServer onNext : ignoring outbound message {}", ioTMessage);
}else {
serverImpl.pushToClient(ioTMessage.getConnectionId(), mqttMessage);
}
serverImpl.postProcess(ioTMessage);
}
示例4: channelRead
import io.netty.handler.codec.mqtt.MqttMessage; //导入依赖的package包/类
@Override
public void channelRead(ChannelHandlerContext ctx, Object message) {
MqttMessage msg = (MqttMessage) message;
MqttMessageType messageType = msg.fixedHeader().messageType();
try {
switch (messageType) {
case PUBLISH:
LOG.info("Received a message of type {}", messageType);
handlePublish((MqttPublishMessage) msg);
return;
default:
LOG.info("Received a message of type {}", messageType);
}
} catch (Exception ex) {
LOG.error("Bad error in processing the message", ex);
}
}
示例5: channelRead
import io.netty.handler.codec.mqtt.MqttMessage; //导入依赖的package包/类
@Override
public void channelRead(ChannelHandlerContext ctx, Object message) {
MqttMessage msg = (MqttMessage) message;
MqttMessageType type = msg.fixedHeader().messageType();
try {
switch (type) {
case PUBLISH:
LOG.info("Received a message of type {}", type);
handlePublish((MqttPublishMessage) msg);
return;
default:
LOG.info("Received a message of type {}", type);
}
} catch (Exception ex) {
LOG.error("Bad error in processing the message", ex);
}
}
示例6: channelRead
import io.netty.handler.codec.mqtt.MqttMessage; //导入依赖的package包/类
@Override
public void channelRead(ChannelHandlerContext ctx, Object message) {
MqttMessage msg = (MqttMessage) message;
MqttMessageType messageType = msg.fixedHeader().messageType();
switch (messageType) {
case PUBLISH:
this.publishesMetrics.mark();
break;
case SUBSCRIBE:
this.subscribeMetrics.mark();
break;
case CONNECT:
this.connectedClientsMetrics.inc();
break;
case DISCONNECT:
this.connectedClientsMetrics.dec();
break;
default:
break;
}
ctx.fireChannelRead(message);
}
示例7: testWillMessageIsWiredOnClientKeepAliveExpiry
import io.netty.handler.codec.mqtt.MqttMessage; //导入依赖的package包/类
@Test
public void testWillMessageIsWiredOnClientKeepAliveExpiry() throws Exception {
LOG.info("*** testWillMessageIsWiredOnClientKeepAliveExpiry ***");
String willTestamentTopic = "/will/test";
String willTestamentMsg = "Bye bye";
m_willSubscriber.connect();
m_willSubscriber.subscribe(willTestamentTopic, 0);
m_client.clientId("FAKECLNT").connect(willTestamentTopic, willTestamentMsg);
long connectTime = System.currentTimeMillis();
Awaitility.await()
.atMost(7, TimeUnit.SECONDS)
.untilAsserted(() -> {
// but after the 2 KEEP ALIVE timeout expires it gets fired,
// NB it's 1,5 * KEEP_ALIVE so 3 secs and some millis to propagate the message
org.eclipse.paho.client.mqttv3.MqttMessage msg = m_messageCollector.getMessageImmediate();
assertNotNull("the will message should be fired after keep alive!", msg);
// the will message hasn't to be received before the elapsing of Keep Alive timeout
assertTrue(System.currentTimeMillis() - connectTime > 3000);
assertEquals(willTestamentMsg, new String(msg.getPayload(), StandardCharsets.UTF_8));
});
m_willSubscriber.disconnect();
}
示例8: testWillMessageIsPublishedOnClientBadDisconnection
import io.netty.handler.codec.mqtt.MqttMessage; //导入依赖的package包/类
@Test
public void testWillMessageIsPublishedOnClientBadDisconnection() throws InterruptedException, MqttException {
LOG.info("*** testWillMessageIsPublishedOnClientBadDisconnection ***");
String willTestamentTopic = "/will/test";
String willTestamentMsg = "Bye bye";
m_willSubscriber.connect();
m_willSubscriber.subscribe(willTestamentTopic, 0);
m_client.clientId("FAKECLNT").connect(willTestamentTopic, willTestamentMsg);
// kill will publisher
m_client.close();
// Verify will testament is published
org.eclipse.paho.client.mqttv3.MqttMessage receivedTestament = m_messageCollector.waitMessage(1);
assertEquals(willTestamentMsg, new String(receivedTestament.getPayload()));
m_willSubscriber.disconnect();
}
示例9: sendMessage
import io.netty.handler.codec.mqtt.MqttMessage; //导入依赖的package包/类
/**
* Send MQTT message to specific client
*
* @param msg MQTT Message to be sent
* @param clientId Client Id
* @param packetId Packet Id
* @param flush Flush?
*/
public void sendMessage(MqttMessage msg, String clientId, Integer packetId, boolean flush) {
ChannelHandlerContext ctx = getSession(clientId);
if (ctx == null) {
String pid = packetId == null || packetId <= 0 ? "" : String.valueOf(packetId);
logger.debug("Message failed: Message {} {} failed to send to {}: Client not connected to this node", msg.fixedHeader().messageType(), pid, clientId);
return;
}
sendMessage(ctx, msg, clientId, packetId, flush);
}
示例10: channelRead
import io.netty.handler.codec.mqtt.MqttMessage; //导入依赖的package包/类
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
log.info("[{}] Processing msg: {}", sessionId, msg);
if (msg instanceof MqttMessage) {
MqttMessage mqttMessage = (MqttMessage) msg;
MqttFixedHeader fixedHeader = mqttMessage.fixedHeader();
if (fixedHeader != null) {
processMqttMsg(ctx, (MqttMessage) msg);
} else {
//xtx
}
}
}
示例11: processMqttMsg
import io.netty.handler.codec.mqtt.MqttMessage; //导入依赖的package包/类
private void processMqttMsg(ChannelHandlerContext ctx, MqttMessage msg) {
// deviceSessionCtx.setChannel(ctx);
// assetSessionCtx.setChannel(ctx);
switch (msg.fixedHeader().messageType()) {
case CONNECT:
processConnect(ctx, (MqttConnectMessage) msg);
break;
case PUBLISH:
processPublish(ctx, (MqttPublishMessage) msg);
// System.out.println("write...");
// ctx.write("just for test");
break;
case SUBSCRIBE:
processSubscribe(ctx, (MqttSubscribeMessage) msg);
break;
case UNSUBSCRIBE:
processUnsubscribe(ctx, (MqttUnsubscribeMessage) msg);
break;
case PINGREQ:
if (checkConnected(ctx)) {
ctx.writeAndFlush(new MqttMessage(new MqttFixedHeader(PINGRESP, false, AT_MOST_ONCE, false, 0)));
}
break;
case DISCONNECT:
if (checkConnected(ctx)) {
processDisconnect(ctx);
}
break;
}
}
示例12: channelRead
import io.netty.handler.codec.mqtt.MqttMessage; //导入依赖的package包/类
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof MqttMessage) {
MqttMessage mqtt = (MqttMessage) msg;
if (StringUtils.isBlank(this.clientId) && mqtt.fixedHeader().messageType() == MqttMessageType.CONNECT) {
this.clientId = ((MqttConnectPayload) mqtt.payload()).clientId();
}
if (StringUtils.isNotBlank(this.clientId)) {
this.metrics.measurement(this.clientId, this.brokerId, MessageDirection.IN, mqtt.fixedHeader().messageType());
}
this.metrics.measurement(this.brokerId, MessageDirection.IN, mqtt.fixedHeader().messageType());
}
ctx.fireChannelRead(msg);
}
示例13: write
import io.netty.handler.codec.mqtt.MqttMessage; //导入依赖的package包/类
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof MqttMessage) {
MqttMessage mqtt = (MqttMessage) msg;
if (StringUtils.isNotBlank(this.clientId)) {
this.metrics.measurement(this.clientId, this.brokerId, MessageDirection.OUT, mqtt.fixedHeader().messageType());
}
this.metrics.measurement(this.brokerId, MessageDirection.OUT, mqtt.fixedHeader().messageType());
}
ctx.write(msg, promise);
}
示例14: doMessage
import io.netty.handler.codec.mqtt.MqttMessage; //导入依赖的package包/类
public MqttMessage doMessage(Channel channel, MqttMessage msg) {
String channelId = channel.id().asLongText();
logger.debug("MQTT PINGREQ " + channelId);
// 更新最新连接时间
ApplicationContext.updateChannelConTime(channelId);
MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PINGRESP, false, MqttQoS.AT_MOST_ONCE, false, 0);
MqttMessage message = new MqttMessage(fixedHeader);
return message;
}
示例15: doMessage
import io.netty.handler.codec.mqtt.MqttMessage; //导入依赖的package包/类
public MqttMessage doMessage(MqttMessage msg) {
logger.debug("MQTT PUBACK");
MqttPublishVariableHeader publishVariableHeader = (MqttPublishVariableHeader) msg.variableHeader();
int packetId = publishVariableHeader.packetId();
ManagerHandler.removeSendedMessage(packetId);
return null;
}