use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.
the class MQTTTest method doTestJmsMapping.
// TODO As with other tests, this should be enabled as part of the cross protocol support with MQTT.
public void doTestJmsMapping(String destinationName) throws Exception {
// start up jms consumer
Connection jmsConn = cf.createConnection();
Session session = jmsConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination dest = session.createQueue(destinationName);
MessageConsumer consumer = session.createConsumer(dest);
jmsConn.start();
// set up mqtt producer
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("foo3");
mqtt.setKeepAlive((short) 2);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
int messagesToSend = 5;
// publish
for (int i = 0; i < messagesToSend; ++i) {
connection.publish("test/foo", "hello world".getBytes(), QoS.AT_LEAST_ONCE, false);
}
connection.disconnect();
for (int i = 0; i < messagesToSend; i++) {
javax.jms.Message message = consumer.receive(2 * 1000);
assertNotNull(message);
assertTrue(message instanceof BytesMessage);
BytesMessage bytesMessage = (BytesMessage) message;
int length = (int) bytesMessage.getBodyLength();
byte[] buffer = new byte[length];
bytesMessage.readBytes(buffer);
assertEquals("hello world", new String(buffer));
}
jmsConn.close();
}
use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.
the class MQTTTest method testPingOnMQTT.
// TODO This should be reworked to align with Artemis recovery.
// @Test(timeout = 60 * 1000)
// public void testActiveMQRecoveryPolicy() throws Exception {
// // test with ActiveMQ LastImageSubscriptionRecoveryPolicy
// final PolicyMap policyMap = new PolicyMap();
// final PolicyEntry policyEntry = new PolicyEntry();
// policyEntry.setSubscriptionRecoveryPolicy(new LastImageSubscriptionRecoveryPolicy());
// policyMap.put(new ActiveMQTopic(">"), policyEntry);
// brokerService.setDestinationPolicy(policyMap);
//
// MQTT mqtt = createMQTTConnection("pub-sub", true);
// final int[] retain = new int[1];
// final int[] nonretain = new int[1];
// mqtt.setTracer(new Tracer() {
// @Override
// public void onReceive(MQTTFrame frame) {
// if (frame.messageType() == PUBLISH.TYPE) {
// LOG.info("Received message with retain=" + frame.retain());
// if (frame.retain()) {
// retain[0]++;
// } else {
// nonretain[0]++;
// }
// }
// }
// });
//
// BlockingConnection connection = mqtt.blockingConnection();
// connection.connect();
// final String RETAINED = "RETAINED";
// connection.publish("one", RETAINED.getBytes(), QoS.AT_LEAST_ONCE, true);
// connection.publish("two", RETAINED.getBytes(), QoS.AT_LEAST_ONCE, true);
//
// final String NONRETAINED = "NONRETAINED";
// connection.publish("one", NONRETAINED.getBytes(), QoS.AT_LEAST_ONCE, false);
// connection.publish("two", NONRETAINED.getBytes(), QoS.AT_LEAST_ONCE, false);
//
// connection.subscribe(new Topic[]{new Topic("#", QoS.AT_LEAST_ONCE)});
// for (int i = 0; i < 4; i++) {
// final Message message = connection.receive(30, TimeUnit.SECONDS);
// assertNotNull("Should receive 4 messages", message);
// message.ack();
// }
// assertEquals("Should receive 2 retained messages", 2, retain[0]);
// assertEquals("Should receive 2 non-retained messages", 2, nonretain[0]);
// }
// TODO As with other tests, this should be enabled as part of the cross protocol support with MQTT.
// @Test(timeout = 60 * 1000)
// public void testSendMQTTReceiveJMSVirtualTopic() throws Exception {
//
// final MQTTClientProvider provider = getMQTTClientProvider();
// initializeConnection(provider);
// final String DESTINATION_NAME = "Consumer.jms.VirtualTopic.TopicA";
//
// // send retained message
// final String RETAINED = "RETAINED";
// final String MQTT_DESTINATION_NAME = "VirtualTopic/TopicA";
// provider.publish(MQTT_DESTINATION_NAME, RETAINED.getBytes(), AT_LEAST_ONCE, true);
//
// ActiveMQConnection activeMQConnection = (ActiveMQConnection) new ActiveMQConnectionFactory(jmsUri).createConnection();
// // MUST set to true to receive retained messages
// activeMQConnection.setUseRetroactiveConsumer(true);
// activeMQConnection.start();
// Session s = activeMQConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Queue jmsQueue = s.createQueue(DESTINATION_NAME);
// MessageConsumer consumer = s.createConsumer(jmsQueue);
//
// // check whether we received retained message on JMS subscribe
// ActiveMQMessage message = (ActiveMQMessage) consumer.receive(5000);
// assertNotNull("Should get retained message", message);
// ByteSequence bs = message.getContent();
// assertEquals(RETAINED, new String(bs.data, bs.offset, bs.length));
// assertTrue(message.getBooleanProperty(RetainedMessageSubscriptionRecoveryPolicy.RETAINED_PROPERTY));
//
// for (int i = 0; i < NUM_MESSAGES; i++) {
// String payload = "Test Message: " + i;
// provider.publish(MQTT_DESTINATION_NAME, payload.getBytes(), AT_LEAST_ONCE);
// message = (ActiveMQMessage) consumer.receive(5000);
// assertNotNull("Should get a message", message);
// bs = message.getContent();
// assertEquals(payload, new String(bs.data, bs.offset, bs.length));
// }
//
// // re-create consumer and check we received retained message again
// consumer.close();
// consumer = s.createConsumer(jmsQueue);
// message = (ActiveMQMessage) consumer.receive(5000);
// assertNotNull("Should get retained message", message);
// bs = message.getContent();
// assertEquals(RETAINED, new String(bs.data, bs.offset, bs.length));
// assertTrue(message.getBooleanProperty(RetainedMessageSubscriptionRecoveryPolicy.RETAINED_PROPERTY));
//
// activeMQConnection.close();
// provider.disconnect();
// }
@Test(timeout = 60 * 1000)
public void testPingOnMQTT() throws Exception {
stopBroker();
protocolConfig = "maxInactivityDuration=-1";
startBroker();
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("test-mqtt");
mqtt.setKeepAlive((short) 2);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
assertTrue("KeepAlive didn't work properly", Wait.waitFor(() -> connection.isConnected()));
connection.disconnect();
}
use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.
the class MQTTTest method testDuplicateClientId.
@Test(timeout = 60 * 1000)
public void testDuplicateClientId() throws Exception {
final String clientId = "duplicateClient";
MQTT mqtt = createMQTTConnection(clientId, false);
mqtt.setKeepAlive((short) 2);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
final String TOPICA = "TopicA";
connection.publish(TOPICA, TOPICA.getBytes(), QoS.EXACTLY_ONCE, true);
MQTT mqtt1 = createMQTTConnection(clientId, false);
mqtt1.setKeepAlive((short) 2);
final BlockingConnection connection1 = mqtt1.blockingConnection();
connection1.connect();
assertTrue("Duplicate client disconnected", Wait.waitFor(() -> connection1.isConnected()));
assertTrue("Old client still connected", Wait.waitFor(() -> !connection.isConnected()));
connection1.publish(TOPICA, TOPICA.getBytes(), QoS.EXACTLY_ONCE, true);
connection1.disconnect();
}
use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.
the class MQTTTest method testPacketIdGeneratorNonCleanSession.
@Test(timeout = 90 * 1000)
public void testPacketIdGeneratorNonCleanSession() throws Exception {
final MQTT mqtt = createMQTTConnection("nonclean-packetid", false);
mqtt.setKeepAlive((short) 15);
final Map<Short, PUBLISH> publishMap = new ConcurrentHashMap<>();
mqtt.setTracer(new Tracer() {
@Override
public void onReceive(MQTTFrame frame) {
LOG.info("Client received:\n" + frame);
if (frame.messageType() == PUBLISH.TYPE) {
PUBLISH publish = new PUBLISH();
try {
publish.decode(frame);
LOG.info("PUBLISH " + publish);
} catch (ProtocolException e) {
fail("Error decoding publish " + e.getMessage());
}
if (publishMap.get(publish.messageId()) != null) {
assertTrue(publish.dup());
}
publishMap.put(publish.messageId(), publish);
}
}
@Override
public void onSend(MQTTFrame frame) {
LOG.info("Client sent:\n" + frame);
}
});
BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
final String TOPIC = "TopicA/";
connection.subscribe(new Topic[] { new Topic(TOPIC, QoS.EXACTLY_ONCE) });
// publish non-retained messages
final int TOTAL_MESSAGES = 10;
for (int i = 0; i < TOTAL_MESSAGES; i++) {
connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false);
}
// receive half the messages in this session
for (int i = 0; i < TOTAL_MESSAGES / 2; i++) {
final Message msg = connection.receive(1000, TimeUnit.MILLISECONDS);
assertNotNull(msg);
assertEquals(TOPIC, new String(msg.getPayload()));
msg.ack();
}
connection.disconnect();
// resume session
connection = mqtt.blockingConnection();
connection.connect();
// receive rest of the messages
Message msg = null;
do {
msg = connection.receive(1000, TimeUnit.MILLISECONDS);
if (msg != null) {
assertEquals(TOPIC, new String(msg.getPayload()));
msg.ack();
}
} while (msg != null);
// make sure we received all message ids
for (short id = 1; id <= TOTAL_MESSAGES; id++) {
assertNotNull("No message for id " + id, publishMap.get(id));
}
connection.unsubscribe(new String[] { TOPIC });
connection.disconnect();
}
use of org.fusesource.mqtt.client.BlockingConnection in project activemq-artemis by apache.
the class MQTTTest method testValidZeroLengthClientId.
@Test(timeout = 30 * 1000)
public void testValidZeroLengthClientId() throws Exception {
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("");
mqtt.setCleanSession(true);
BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
connection.disconnect();
}
Aggregations