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); } }
MqttMessage message = new MqttMessage(messageContent.getBytes()); message.setQos(qos); sampleClient.publish(topic, message); log.info("Message published"); sampleClient.disconnect();
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()); } }
message.setPayload("A single message from my computer fff" .getBytes()); client.publish("foo", message); } catch (MqttException e) { e.printStackTrace();
@Override public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws KettleException { Object[] row = getRow(); if ( null == row ) { setOutputDone(); return false; } try { client.get() // client is memoized, loaded on first use .publish( getTopic( row ), getMessage( row ) ); incrementLinesOutput(); putRow( getInputRowMeta(), row ); // copy row to possible alternate rowset(s). if ( checkFeedback( getLinesRead() ) && log.isBasic() ) { logBasic( getString( PKG, "MQTTProducer.Log.LineNumber" ) + getLinesRead() ); } } catch ( MqttException e ) { logError( getString( PKG, "MQTTProducer.Error.QOSNotSupported", meta.qos ) ); logError( e.getMessage(), e ); setErrors( 1 ); stopAll(); return false; } catch ( RuntimeException re ) { stopAll(); logError( re.getMessage(), re ); return false; } return true; }
@Test public void checkSupportSSLForMultipleClient() throws Exception { LOG.info("*** checkSupportSSLForMultipleClient ***"); SSLSocketFactory ssf = configureSSLSocketFactory(); MqttConnectOptions options = new MqttConnectOptions(); options.setSocketFactory(ssf); m_client.connect(options); m_client.subscribe("/topic", 0); MqttClient secondClient = new MqttClient("ssl://localhost:8883", "secondTestClient", new MemoryPersistence()); MqttConnectOptions secondClientOptions = new MqttConnectOptions(); secondClientOptions.setSocketFactory(ssf); secondClient.connect(secondClientOptions); secondClient.publish("/topic", new MqttMessage("message".getBytes(UTF_8))); secondClient.disconnect(); m_client.disconnect(); }
MqttClient clientForPublish = new MqttClient("tcp://localhost:1883", "clientForPublish", dsSubscriberPUB); clientForPublish.connect(); clientForPublish.publish("topic", "Hello".getBytes(UTF_8), 2, true);
public void publish(String topic, byte[] payload,int qos, boolean retained) throws MqttException, MqttPersistenceException { MqttMessage message = new MqttMessage(payload); message.setQos(qos); message.setRetained(retained); this.publish(topic, message); }
@Override public boolean writeStreamElement(StreamElement se) { try { client.publish(topic, se.toJSON(vsname).getBytes(), 0, false); } catch (MqttException e) { logger.error("Unable to publish stream element to topic " + topic + " on "+ serverURI); return false; } return true; }
@When("^topic \"(.*)\" content \"(.*)\" is published by client named \"(.*)\"$") public void publishMessageByClient(String topic, String content, String clientName) throws Exception { MqttClient mqttClient = (MqttClient) stepData.get(clientName); byte[] payload = Files.readAllBytes(Paths.get(content)); if (mqttClient == null) { throw new Exception("Mqtt test client not found"); } mqttClient.publish(topic, payload, 0, false); }
@Override public void dataAvailable(String inputStreamName, StreamElement streamElement) { try { //to adapt according to content to be sent... client.publish(topic, ((byte[])streamElement.getData("raw_packet")), 0, false); } catch (MqttException e) { logger.warn("Error while sending stream element to the MQTT server.", e); } dataProduced(streamElement); }
private void publishResponse(Response response) throws MqttException { DiozeroProtos.Response message = DiozeroProtosConverter.convert(response); mqttClient.publish(MqttProviderConstants.RESPONSE_TOPIC, message.toByteArray(), MqttProviderConstants.DEFAULT_QOS, MqttProviderConstants.DEFAULT_RETAINED); }
private void publishResponse(GpioPwmReadResponse response) throws MqttException { DiozeroProtos.Gpio.PwmReadResponse message = DiozeroProtosConverter.convert(response); mqttClient.publish(MqttProviderConstants.GPIO_PWM_READ_RESPONSE_TOPIC, message.toByteArray(), MqttProviderConstants.DEFAULT_QOS, MqttProviderConstants.DEFAULT_RETAINED); }
private void publishResponse(I2CReadResponse response) throws MqttException { DiozeroProtos.I2C.ReadResponse message = DiozeroProtosConverter.convert(response); mqttClient.publish(MqttProviderConstants.I2C_READ_RESPONSE_TOPIC, message.toByteArray(), MqttProviderConstants.DEFAULT_QOS, MqttProviderConstants.DEFAULT_RETAINED); }
private void publishResponse(GpioDigitalReadResponse response) throws MqttException { DiozeroProtos.Gpio.DigitalReadResponse message = DiozeroProtosConverter.convert(response); mqttClient.publish(MqttProviderConstants.GPIO_DIGITAL_READ_RESPONSE_TOPIC, message.toByteArray(), MqttProviderConstants.DEFAULT_QOS, MqttProviderConstants.DEFAULT_RETAINED); }
private void publishResponse(GpioAnalogReadResponse response) throws MqttException { DiozeroProtos.Gpio.AnalogReadResponse message = DiozeroProtosConverter.convert(response); mqttClient.publish(MqttProviderConstants.GPIO_ANALOG_READ_RESPONSE_TOPIC, message.toByteArray(), MqttProviderConstants.DEFAULT_QOS, MqttProviderConstants.DEFAULT_RETAINED); }
private void publishResponse(I2CReadByteResponse response) throws MqttException { DiozeroProtos.I2C.ReadByteResponse message = DiozeroProtosConverter.convert(response); mqttClient.publish(MqttProviderConstants.I2C_READ_BYTE_RESPONSE_TOPIC, message.toByteArray(), MqttProviderConstants.DEFAULT_QOS, MqttProviderConstants.DEFAULT_RETAINED); }
public void publish(String topic, byte[] payload,int qos, boolean retained) throws MqttException, MqttPersistenceException { MqttMessage message = new MqttMessage(payload); message.setQos(qos); message.setRetained(retained); this.publish(topic, message); }
public void publish(String topic, byte[] payload,int qos, boolean retained) throws MqttException, MqttPersistenceException { MqttMessage message = new MqttMessage(payload); message.setQos(qos); message.setRetained(retained); this.publish(topic, message); }
@Override public void valueChanged(DigitalInputEvent event) { DiozeroProtos.Gpio.Notification notification = DiozeroProtosConverter.convert(event); try { mqttClient.publish(MqttProviderConstants.GPIO_NOTIFICATION_TOPIC, notification.toByteArray(), MqttProviderConstants.DEFAULT_QOS, MqttProviderConstants.DEFAULT_RETAINED); } catch (MqttException e) { Logger.error(e, "MQTT Error: {}", e); } } }