use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.
the class MqttClusterRemoteSubscribeTest method unsubscribeRemoteQueue.
@Test
public void unsubscribeRemoteQueue() throws Exception {
final String TOPIC = "test/1/some/la";
setupServers(TOPIC);
startServers(0, 1);
BlockingConnection connection1 = null;
BlockingConnection connection2 = null;
try {
connection1 = retrieveMQTTConnection("tcp://localhost:61616");
connection2 = retrieveMQTTConnection("tcp://localhost:61617");
// Subscribe to topics
Topic[] topics = { new Topic(TOPIC, QoS.AT_MOST_ONCE) };
connection1.subscribe(topics);
connection2.subscribe(topics);
waitForBindings(0, TOPIC, 1, 1, true);
waitForBindings(1, TOPIC, 1, 1, true);
waitForBindings(0, TOPIC, 1, 1, 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";
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 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();
assertEquals(payload1, new String(message1.getPayload()));
assertEquals(payload2, new String(message2.getPayload()));
assertEquals(payload3, new String(message3.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 = connection1.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();
}
}
}
use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.
the class MqttClusterRemoteSubscribeTest method unsubscribeRemoteQueueWildCard.
@Test
public void unsubscribeRemoteQueueWildCard() throws Exception {
final String TOPIC = "test/+/some/#";
setupServers(TOPIC);
startServers(0, 1);
BlockingConnection connection1 = null;
BlockingConnection connection2 = null;
try {
connection1 = retrieveMQTTConnection("tcp://localhost:61616");
connection2 = retrieveMQTTConnection("tcp://localhost:61617");
// Subscribe to topics
Topic[] topics = { new Topic(TOPIC, QoS.AT_MOST_ONCE) };
connection1.subscribe(topics);
connection2.subscribe(topics);
waitForBindings(0, TOPIC, 1, 1, true);
waitForBindings(1, TOPIC, 1, 1, true);
waitForBindings(0, TOPIC, 1, 1, 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";
connection1.publish("test/1/some/la", payload1.getBytes(), QoS.AT_LEAST_ONCE, false);
connection1.publish("test/1/some/la", payload2.getBytes(), QoS.AT_MOST_ONCE, false);
connection1.publish("test/1/some/la", payload3.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();
assertEquals(payload1, new String(message1.getPayload()));
assertEquals(payload2, new String(message2.getPayload()));
assertEquals(payload3, new String(message3.getPayload()));
connection2.unsubscribe(new String[] { TOPIC });
connection1.publish("test/1/some/la", payload1.getBytes(), QoS.AT_LEAST_ONCE, false);
connection1.publish("test/1/some/la", payload2.getBytes(), QoS.AT_MOST_ONCE, false);
connection1.publish("test/1/some/la", payload3.getBytes(), QoS.AT_MOST_ONCE, false);
Message message11 = connection1.receive(5, TimeUnit.SECONDS);
message11.ack();
Message message21 = connection1.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();
}
}
}
use of org.fusesource.mqtt.client.BlockingConnection 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.BlockingConnection 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.BlockingConnection 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();
}
}
Aggregations