use of org.fusesource.mqtt.client.Message in project quickstarts by jboss-switchyard.
the class MQTTClient method main.
/**
* Only execution point for this application.
* @param args command line args.
* @throws Exception if something goes wrong.
*/
public static void main(final String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(MESSAGE_PAYLOAD)));
String payload = reader.readLine();
reader.close();
BlockingConnection publishConnection = null;
BlockingConnection subscribeConnection = null;
try {
Topic outputTopic = new Topic(TOPIC_OUTPUT, QoS.AT_LEAST_ONCE);
MQTT mqtt = new MQTT();
mqtt.setUserName(USER_NAME);
mqtt.setPassword(PASSWORD);
subscribeConnection = mqtt.blockingConnection();
subscribeConnection.connect();
subscribeConnection.subscribe(new Topic[] { outputTopic });
publishConnection = mqtt.blockingConnection();
publishConnection.connect();
publishConnection.publish(TOPIC_INPUT, payload.getBytes(), QoS.AT_LEAST_ONCE, false);
System.out.println("Published a message to " + TOPIC_INPUT + ": " + payload);
Message msg = subscribeConnection.receive(60000, TimeUnit.MILLISECONDS);
if (msg != null) {
System.out.println("Received a message from " + TOPIC_OUTPUT + ": " + new String(msg.getPayload()));
} else {
System.out.println("No message was received from " + TOPIC_OUTPUT);
}
} finally {
if (publishConnection != null && publishConnection.isConnected()) {
publishConnection.disconnect();
}
if (subscribeConnection != null && subscribeConnection.isConnected()) {
subscribeConnection.disconnect();
}
}
}
use of org.fusesource.mqtt.client.Message in project storm by apache.
the class StormMqttIntegrationTest method testMqttTopology.
@Test
public void testMqttTopology() throws Exception {
MQTT client = new MQTT();
client.setTracer(new MqttLogger());
URI uri = URI.create("tcp://localhost:1883");
client.setHost(uri);
client.setClientId("MQTTSubscriber");
client.setCleanSession(false);
BlockingConnection connection = client.blockingConnection();
connection.connect();
Topic[] topics = { new Topic("/integration-result", QoS.AT_LEAST_ONCE) };
byte[] qoses = connection.subscribe(topics);
try (LocalCluster cluster = new LocalCluster();
LocalTopology topo = cluster.submitTopology("test", new Config(), buildMqttTopology())) {
LOG.info("topology started");
while (!spoutActivated) {
Thread.sleep(500);
}
// publish a retained message to the broker
MqttOptions options = new MqttOptions();
options.setCleanConnection(false);
MqttPublisher publisher = new MqttPublisher(options, true);
publisher.connectMqtt("MqttPublisher");
publisher.publish(new MqttMessage(TEST_TOPIC, "test".getBytes()));
LOG.info("published message");
Message message = connection.receive();
LOG.info("Message recieved on topic: {}", message.getTopic());
LOG.info("Payload: {}", new String(message.getPayload()));
message.ack();
Assert.assertArrayEquals(message.getPayload(), RESULT_PAYLOAD.getBytes());
Assert.assertEquals(message.getTopic(), RESULT_TOPIC);
}
}
use of org.fusesource.mqtt.client.Message in project beam by apache.
the class MqttIOTest method testWrite.
@Test
public void testWrite() throws Exception {
MQTT client = new MQTT();
client.setHost("tcp://localhost:" + port);
final BlockingConnection connection = client.blockingConnection();
connection.connect();
connection.subscribe(new Topic[] { new Topic(Buffer.utf8("WRITE_TOPIC"), QoS.AT_LEAST_ONCE) });
final Set<String> messages = new HashSet<>();
Thread subscriber = new Thread() {
public void run() {
try {
for (int i = 0; i < 200; i++) {
Message message = connection.receive();
messages.add(new String(message.getPayload()));
message.ack();
}
} catch (Exception e) {
LOG.error("Can't receive message", e);
}
}
};
subscriber.start();
ArrayList<byte[]> data = new ArrayList<>();
for (int i = 0; i < 200; i++) {
data.add(("Test " + i).getBytes());
}
pipeline.apply(Create.of(data)).apply(MqttIO.write().withConnectionConfiguration(MqttIO.ConnectionConfiguration.create("tcp://localhost:" + port, "WRITE_TOPIC")));
pipeline.run();
subscriber.join();
connection.disconnect();
assertEquals(200, messages.size());
for (int i = 0; i < 200; i++) {
assertTrue(messages.contains("Test " + i));
}
}
Aggregations