@Override public void messageArrived( String topic, MqttMessage message ) throws Exception { acceptRows( singletonList( ImmutableList.of( new String( message.getPayload(), Charsets.UTF_8 ), topic ) ) ); }
@Override public void messageArrived(String topic, MqttMessage message) throws Exception { logger.error("Message arrived to a PublishMQTT processor { topic:'" + topic +"; payload:"+ Arrays.toString(message.getPayload())+"}"); }
@Override public void messageArrived(String topic, MqttMessage message) throws Exception { if (logger.isDebugEnabled()) { byte[] payload = message.getPayload(); String text = new String(payload, "UTF-8"); if (StringUtils.isAsciiPrintable(text)) { logger.debug("Message arrived from topic {}. Payload: {}", new Object[] {topic, text}); } else { logger.debug("Message arrived from topic {}. Binary value of size {}", new Object[] {topic, payload.length}); } } if (mqttQueue.size() >= maxQueueSize){ throw new IllegalStateException("The subscriber queue is full, cannot receive another message until the processor is scheduled to run."); } else { mqttQueue.add(new MQTTQueueMessage(topic, message)); } }
public MQTTQueueMessage(String topic, MqttMessage message) { this.topic = topic; payload = message.getPayload(); qos = message.getQos(); retained = message.isRetained(); duplicate = message.isDuplicate(); }
protected static byte[] encodePayload(MqttMessage message) { return message.getPayload(); }
/** * Connect subscribe to topic and publish on the same topic */ private IMqttClient subscribeAndPublish(String topic) throws Exception { IMqttClient client = new MqttClient("tcp://localhost:1883", "SubPub"); MessageCollector collector = new MessageCollector(); client.setCallback(collector); client.connect(); client.subscribe(topic, 1); client.publish(topic, "Hello world MQTT!!".getBytes(UTF_8), 0, false); MqttMessage msg = collector.waitMessage(1); assertEquals("Hello world MQTT!!", new String(msg.getPayload(), UTF_8)); return client; } }
@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(), UTF_8)); m_willSubscriber.disconnect(); }
/** * Subclasses can override this method to convert the byte[] to a payload. * The default implementation creates a String (default) or byte[]. * @param mqttMessage The inbound message. * @return The payload for the Spring integration message */ protected Object mqttBytesToPayload(MqttMessage mqttMessage) { if (this.payloadAsBytes) { return mqttMessage.getPayload(); } else { return new String(mqttMessage.getPayload(), this.charset); } }
@Override public void messageArrived(String topic, MqttMessage message) throws Exception { addToHistory("Incoming message: " + new String(message.getPayload())); }
@Override public void messageArrived(String topic, MqttMessage message) throws Exception { // message Arrived! System.out.println("Message: " + topic + " : " + new String(message.getPayload())); } });
@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(), UTF_8)); }); m_willSubscriber.disconnect(); }
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();
@Test public void checkRestartCleanSubscriptionTree() throws Exception { // subscribe to /topic m_subscriber.connect(CLEAN_SESSION_OPT); m_subscriber.subscribe("/topic", 1); m_subscriber.disconnect(); // shutdown the integration m_server.stopServer(); // restart the integration m_server.startServer(IntegrationUtils.prepareTestProperties()); // reconnect the Subscriber subscribing to the same /topic but different QoS m_subscriber.connect(CLEAN_SESSION_OPT); m_subscriber.subscribe("/topic", 2); // should be just one registration so a publisher receive one notification m_publisher.connect(CLEAN_SESSION_OPT); m_publisher.publish("/topic", "Hello world MQTT!!".getBytes(UTF_8), 1, false); // read the messages MqttMessage msg = m_messageCollector.waitMessage(1); assertEquals("Hello world MQTT!!", new String(msg.getPayload(), UTF_8)); // no more messages on the same topic will be received assertNull(m_messageCollector.waitMessage(1)); }
@Override public void onMessageReceived(ReceivedMessage message) { System.out.println("GOT A MESSAGE in history " + new String(message.getMessage().getPayload())); System.out.println("M: " + messages.size()); messageListAdapter.notifyDataSetChanged(); } });
LOG.info("Waiting to receive 1 sec from {}", clientForSubscribe2.getClientId()); MqttMessage messageOnB = cbSubscriber2.waitMessage(1); assertEquals("Hello", new String(messageOnB.getPayload(), UTF_8));
@NonNull @Override public View getView(final int position, View convertView, @NonNull ViewGroup parent){ LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.message_list_item, parent, false); TextView topicTextView = (TextView) rowView.findViewById(R.id.message_topic_text); TextView messageTextView = (TextView) rowView.findViewById(R.id.message_text); TextView dateTextView = (TextView) rowView.findViewById(R.id.message_date_text); messageTextView.setText(new String(messages.get(position).getMessage().getPayload())); topicTextView.setText(context.getString(R.string.topic_fmt, messages.get(position).getTopic())); DateFormat dateTimeFormatter = SimpleDateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); String shortDateStamp = dateTimeFormatter.format(messages.get(position).getTimestamp()); dateTextView.setText(context.getString(R.string.message_time_fmt, shortDateStamp)); return rowView; } }
public byte[] getPayload() throws MqttException { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); encodeUTF8(dos,clientId); if (willMessage != null) { encodeUTF8(dos,willDestination); dos.writeShort(willMessage.getPayload().length); dos.write(willMessage.getPayload()); } if (userName != null) { encodeUTF8(dos,userName); if (password != null) { encodeUTF8(dos,new String(password)); } } dos.flush(); return baos.toByteArray(); } catch (IOException ex) { throw new MqttException(ex); } }
/** * @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 mqttMessage) throws Exception { LOGGER.info("message arrived on topic: " + topic); final byte[] data = mqttMessage.getPayload(); for (final String topicKey : topicListeners.keySet()) { if (isTopicMatch(topicKey, topic)) { final AWSIotMqttTopic topicModel = topicListeners.get(topicKey); if (topicModel != null) { if (topicModel.getCallback() != null) { topicModel.getCallback().onMessageArrived(topic, data); } } } } }
ParcelableMqttMessage(MqttMessage original) { super(original.getPayload()); setQos(original.getQos()); setRetained(original.isRetained()); setDuplicate(original.isDuplicate()); }