Search in sources :

Example 1 with InMemorySubscriptionStore

use of org.apache.rocketmq.iot.storage.subscription.impl.InMemorySubscriptionStore in project rocketmq-externals by apache.

the class ProduceMessageIntegrationTest method setup.

@Before
public void setup() {
    /* start the mocked MQTTBridge */
    clientManager = new ClientManagerImpl();
    subscriptionStore = Mockito.spy(new InMemorySubscriptionStore());
    mqttConnectMessageHandler = new MqttConnectMessageHandler(clientManager);
    mqttMessageForwarder = new MqttMessageForwarder(subscriptionStore);
    dispatcher = new MessageDispatcher(clientManager);
    dispatcher.registerHandler(Message.Type.MQTT_CONNECT, mqttConnectMessageHandler);
    dispatcher.registerHandler(Message.Type.MQTT_PUBLISH, mqttMessageForwarder);
    producerChannel = new EmbeddedChannel(dispatcher);
    mockedConsuemr = Mockito.spy(new MqttClient());
    mockedConsumerCtx = Mockito.mock(ChannelHandlerContext.class);
    mockedSubscriptions = new ArrayList<>();
    mockedSubscriptions.add(Subscription.Builder.newBuilder().client(mockedConsuemr).qos(0).build());
    Mockito.when(mockedConsuemr.getCtx()).thenReturn(mockedConsumerCtx);
    Mockito.when(mockedConsumerCtx.writeAndFlush(Mockito.any(MqttPublishMessage.class))).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            MqttPublishMessage publishMessage = (MqttPublishMessage) mock.getArguments()[0];
            consumerChannel.writeOutbound(publishMessage);
            return consumerChannel.newSucceededFuture();
        }
    });
}
Also used : MqttMessageForwarder(org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttMessageForwarder) ClientManagerImpl(org.apache.rocketmq.iot.connection.client.impl.ClientManagerImpl) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) MqttConnectMessageHandler(org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttConnectMessageHandler) MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) MessageDispatcher(org.apache.rocketmq.iot.protocol.mqtt.handler.MessageDispatcher) MqttPublishMessage(io.netty.handler.codec.mqtt.MqttPublishMessage) InvocationOnMock(org.mockito.invocation.InvocationOnMock) InMemorySubscriptionStore(org.apache.rocketmq.iot.storage.subscription.impl.InMemorySubscriptionStore) Before(org.junit.Before)

Example 2 with InMemorySubscriptionStore

use of org.apache.rocketmq.iot.storage.subscription.impl.InMemorySubscriptionStore in project rocketmq-externals by apache.

the class InMemorySubscriptionTest method setup.

@Before
public void setup() throws IllegalAccessException {
    subscriptionStore = new InMemorySubscriptionStore();
    topic1 = "test/in/memory/topic-a";
    topic2 = "test/in/memory/topic-b";
    topic3 = "test/in/disk/topic-c";
    client1 = Mockito.spy(new MqttClient());
    client2 = Mockito.spy(new MqttClient());
    client1.setId("test-client-1");
    client2.setId("test-client-2");
    subscription1 = Subscription.Builder.newBuilder().client(client1).build();
    subscription2 = Subscription.Builder.newBuilder().client(client2).build();
    subscriptionStore.addTopic(topic1);
    subscriptionStore.addTopic(topic2);
    subscriptionStore.addTopic(topic3);
}
Also used : MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) InMemorySubscriptionStore(org.apache.rocketmq.iot.storage.subscription.impl.InMemorySubscriptionStore) Before(org.junit.Before)

Example 3 with InMemorySubscriptionStore

use of org.apache.rocketmq.iot.storage.subscription.impl.InMemorySubscriptionStore in project rocketmq-externals by apache.

the class MQTTBridge method init.

private void init() {
    bossGroup = new NioEventLoopGroup(MQTTBridgeConfiguration.threadNumOfBossGroup());
    workerGroup = new NioEventLoopGroup(MQTTBridgeConfiguration.threadNumOfWorkerGroup());
    serverBootstrap = new ServerBootstrap();
    serverBootstrap.group(bossGroup, workerGroup).localAddress(MQTTBridgeConfiguration.port()).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, MQTTBridgeConfiguration.socketBacklog()).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast("mqtt-decoder", new MqttDecoder());
            pipeline.addLast("mqtt-encoder", MqttEncoder.INSTANCE);
            pipeline.addLast("channel-idle-handler", new MqttIdleHandler());
            pipeline.addLast("message-dispatcher", messageDispatcher);
            pipeline.addLast("connection-manager", connectionHandler);
        }
    });
    subscriptionStore = new InMemorySubscriptionStore();
    clientManager = new ClientManagerImpl();
    messageDispatcher = new MessageDispatcher(clientManager);
    connectionHandler = new MqttConnectionHandler(clientManager, subscriptionStore);
    registerMessageHandlers();
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) MqttDecoder(io.netty.handler.codec.mqtt.MqttDecoder) ClientManagerImpl(org.apache.rocketmq.iot.connection.client.impl.ClientManagerImpl) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) MqttIdleHandler(org.apache.rocketmq.iot.protocol.mqtt.handler.MqttIdleHandler) MessageDispatcher(org.apache.rocketmq.iot.protocol.mqtt.handler.MessageDispatcher) MqttConnectionHandler(org.apache.rocketmq.iot.protocol.mqtt.handler.MqttConnectionHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) InMemorySubscriptionStore(org.apache.rocketmq.iot.storage.subscription.impl.InMemorySubscriptionStore)

Example 4 with InMemorySubscriptionStore

use of org.apache.rocketmq.iot.storage.subscription.impl.InMemorySubscriptionStore in project rocketmq-externals by apache.

the class PubSubIntegrationTest method setup.

@Before
public void setup() {
    clientManager = new ClientManagerImpl();
    subscriptionStore = new InMemorySubscriptionStore();
    messageDispatcher = new MessageDispatcher(clientManager);
    mqttConnectMessageHandler = new MqttConnectMessageHandler(clientManager);
    mqttSubscribeMessageHandler = new MqttSubscribeMessageHandler(subscriptionStore);
    mqttMessageForwarder = new MqttMessageForwarder(subscriptionStore);
    messageDispatcher.registerHandler(Message.Type.MQTT_CONNECT, mqttConnectMessageHandler);
    messageDispatcher.registerHandler(Message.Type.MQTT_PUBLISH, mqttMessageForwarder);
    messageDispatcher.registerHandler(Message.Type.MQTT_SUBSCRIBE, mqttSubscribeMessageHandler);
    producer = Mockito.spy(new MqttClient());
    producer.setId(producerId);
    consumer = Mockito.spy(new MqttClient());
    consumer.setId(consumerId);
    producerChannel = new EmbeddedChannel(messageDispatcher);
    producer.setCtx(producerChannel.pipeline().context("producer-ctx"));
    consumerChannel = new EmbeddedChannel(messageDispatcher);
    consumer.setCtx(consumerChannel.pipeline().context("consumer-ctx"));
    topicSubscriptions.add(new MqttTopicSubscription(topicName, MqttQoS.AT_MOST_ONCE));
// Mockito.when(consumerCtx.writeAndFlush(Mockito.any(MqttMessage.class))).then(
// new Answer<Object>() {
// @Override public Object answer(InvocationOnMock mock) throws Throwable {
// MqttMessage message = (MqttMessage) mock.getArguments()[0];
// consumerChannel.writeOutbound(message);
// return consumerChannel.newSucceededFuture();
// }
// }
// );
}
Also used : MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) MessageDispatcher(org.apache.rocketmq.iot.protocol.mqtt.handler.MessageDispatcher) MqttMessageForwarder(org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttMessageForwarder) ClientManagerImpl(org.apache.rocketmq.iot.connection.client.impl.ClientManagerImpl) MqttTopicSubscription(io.netty.handler.codec.mqtt.MqttTopicSubscription) MqttSubscribeMessageHandler(org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttSubscribeMessageHandler) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) MqttConnectMessageHandler(org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttConnectMessageHandler) InMemorySubscriptionStore(org.apache.rocketmq.iot.storage.subscription.impl.InMemorySubscriptionStore) Before(org.junit.Before)

Example 5 with InMemorySubscriptionStore

use of org.apache.rocketmq.iot.storage.subscription.impl.InMemorySubscriptionStore in project rocketmq-externals by apache.

the class ConsumeMessageIntegrationTest method setup.

@Before
public void setup() {
    clientManager = new ClientManagerImpl();
    subscriptionStore = new InMemorySubscriptionStore();
    messageDispatcher = new MessageDispatcher(clientManager);
    mqttConnectMessageHandler = new MqttConnectMessageHandler(clientManager);
    mqttSubscribeMessageHandler = new MqttSubscribeMessageHandler(subscriptionStore);
    mqttMessageForwarder = new MqttMessageForwarder(subscriptionStore);
    messageDispatcher.registerHandler(Message.Type.MQTT_CONNECT, mqttConnectMessageHandler);
    messageDispatcher.registerHandler(Message.Type.MQTT_SUBSCRIBE, mqttSubscribeMessageHandler);
    messageDispatcher.registerHandler(Message.Type.MQTT_PUBLISH, mqttMessageForwarder);
    embeddedChannel = new EmbeddedChannel(messageDispatcher);
    subscriptions = new ArrayList<>();
    subscriptions.add(new MqttTopicSubscription(topicName, MqttQoS.AT_MOST_ONCE));
    consuemr = Mockito.spy(new MqttClient());
    consuermCtx = Mockito.mock(ChannelHandlerContext.class);
    Mockito.when(consuemr.getCtx()).thenReturn(consuermCtx);
}
Also used : MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) MessageDispatcher(org.apache.rocketmq.iot.protocol.mqtt.handler.MessageDispatcher) MqttMessageForwarder(org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttMessageForwarder) ClientManagerImpl(org.apache.rocketmq.iot.connection.client.impl.ClientManagerImpl) MqttTopicSubscription(io.netty.handler.codec.mqtt.MqttTopicSubscription) MqttSubscribeMessageHandler(org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttSubscribeMessageHandler) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) MqttConnectMessageHandler(org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttConnectMessageHandler) InMemorySubscriptionStore(org.apache.rocketmq.iot.storage.subscription.impl.InMemorySubscriptionStore) Before(org.junit.Before)

Aggregations

InMemorySubscriptionStore (org.apache.rocketmq.iot.storage.subscription.impl.InMemorySubscriptionStore)5 ClientManagerImpl (org.apache.rocketmq.iot.connection.client.impl.ClientManagerImpl)4 MqttClient (org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient)4 MessageDispatcher (org.apache.rocketmq.iot.protocol.mqtt.handler.MessageDispatcher)4 Before (org.junit.Before)4 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)3 MqttConnectMessageHandler (org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttConnectMessageHandler)3 MqttMessageForwarder (org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttMessageForwarder)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 MqttTopicSubscription (io.netty.handler.codec.mqtt.MqttTopicSubscription)2 MqttSubscribeMessageHandler (org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttSubscribeMessageHandler)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 MqttDecoder (io.netty.handler.codec.mqtt.MqttDecoder)1 MqttPublishMessage (io.netty.handler.codec.mqtt.MqttPublishMessage)1 MqttConnectionHandler (org.apache.rocketmq.iot.protocol.mqtt.handler.MqttConnectionHandler)1 MqttIdleHandler (org.apache.rocketmq.iot.protocol.mqtt.handler.MqttIdleHandler)1