use of org.apache.activemq.artemis.core.protocol.mqtt.MQTTInterceptor in project activemq-artemis by apache.
the class MQTTInterceptorPropertiesTest method testCheckInterceptedMQTTMessageProperties.
@Test(timeout = 60000)
public void testCheckInterceptedMQTTMessageProperties() throws Exception {
final String addressQueue = name.getMethodName();
final String msgText = "Test intercepted message";
final boolean retained = true;
Map<String, Object> expectedProperties = new ArrayMap<>();
expectedProperties.put(ADDRESS, addressQueue);
expectedProperties.put(MESSAGE_TEXT, msgText);
expectedProperties.put(RETAINED, retained);
final MQTTClientProvider subscribeProvider = getMQTTClientProvider();
initializeConnection(subscribeProvider);
subscribeProvider.subscribe(addressQueue, AT_MOST_ONCE);
final CountDownLatch latch = new CountDownLatch(1);
MQTTInterceptor incomingInterceptor = new MQTTInterceptor() {
@Override
public boolean intercept(MqttMessage packet, RemotingConnection connection) throws ActiveMQException {
System.out.println("incoming");
if (packet.getClass() == MqttPublishMessage.class) {
return checkMessageProperties(packet, expectedProperties);
} else {
return true;
}
}
};
MQTTInterceptor outgoingInterceptor = new MQTTInterceptor() {
@Override
public boolean intercept(MqttMessage packet, RemotingConnection connection) throws ActiveMQException {
System.out.println("outgoing");
if (packet.getClass() == MqttPublishMessage.class) {
return checkMessageProperties(packet, expectedProperties);
} else {
return true;
}
}
};
server.getRemotingService().addIncomingInterceptor(incomingInterceptor);
server.getRemotingService().addOutgoingInterceptor(outgoingInterceptor);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
byte[] payload = subscribeProvider.receive(10000);
assertNotNull("Should get a message", payload);
latch.countDown();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
final MQTTClientProvider publishProvider = getMQTTClientProvider();
initializeConnection(publishProvider);
publishProvider.publish(addressQueue, msgText.getBytes(), AT_MOST_ONCE, retained);
latch.await(10, TimeUnit.SECONDS);
subscribeProvider.disconnect();
publishProvider.disconnect();
}
Aggregations