public MQTTQueueMessage(String topic, MqttMessage message) { this.topic = topic; payload = message.getPayload(); qos = message.getQos(); retained = message.isRetained(); duplicate = message.isDuplicate(); }
public void publishMessage(String topic, String content, Integer qos, Boolean retain) { MqttMessage message = new MqttMessage(StringEscapeUtils.unescapeJava(content).getBytes()); message.setQos(Optional.ofNullable(qos).orElse(1)); message.setRetained(Optional.ofNullable(retain).orElse(false)); try { if(!myClient.isConnected()) { try { myClient.connect(); } catch (MqttSecurityException e1) { log.error("Could not retry connect to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e1.getMessage()); return; } catch (MqttException e1) { log.error("Could not retry connect to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e1.getMessage()); return; } } myClient.publish(topic, message); } catch (MqttException e) { log.error("Could not publish to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage()); } }
private MqttMessage getMessage( Object[] row ) throws KettleStepException { MqttMessage mqttMessage = new MqttMessage(); try { mqttMessage.setQos( Integer.parseInt( meta.qos ) ); } catch ( NumberFormatException e ) { throw new KettleStepException( getString( PKG, "MQTTProducer.Error.QOS", meta.qos ) ); } mqttMessage.setPayload( getField( row, meta.messageField ) .map( fas -> fas.getBytes( Charsets.UTF_8 ) ) .orElse( null ) ); //allow nulls to pass through return mqttMessage; }
public void publish(String channel, String message) { channel = channel.replaceAll("#", ""); DAO.log("Publishing " + message.substring(0, 10) + " to " + channel); MqttMessage msg = new MqttMessage(message.getBytes()); msg.setQos(this.qos); try { this.client.publish(channel, msg); } catch (MqttException e) { DAO.severe("Failed to publish message to channel " + channel + " due to MQTT exception", e); } catch (IllegalArgumentException e) { DAO.severe("Failed to publish message to channel " + channel + " because of invalid channel name", e); } catch (Exception e) { DAO.severe("Failed to publish message to channel " + channel + " due to unknown exception", e); } }
/** * @see org.eclipse.paho.client.mqttv3.MqttCallback#messageArrived(java.lang.String, org.eclipse.paho.client.mqttv3.MqttMessage) */ @Override public void messageArrived(String topic, MqttMessage message) throws Exception { //Get connection object associated with this object Connection c = Connections.getInstance(context).getConnection(clientHandle); c.messageArrived(topic, message); //get the string from strings.xml and format String messageString = context.getString(R.string.messageRecieved, new String(message.getPayload()), topic+";qos:"+message.getQos()+";retained:"+message.isRetained()); Log.i(TAG, messageString); //update client history c.addAction(messageString); }
@Override public void messageArrived( String topic, MqttMessage message ) throws Exception { acceptRows( singletonList( ImmutableList.of( new String( message.getPayload(), Charsets.UTF_8 ), topic ) ) ); }
public void publishMessage(){ try { MqttMessage message = new MqttMessage(); message.setPayload(publishMessage.getBytes()); mqttAndroidClient.publish(publishTopic, message); addToHistory("Message Published"); if(!mqttAndroidClient.isConnected()){ addToHistory(mqttAndroidClient.getBufferedMessageCount() + " messages in buffer."); } } catch (MqttException e) { System.err.println("Error Publishing: " + e.getMessage()); e.printStackTrace(); } }
final MqttMessage mqttMessage = new MqttMessage(messageContent); mqttMessage.setQos(qos); mqttMessage.setPayload(messageContent); mqttMessage.setRetained(context.getProperty(PROP_RETAIN).evaluateAttributeExpressions(flowfile).asBoolean());
@Test public void checkSubscriberQoS1ReceiveQoS1publishes() throws Exception { LOG.info("*** checkSubscriberQoS1ReceiveQoS1publishes ***"); m_subscriber.subscribe("/topic", 1); m_publisher.publish("/topic", "Hello world MQTT QoS1".getBytes(UTF_8), 1, false); MqttMessage message = m_callback.waitMessage(1); assertEquals("Hello world MQTT QoS1", message.toString()); assertEquals(1, message.getQos()); }
/** * Constructs a new MqttPublish object. * @param info the message info byte * @param data the variable header and payload bytes * @throws MqttException if an exception occurs creating the publish * @throws IOException if an exception occurs creating the publish */ public MqttPublish(byte info, byte[] data) throws MqttException, IOException { super(MqttWireMessage.MESSAGE_TYPE_PUBLISH); message = new MqttReceivedMessage(); message.setQos((info >> 1) & 0x03); if ((info & 0x01) == 0x01) { message.setRetained(true); } if ((info & 0x08) == 0x08) { ((MqttReceivedMessage) message).setDuplicate(true); } ByteArrayInputStream bais = new ByteArrayInputStream(data); CountingInputStream counter = new CountingInputStream(bais); DataInputStream dis = new DataInputStream(counter); topicName = decodeUTF8(dis); if (message.getQos() > 0) { msgId = dis.readUnsignedShort(); } byte[] payload = new byte[data.length-counter.getCounter()]; dis.readFully(payload); dis.close(); message.setPayload(payload); }
public void publishMessage(@NonNull MqttAndroidClient client, @NonNull String msg, int qos, @NonNull String topic) throws MqttException, UnsupportedEncodingException { byte[] encodedPayload = new byte[0]; encodedPayload = msg.getBytes("UTF-8"); MqttMessage message = new MqttMessage(encodedPayload); message.setId(320); message.setRetained(true); message.setQos(qos); client.publish(topic, message); }
assertEquals("Hello world MQTT!!", new String(messageOnA.getPayload(), UTF_8)); assertEquals(1, messageOnA.getQos()); subscriberA.disconnect(); assertEquals("Hello world MQTT!!", new String(messageOnB.getPayload(), UTF_8)); assertEquals(2, messageOnB.getQos()); subscriberB.disconnect();
String msg = "messages"; MqttMessage message = new MqttMessage(); message.setRetained(true); message.setPayload(msg.getBytes()); try { client_Panic.publish(topic, message); } catch (MqttException e) { e.printStackTrace(); }
client.setCallback(this); client.subscribe("foo"); MqttMessage message = new MqttMessage(); message.setPayload("A single message from my computer fff" .getBytes());
/** * Sets up the will information, based on the supplied parameters. * * @param topic the topic to send the LWT message to * @param msg the {@link MqttMessage} to send * @param qos the QoS Level to send the message at * @param retained whether the message should be retained or not */ protected void setWill(String topic, MqttMessage msg, int qos, boolean retained) { willDestination = topic; willMessage = msg; willMessage.setQos(qos); willMessage.setRetained(retained); // Prevent any more changes to the will message willMessage.setMutable(false); }
protected byte getMessageInfo() { byte info = (byte) (message.getQos() << 1); if (message.isRetained()) { info |= 0x01; } if (message.isDuplicate() || duplicate ) { info |= 0x08; } return info; }
connectFlags |= (willMessage.getQos()<<3); if (willMessage.isRetained()) { connectFlags |= 0x20;
@Override public void messageArrived(String topic, MqttMessage message) throws Exception { //check if Qos is not 0 if ((message.isRetained()==false)&&(message.getQos() ==1)){ handleMqttMessage(topic, message.toString()); } if (message.getQos() !=1){ sendResponse(topic, "QoS must be 1"); } if (message.isRetained()==true){ sendResponse(topic, "Message retained should be false"); } }
@Override public void messageArrived(String topic, MqttMessage message) throws Exception { this.emitter.data(message.toString()); }
/** * Constructs a message with the specified byte array as a payload, * and all other values set to defaults. * @param payload The Bytearray of the payload */ public MqttMessage(byte[] payload) { setPayload(payload); }