use of org.fusesource.mqtt.client.Topic in project activemq-artemis by apache.
the class MqttClusterRemoteSubscribeTest method unsubscribeExistingQueue.
@Test
public void unsubscribeExistingQueue() throws Exception {
final String TOPIC = "test/1/some/la";
setupServers(TOPIC);
startServers(0, 1);
BlockingConnection connection1 = null;
BlockingConnection connection2 = null;
BlockingConnection connection3 = null;
try {
connection1 = retrieveMQTTConnection("tcp://localhost:61616");
connection2 = retrieveMQTTConnection("tcp://localhost:61617");
connection3 = retrieveMQTTConnection("tcp://localhost:61617");
// Subscribe to topics
Topic[] topics = { new Topic(TOPIC, QoS.AT_MOST_ONCE) };
connection1.subscribe(topics);
connection2.subscribe(topics);
connection3.subscribe(topics);
waitForBindings(0, TOPIC, 1, 1, true);
waitForBindings(1, TOPIC, 1, 2, true);
waitForBindings(0, TOPIC, 1, 2, false);
waitForBindings(1, TOPIC, 1, 1, false);
// Publish Messages
String payload1 = "This is message 1";
String payload2 = "This is message 2";
String payload3 = "This is message 3";
String payload4 = "This is message 4";
connection1.publish(TOPIC, payload1.getBytes(), QoS.AT_LEAST_ONCE, false);
connection1.publish(TOPIC, payload2.getBytes(), QoS.AT_MOST_ONCE, false);
connection1.publish(TOPIC, payload3.getBytes(), QoS.AT_MOST_ONCE, false);
connection1.publish(TOPIC, payload4.getBytes(), QoS.AT_MOST_ONCE, false);
Message message1 = connection1.receive(5, TimeUnit.SECONDS);
message1.ack();
Message message2 = connection2.receive(5, TimeUnit.SECONDS);
message2.ack();
Message message3 = connection1.receive(5, TimeUnit.SECONDS);
message3.ack();
Message message4 = connection3.receive(5, TimeUnit.SECONDS);
message4.ack();
assertEquals(payload1, new String(message1.getPayload()));
assertEquals(payload2, new String(message2.getPayload()));
assertEquals(payload3, new String(message3.getPayload()));
assertEquals(payload4, new String(message4.getPayload()));
connection2.unsubscribe(new String[] { TOPIC });
connection1.publish(TOPIC, payload1.getBytes(), QoS.AT_LEAST_ONCE, false);
connection1.publish(TOPIC, payload2.getBytes(), QoS.AT_MOST_ONCE, false);
connection1.publish(TOPIC, payload3.getBytes(), QoS.AT_MOST_ONCE, false);
Message message11 = connection1.receive(5, TimeUnit.SECONDS);
message11.ack();
Message message21 = connection3.receive(5, TimeUnit.SECONDS);
message21.ack();
Message message31 = connection1.receive(5, TimeUnit.SECONDS);
message31.ack();
String message11String = new String(message11.getPayload());
String message21String = new String(message21.getPayload());
String message31String = new String(message31.getPayload());
assertTrue(payload1.equals(message11String) || payload1.equals(message21String) || payload1.equals(message31String));
assertTrue(payload2.equals(message11String) || payload2.equals(message21String) || payload2.equals(message31String));
assertTrue(payload3.equals(message11String) || payload3.equals(message21String) || payload3.equals(message31String));
} finally {
String[] topics = new String[] { TOPIC };
if (connection1 != null) {
connection1.unsubscribe(topics);
connection1.disconnect();
}
if (connection2 != null) {
connection2.unsubscribe(topics);
connection2.disconnect();
}
if (connection3 != null) {
connection3.unsubscribe(topics);
connection3.disconnect();
}
}
}
use of org.fusesource.mqtt.client.Topic in project activemq-artemis by apache.
the class InterceptorExample method main.
public static void main(final String[] args) throws Exception {
System.out.println("Connecting to Artemis using MQTT");
MQTT mqtt = new MQTT();
mqtt.setHost("tcp://localhost:1883");
BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
System.out.println("Connected to Artemis");
// Subscribe to a topic
Topic[] topics = { new Topic("mqtt/example/interceptor", QoS.EXACTLY_ONCE) };
connection.subscribe(topics);
System.out.println("Subscribed to topics.");
// Publish message
String payload1 = "This is message 1";
connection.publish("mqtt/example/interceptor", payload1.getBytes(), QoS.EXACTLY_ONCE, false);
System.out.println("Sent message");
// Receive the sent message
Message message1 = connection.receive(5, TimeUnit.SECONDS);
String messagePayload = new String(message1.getPayload(), StandardCharsets.UTF_8);
System.out.println("Received message: " + messagePayload);
}
use of org.fusesource.mqtt.client.Topic in project wildfly-camel by wildfly-extras.
the class MQTTIntegrationTest method testMQTTProducer.
@Test
public void testMQTTProducer() throws Exception {
String conUrl = TestUtils.getResourceValue(getClass(), "/tcp-connection");
CamelContext camelctx = new DefaultCamelContext();
camelctx.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").transform(body().prepend("Hello ")).to("mqtt:foo?publishTopicName=" + BrokerSetup.TEST_TOPIC + "&host=" + conUrl);
}
});
camelctx.start();
try {
MQTT mqtt = new MQTT();
mqtt.setHost(conUrl);
BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
try {
Topic topic = new Topic(BrokerSetup.TEST_TOPIC, QoS.AT_MOST_ONCE);
connection.subscribe(new Topic[] { topic });
ProducerTemplate producer = camelctx.createProducerTemplate();
producer.asyncSendBody("direct:start", "Kermit");
Message message = connection.receive(10, TimeUnit.SECONDS);
message.ack();
String result = new String(message.getPayload());
Assert.assertEquals("Hello Kermit", result);
} finally {
connection.disconnect();
}
} finally {
camelctx.stop();
}
}
use of org.fusesource.mqtt.client.Topic in project beam by apache.
the class MqttIOTest method testWrite.
@Test
public void testWrite() throws Exception {
final int numberOfTestMessages = 200;
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.EXACTLY_ONCE) });
final Set<String> messages = new ConcurrentSkipListSet<>();
Thread subscriber = new Thread(() -> {
try {
for (int i = 0; i < numberOfTestMessages; i++) {
Message message = connection.receive();
messages.add(new String(message.getPayload(), StandardCharsets.UTF_8));
message.ack();
}
} catch (Exception e) {
LOG.error("Can't receive message", e);
}
});
subscriber.start();
ArrayList<byte[]> data = new ArrayList<>();
for (int i = 0; i < numberOfTestMessages; i++) {
data.add(("Test " + i).getBytes(StandardCharsets.UTF_8));
}
pipeline.apply(Create.of(data)).apply(MqttIO.write().withConnectionConfiguration(MqttIO.ConnectionConfiguration.create("tcp://localhost:" + port, "WRITE_TOPIC")));
pipeline.run();
subscriber.join();
connection.disconnect();
assertEquals(numberOfTestMessages, messages.size());
for (int i = 0; i < numberOfTestMessages; i++) {
assertTrue(messages.contains("Test " + i));
}
}
use of org.fusesource.mqtt.client.Topic 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);
}
}
Aggregations