use of org.fusesource.mqtt.client.BlockingConnection 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.BlockingConnection in project quickstarts by jboss-switchyard.
the class CamelMQTTBindingTest method testServiceBinding.
@Test
public void testServiceBinding() throws Exception {
MockHandler mock = _testKit.replaceService("StoreReference");
MQTT mqtt = new MQTT();
BlockingConnection connection = mqtt.blockingConnection();
try {
connection.connect();
connection.publish(TOPIC_INPUT, MESSAGE_INPUT.getBytes(), QoS.AT_LEAST_ONCE, false);
Thread.sleep(1000);
LinkedBlockingQueue<Exchange> received = mock.getMessages();
Assert.assertNotNull(received);
Exchange exchange = received.iterator().next();
Assert.assertEquals(MESSAGE_OUTPUT, exchange.getMessage().getContent(String.class));
} finally {
connection.disconnect();
}
}
use of org.fusesource.mqtt.client.BlockingConnection in project quickstarts by jboss-switchyard.
the class CamelMQTTBindingTest method before.
@Before
public void before() {
MQTT mqtt = new MQTT();
Topic outputTopic = new Topic(TOPIC_OUTPUT, QoS.AT_LEAST_ONCE);
BlockingConnection connection = mqtt.blockingConnection();
try {
connection.connect();
connection.subscribe(new Topic[] { outputTopic });
while (connection.receive(1000, TimeUnit.MILLISECONDS) != null) ;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
use of org.fusesource.mqtt.client.BlockingConnection in project camel by apache.
the class MQTTProducerReconnectTest method testProduce.
@Test
public void testProduce() throws Exception {
MQTT mqtt = new MQTT();
mqtt.setHost(MQTTTestSupport.getHostForMQTTEndpoint());
final BlockingConnection subscribeConnection = mqtt.blockingConnection();
subscribeConnection.connect();
Topic topic = new Topic(TEST_TOPIC, QoS.AT_MOST_ONCE);
Topic[] topics = { topic };
subscribeConnection.subscribe(topics);
final CountDownLatch latch = new CountDownLatch(numberOfMessages);
Thread thread = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < numberOfMessages; i++) {
try {
Message message = subscribeConnection.receive();
message.ack();
latch.countDown();
} catch (Exception e) {
e.printStackTrace();
break;
}
}
}
});
thread.start();
for (int i = 0; i < numberOfMessages; i++) {
if (i == 5) {
LOG.info("Stopping MQTT transport");
brokerService.getTransportConnectorByScheme("mqtt").stop();
Thread starter = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(3000);
LOG.info("Starting MQTT transport again");
brokerService.getTransportConnectorByScheme("mqtt").start();
} catch (Exception e) {
// ignore
}
}
});
starter.start();
}
try {
template.sendBody("direct:foo", "test message " + i);
} catch (Exception e) {
// ignore
}
}
latch.await(20, TimeUnit.SECONDS);
assertTrue("Messages not consumed = " + latch.getCount(), latch.getCount() == 0);
}
use of org.fusesource.mqtt.client.BlockingConnection in project camel by apache.
the class MQTTProducerTest method testProduce.
@Test
public void testProduce() throws Exception {
MQTT mqtt = new MQTT();
mqtt.setHost(MQTTTestSupport.getHostForMQTTEndpoint());
final BlockingConnection subscribeConnection = mqtt.blockingConnection();
subscribeConnection.connect();
Topic topic = new Topic(TEST_TOPIC, QoS.AT_MOST_ONCE);
Topic[] topics = { topic };
subscribeConnection.subscribe(topics);
final CountDownLatch latch = new CountDownLatch(numberOfMessages);
Thread thread = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < numberOfMessages; i++) {
try {
Message message = subscribeConnection.receive();
message.ack();
latch.countDown();
} catch (Exception e) {
e.printStackTrace();
break;
}
}
}
});
thread.start();
Producer producer = context.getEndpoint("direct:foo").createProducer();
for (int i = 0; i < numberOfMessages; i++) {
Exchange exchange = producer.createExchange();
exchange.getIn().setBody("test message " + i);
producer.process(exchange);
}
latch.await(20, TimeUnit.SECONDS);
assertTrue("Messages not consumed = " + latch.getCount(), latch.getCount() == 0);
}
Aggregations