use of org.fusesource.mqtt.client.Topic in project activemq-artemis by apache.
the class MQTTTest method testLinkRouteAmqpReceiveMQTT.
@Test(timeout = 60 * 1000)
public void testLinkRouteAmqpReceiveMQTT() throws Exception {
MQTT mqtt = createMQTTConnection();
mqtt.setClientId("TestClient");
BlockingConnection blockingConnection = mqtt.blockingConnection();
blockingConnection.connect();
Topic t = new Topic("test", QoS.AT_LEAST_ONCE);
blockingConnection.subscribe(new Topic[] { t });
AmqpClient client = new AmqpClient(new URI(AMQP_URI), null, null);
AmqpConnection connection = client.connect();
try {
AmqpSession session = connection.createSession();
AmqpSender sender = session.createSender("test", true);
AmqpMessage message = new AmqpMessage();
message.setText("Test-Message");
sender.send(message);
sender.close();
} finally {
connection.close();
}
try {
blockingConnection.subscribe(new Topic[] { t });
assertNotNull(blockingConnection.receive(5, TimeUnit.SECONDS));
} finally {
blockingConnection.kill();
}
}
use of org.fusesource.mqtt.client.Topic 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.Topic in project activemq-artemis by apache.
the class MQTTTest method testResendMessageId.
@Test(timeout = 60 * 1000)
public void testResendMessageId() throws Exception {
final MQTT mqtt = createMQTTConnection("resend", false);
mqtt.setKeepAlive((short) 5);
final List<PUBLISH> publishList = new ArrayList<>();
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);
} catch (ProtocolException e) {
fail("Error decoding publish " + e.getMessage());
}
publishList.add(publish);
}
}
@Override
public void onSend(MQTTFrame frame) {
LOG.info("Client sent:\n" + frame);
}
});
BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
final String TOPIC = "TopicA/";
final String[] topics = new String[] { TOPIC, "TopicA/+" };
connection.subscribe(new Topic[] { new Topic(topics[0], QoS.AT_LEAST_ONCE), new Topic(topics[1], QoS.EXACTLY_ONCE) });
// publish non-retained message
connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false);
assertTrue(Wait.waitFor(() -> publishList.size() == 2, 5000));
connection.disconnect();
connection = mqtt.blockingConnection();
connection.connect();
assertTrue(Wait.waitFor(() -> publishList.size() == 4, 5000));
// TODO Investigate if receiving the same ID for overlapping subscriptions is actually spec compliant.
// In Artemis we send a new ID for every copy of the message.
// make sure we received duplicate message ids
// assertTrue(publishList.get(0).messageId() == publishList.get(2).messageId() || publishList.get(0).messageId() == publishList.get(3).messageId());
// assertTrue(publishList.get(1).messageId() == publishList.get(3).messageId() || publishList.get(1).messageId() == publishList.get(2).messageId());
// assertTrue(publishList.get(2).dup() && publishList.get(3).dup());
connection.unsubscribe(topics);
connection.disconnect();
}
use of org.fusesource.mqtt.client.Topic in project activemq-artemis by apache.
the class MQTTTest method testPublishDollarTopics.
@Ignore
@Test(timeout = 60 * 1000)
public // TODO Make dollar topics configurable in code base.
void testPublishDollarTopics() throws Exception {
MQTT mqtt = createMQTTConnection();
final String clientId = "publishDollar";
mqtt.setClientId(clientId);
mqtt.setKeepAlive((short) 2);
BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
final String DOLLAR_TOPIC = "$TopicA";
connection.subscribe(new Topic[] { new Topic(DOLLAR_TOPIC, QoS.EXACTLY_ONCE) });
connection.publish(DOLLAR_TOPIC, DOLLAR_TOPIC.getBytes(), QoS.EXACTLY_ONCE, true);
Message message = connection.receive(10, TimeUnit.SECONDS);
assertNull("Publish enabled for $ Topics by default", message);
connection.disconnect();
stopBroker();
protocolConfig = "transport.publishDollarTopics=true";
startBroker();
mqtt = createMQTTConnection();
mqtt.setClientId(clientId);
mqtt.setKeepAlive((short) 2);
connection = mqtt.blockingConnection();
connection.connect();
connection.subscribe(new Topic[] { new Topic(DOLLAR_TOPIC, QoS.EXACTLY_ONCE) });
connection.publish(DOLLAR_TOPIC, DOLLAR_TOPIC.getBytes(), QoS.EXACTLY_ONCE, true);
message = connection.receive(10, TimeUnit.SECONDS);
assertNotNull(message);
message.ack();
assertEquals("Message body", DOLLAR_TOPIC, new String(message.getPayload()));
connection.disconnect();
}
use of org.fusesource.mqtt.client.Topic in project activemq-artemis by apache.
the class MQTTTest method testClientDisconnectedOnMaxConsumerLimitReached.
@Test(timeout = 60 * 1000)
public void testClientDisconnectedOnMaxConsumerLimitReached() throws Exception {
Exception peerDisconnectedException = null;
try {
String clientId = "test.client";
SimpleString coreAddress = new SimpleString("foo.bar");
Topic[] mqttSubscription = new Topic[] { new Topic("foo/bar", QoS.AT_LEAST_ONCE) };
getServer().createQueue(coreAddress, RoutingType.MULTICAST, new SimpleString(clientId + "." + coreAddress), null, false, true, 0, false, true);
MQTT mqtt = createMQTTConnection();
mqtt.setClientId(clientId);
mqtt.setKeepAlive((short) 2);
final BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
connection.subscribe(mqttSubscription);
} catch (EOFException e) {
peerDisconnectedException = e;
}
assertNotNull(peerDisconnectedException);
assertTrue(peerDisconnectedException.getMessage().contains("Peer disconnected"));
}
Aggregations