use of org.fusesource.mqtt.client.Topic in project camel by apache.
the class MQTTConsumerMultipleTopicsTest method testConsumeMultipleTopics.
@Test
public void testConsumeMultipleTopics() throws Exception {
MQTT mqtt = new MQTT();
mqtt.setHost(MQTTTestSupport.getHostForMQTTEndpoint());
BlockingConnection publisherConnection = mqtt.blockingConnection();
Topic topic1 = new Topic(TEST_TOPIC, QoS.AT_MOST_ONCE);
Topic topic2 = new Topic(TEST_TOPIC_2, QoS.AT_MOST_ONCE);
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMinimumMessageCount(numberOfMessages * 2);
publisherConnection.connect();
String payload;
for (int i = 0; i < numberOfMessages; i++) {
payload = "Topic 1, Message " + i;
publisherConnection.publish(topic1.name().toString(), payload.getBytes(), QoS.AT_LEAST_ONCE, false);
payload = "Topic 2, Message " + i;
publisherConnection.publish(topic2.name().toString(), payload.getBytes(), QoS.AT_LEAST_ONCE, false);
}
mock.await(5, TimeUnit.SECONDS);
mock.assertIsSatisfied();
}
use of org.fusesource.mqtt.client.Topic in project camel by apache.
the class MQTTConsumerTest method testConsume.
@Test
public void testConsume() throws Exception {
MQTT mqtt = new MQTT();
mqtt.setHost(MQTTTestSupport.getHostForMQTTEndpoint());
BlockingConnection publisherConnection = mqtt.blockingConnection();
Topic topic = new Topic(TEST_TOPIC, QoS.AT_MOST_ONCE);
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMinimumMessageCount(numberOfMessages);
publisherConnection.connect();
for (int i = 0; i < numberOfMessages; i++) {
String payload = "Message " + i;
publisherConnection.publish(topic.name().toString(), payload.getBytes(), QoS.AT_LEAST_ONCE, false);
}
mock.await(5, TimeUnit.SECONDS);
mock.assertIsSatisfied();
}
use of org.fusesource.mqtt.client.Topic in project wildfly-camel by wildfly-extras.
the class MQTTIntegrationTest method testMQTTConsumer.
@Test
public void testMQTTConsumer() throws Exception {
String conUrl = TestUtils.getResourceValue(getClass(), "/tcp-connection");
CamelContext camelctx = new DefaultCamelContext();
camelctx.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("mqtt:bar?subscribeTopicName=" + BrokerSetup.TEST_TOPIC + "&host=" + conUrl).transform(body().prepend("Hello ")).to("seda:end");
}
});
camelctx.start();
PollingConsumer consumer = camelctx.getEndpoint("seda:end").createPollingConsumer();
consumer.start();
try {
MQTT mqtt = new MQTT();
mqtt.setHost(conUrl);
BlockingConnection connection = mqtt.blockingConnection();
Topic topic = new Topic(BrokerSetup.TEST_TOPIC, QoS.AT_MOST_ONCE);
connection.connect();
try {
connection.publish(topic.name().toString(), "Kermit".getBytes(), QoS.AT_LEAST_ONCE, false);
} finally {
connection.disconnect();
}
String result = consumer.receive(3000).getIn().getBody(String.class);
Assert.assertEquals("Hello Kermit", result);
} finally {
camelctx.stop();
}
}
use of org.fusesource.mqtt.client.Topic in project activemq-artemis by apache.
the class MqttCrlEnabledExample method callBroker.
private static void callBroker(String truststorePath, String truststorePass, String keystorePath, String keystorePass) throws Exception {
BlockingConnection connection = null;
try {
connection = retrieveMQTTConnection("ssl://localhost:1883", truststorePath, truststorePass, keystorePath, keystorePass);
// Subscribe to topics
Topic[] topics = { new Topic("test/+/some/#", QoS.AT_MOST_ONCE) };
connection.subscribe(topics);
// Publish Messages
String payload = "This is message 1";
connection.publish("test/1/some/la", payload.getBytes(), QoS.AT_LEAST_ONCE, false);
Message message = connection.receive(5, TimeUnit.SECONDS);
System.out.println("Message received: " + new String(message.getPayload()));
} catch (Exception e) {
throw e;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
use of org.fusesource.mqtt.client.Topic in project activemq-artemis by apache.
the class MQTTPublishSubscribeExample method main.
public static void main(final String[] args) throws Exception {
// Create a new MQTT connection to the broker. We are not setting the client ID. The broker will pick one for us.
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 topics
Topic[] topics = { new Topic("mqtt/example/publish", QoS.AT_LEAST_ONCE), new Topic("test/#", QoS.EXACTLY_ONCE), new Topic("foo/+/bar", QoS.AT_LEAST_ONCE) };
connection.subscribe(topics);
System.out.println("Subscribed to topics.");
// Publish Messages
String payload1 = "This is message 1";
String payload2 = "This is message 2";
String payload3 = "This is message 3";
connection.publish("mqtt/example/publish", payload1.getBytes(), QoS.AT_LEAST_ONCE, false);
connection.publish("test/test", payload2.getBytes(), QoS.AT_MOST_ONCE, false);
connection.publish("foo/1/bar", payload3.getBytes(), QoS.AT_MOST_ONCE, false);
System.out.println("Sent messages.");
Message message1 = connection.receive(5, TimeUnit.SECONDS);
Message message2 = connection.receive(5, TimeUnit.SECONDS);
Message message3 = connection.receive(5, TimeUnit.SECONDS);
System.out.println("Received messages.");
System.out.println(new String(message1.getPayload()));
System.out.println(new String(message2.getPayload()));
System.out.println(new String(message3.getPayload()));
}
Aggregations