Search in sources :

Example 1 with MQTTInterceptor

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();
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ArrayMap(org.apache.felix.resolver.util.ArrayMap) CountDownLatch(java.util.concurrent.CountDownLatch) MQTTInterceptor(org.apache.activemq.artemis.core.protocol.mqtt.MQTTInterceptor) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) Test(org.junit.Test)

Aggregations

MqttMessage (io.netty.handler.codec.mqtt.MqttMessage)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)1 MQTTInterceptor (org.apache.activemq.artemis.core.protocol.mqtt.MQTTInterceptor)1 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)1 ArrayMap (org.apache.felix.resolver.util.ArrayMap)1 Test (org.junit.Test)1