Search in sources :

Example 1 with RebalanceImpl

use of org.apache.rocketmq.client.impl.consumer.RebalanceImpl in project LinkAgent by shulieTech.

the class ConsumerRegistry method addListener.

private static void addListener(final DefaultMQPushConsumer businessConsumer) {
    final RebalanceImpl rebalance = businessConsumer.getDefaultMQPushConsumerImpl().getRebalanceImpl();
    final PradarEventListener listener = new PradarEventListener() {

        @Override
        public EventResult onEvent(IEvent event) {
            if (event instanceof ClusterTestSwitchOnEvent) {
                try {
                    // 取出配置创建影子消费者
                    DefaultMQPushConsumer defaultMQPushConsumer = buildMQPushConsumer(businessConsumer);
                    if (defaultMQPushConsumer != null) {
                        defaultMQPushConsumer.start();
                    }
                } catch (MQClientException e) {
                    logger.error(e.getMessage());
                    return EventResult.error("apache-rocketmq-plugin-open", "Apache-RocketMQ PT Consumer start failed: " + e.getMessage());
                }
                return EventResult.success("apache-rocketmq-plugin-open");
            } else if (event instanceof ClusterTestSwitchOffEvent || event instanceof SilenceSwitchOnEvent) {
                return shutdownShadowConsumer(businessConsumer);
            } else if (event instanceof ShadowConsumerDisableEvent) {
                String group = businessConsumer.getConsumerGroup();
                Set<String> topics = rebalance.getSubscriptionInner().keySet();
                for (String topic : topics) {
                    List<ShadowConsumerDisableInfo> disableInfos = ((ShadowConsumerDisableEvent) event).getTarget();
                    for (ShadowConsumerDisableInfo disableInfo : disableInfos) {
                        if (topic.equals(disableInfo.getTopic()) && group.equals(disableInfo.getConsumerGroup())) {
                            return shutdownShadowConsumer(businessConsumer);
                        }
                    }
                }
            }
            return EventResult.IGNORE;
        }

        @Override
        public int order() {
            return 4;
        }
    };
    PradarEventListener old = listeners.putIfAbsent(businessConsumer, listener);
    if (old == null) {
        EventRouter.router().addListener(listener);
    }
}
Also used : IEvent(com.pamirs.pradar.pressurement.agent.event.IEvent) RebalanceImpl(org.apache.rocketmq.client.impl.consumer.RebalanceImpl) SilenceSwitchOnEvent(com.pamirs.pradar.pressurement.agent.event.impl.SilenceSwitchOnEvent) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) ShadowConsumerDisableEvent(com.pamirs.pradar.pressurement.agent.event.impl.ShadowConsumerDisableEvent) ShadowConsumerDisableInfo(com.pamirs.pradar.pressurement.agent.listener.model.ShadowConsumerDisableInfo) PradarEventListener(com.pamirs.pradar.pressurement.agent.listener.PradarEventListener) ClusterTestSwitchOnEvent(com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOnEvent) MQClientException(org.apache.rocketmq.client.exception.MQClientException) ClusterTestSwitchOffEvent(com.pamirs.pradar.pressurement.agent.event.impl.ClusterTestSwitchOffEvent)

Example 2 with RebalanceImpl

use of org.apache.rocketmq.client.impl.consumer.RebalanceImpl in project rocketmq-comments by HScarb.

the class DefaultMQLitePullConsumerWithTraceTest method initDefaultLitePullConsumer.

private void initDefaultLitePullConsumer(DefaultLitePullConsumer litePullConsumer) throws Exception {
    asyncTraceDispatcher = (AsyncTraceDispatcher) litePullConsumer.getTraceDispatcher();
    traceProducer = asyncTraceDispatcher.getTraceProducer();
    Field field = DefaultLitePullConsumer.class.getDeclaredField("defaultLitePullConsumerImpl");
    field.setAccessible(true);
    litePullConsumerImpl = (DefaultLitePullConsumerImpl) field.get(litePullConsumer);
    field = DefaultLitePullConsumerImpl.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(litePullConsumerImpl, mQClientFactory);
    PullAPIWrapper pullAPIWrapper = litePullConsumerImpl.getPullAPIWrapper();
    field = PullAPIWrapper.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(pullAPIWrapper, mQClientFactory);
    Field fieldTrace = DefaultMQProducerImpl.class.getDeclaredField("mQClientFactory");
    fieldTrace.setAccessible(true);
    fieldTrace.set(traceProducer.getDefaultMQProducerImpl(), mQClientFactory);
    field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
    field.setAccessible(true);
    field.set(mQClientFactory, mQClientAPIImpl);
    field = MQClientInstance.class.getDeclaredField("mQAdminImpl");
    field.setAccessible(true);
    field.set(mQClientFactory, mQAdminImpl);
    field = DefaultLitePullConsumerImpl.class.getDeclaredField("rebalanceImpl");
    field.setAccessible(true);
    rebalanceImpl = (RebalanceImpl) field.get(litePullConsumerImpl);
    field = RebalanceImpl.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(rebalanceImpl, mQClientFactory);
    offsetStore = spy(litePullConsumerImpl.getOffsetStore());
    field = DefaultLitePullConsumerImpl.class.getDeclaredField("offsetStore");
    field.setAccessible(true);
    field.set(litePullConsumerImpl, offsetStore);
    traceProducer.getDefaultMQProducerImpl().getmQClientFactory().registerProducer(producerGroupTraceTemp, traceProducer.getDefaultMQProducerImpl());
    when(mQClientFactory.getMQClientAPIImpl().pullMessage(anyString(), any(PullMessageRequestHeader.class), anyLong(), any(CommunicationMode.class), nullable(PullCallback.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            PullMessageRequestHeader requestHeader = mock.getArgument(1);
            MessageClientExt messageClientExt = new MessageClientExt();
            messageClientExt.setTopic(topic);
            messageClientExt.setQueueId(0);
            messageClientExt.setMsgId("123");
            messageClientExt.setBody(new byte[] { 'a' });
            messageClientExt.setOffsetMsgId("234");
            messageClientExt.setBornHost(new InetSocketAddress(8080));
            messageClientExt.setStoreHost(new InetSocketAddress(8080));
            PullResult pullResult = createPullResult(requestHeader, PullStatus.FOUND, Collections.<MessageExt>singletonList(messageClientExt));
            return pullResult;
        }
    });
    when(mQClientFactory.findBrokerAddressInSubscribe(anyString(), anyLong(), anyBoolean())).thenReturn(new FindBrokerResult("127.0.0.1:10911", false));
    doReturn(Collections.singletonList(mQClientFactory.getClientId())).when(mQClientFactory).findConsumerIdList(anyString(), anyString());
    doReturn(123L).when(offsetStore).readOffset(any(MessageQueue.class), any(ReadOffsetType.class));
}
Also used : DefaultLitePullConsumerImpl(org.apache.rocketmq.client.impl.consumer.DefaultLitePullConsumerImpl) MessageClientExt(org.apache.rocketmq.common.message.MessageClientExt) CommunicationMode(org.apache.rocketmq.client.impl.CommunicationMode) InetSocketAddress(java.net.InetSocketAddress) RebalanceImpl(org.apache.rocketmq.client.impl.consumer.RebalanceImpl) FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult) PullResult(org.apache.rocketmq.client.consumer.PullResult) PullAPIWrapper(org.apache.rocketmq.client.impl.consumer.PullAPIWrapper) PullMessageRequestHeader(org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader) PullCallback(org.apache.rocketmq.client.consumer.PullCallback) Field(java.lang.reflect.Field) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ReadOffsetType(org.apache.rocketmq.client.consumer.store.ReadOffsetType) MQClientInstance(org.apache.rocketmq.client.impl.factory.MQClientInstance)

Example 3 with RebalanceImpl

use of org.apache.rocketmq.client.impl.consumer.RebalanceImpl in project rocketmq-comments by HScarb.

the class DefaultLitePullConsumerTest method initDefaultLitePullConsumer.

private void initDefaultLitePullConsumer(DefaultLitePullConsumer litePullConsumer) throws Exception {
    Field field = DefaultLitePullConsumer.class.getDeclaredField("defaultLitePullConsumerImpl");
    field.setAccessible(true);
    litePullConsumerImpl = (DefaultLitePullConsumerImpl) field.get(litePullConsumer);
    field = DefaultLitePullConsumerImpl.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(litePullConsumerImpl, mQClientFactory);
    PullAPIWrapper pullAPIWrapper = litePullConsumerImpl.getPullAPIWrapper();
    field = PullAPIWrapper.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(pullAPIWrapper, mQClientFactory);
    field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
    field.setAccessible(true);
    field.set(mQClientFactory, mQClientAPIImpl);
    field = MQClientInstance.class.getDeclaredField("mQAdminImpl");
    field.setAccessible(true);
    field.set(mQClientFactory, mQAdminImpl);
    field = DefaultLitePullConsumerImpl.class.getDeclaredField("rebalanceImpl");
    field.setAccessible(true);
    rebalanceImpl = (RebalanceImpl) field.get(litePullConsumerImpl);
    field = RebalanceImpl.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(rebalanceImpl, mQClientFactory);
    offsetStore = spy(litePullConsumerImpl.getOffsetStore());
    field = DefaultLitePullConsumerImpl.class.getDeclaredField("offsetStore");
    field.setAccessible(true);
    field.set(litePullConsumerImpl, offsetStore);
    when(mQClientFactory.getMQClientAPIImpl().pullMessage(anyString(), any(PullMessageRequestHeader.class), anyLong(), any(CommunicationMode.class), nullable(PullCallback.class))).thenAnswer(new Answer<PullResult>() {

        @Override
        public PullResult answer(InvocationOnMock mock) throws Throwable {
            PullMessageRequestHeader requestHeader = mock.getArgument(1);
            MessageClientExt messageClientExt = new MessageClientExt();
            messageClientExt.setTopic(topic);
            messageClientExt.setQueueId(0);
            messageClientExt.setMsgId("123");
            messageClientExt.setBody(new byte[] { 'a' });
            messageClientExt.setOffsetMsgId("234");
            messageClientExt.setBornHost(new InetSocketAddress(8080));
            messageClientExt.setStoreHost(new InetSocketAddress(8080));
            PullResult pullResult = createPullResult(requestHeader, PullStatus.FOUND, Collections.<MessageExt>singletonList(messageClientExt));
            return pullResult;
        }
    });
    when(mQClientFactory.findBrokerAddressInSubscribe(anyString(), anyLong(), anyBoolean())).thenReturn(new FindBrokerResult("127.0.0.1:10911", false));
    doReturn(Collections.singletonList(mQClientFactory.getClientId())).when(mQClientFactory).findConsumerIdList(anyString(), anyString());
    doReturn(123L).when(offsetStore).readOffset(any(MessageQueue.class), any(ReadOffsetType.class));
}
Also used : DefaultLitePullConsumerImpl(org.apache.rocketmq.client.impl.consumer.DefaultLitePullConsumerImpl) MessageClientExt(org.apache.rocketmq.common.message.MessageClientExt) CommunicationMode(org.apache.rocketmq.client.impl.CommunicationMode) InetSocketAddress(java.net.InetSocketAddress) RebalanceImpl(org.apache.rocketmq.client.impl.consumer.RebalanceImpl) FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult) PullAPIWrapper(org.apache.rocketmq.client.impl.consumer.PullAPIWrapper) PullMessageRequestHeader(org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader) Field(java.lang.reflect.Field) MessageExt(org.apache.rocketmq.common.message.MessageExt) AssignedMessageQueue(org.apache.rocketmq.client.impl.consumer.AssignedMessageQueue) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ReadOffsetType(org.apache.rocketmq.client.consumer.store.ReadOffsetType) MQClientInstance(org.apache.rocketmq.client.impl.factory.MQClientInstance)

Example 4 with RebalanceImpl

use of org.apache.rocketmq.client.impl.consumer.RebalanceImpl in project rocketmq by apache.

the class DefaultMQLitePullConsumerWithTraceTest method initDefaultLitePullConsumer.

private void initDefaultLitePullConsumer(DefaultLitePullConsumer litePullConsumer) throws Exception {
    asyncTraceDispatcher = (AsyncTraceDispatcher) litePullConsumer.getTraceDispatcher();
    traceProducer = asyncTraceDispatcher.getTraceProducer();
    Field field = DefaultLitePullConsumer.class.getDeclaredField("defaultLitePullConsumerImpl");
    field.setAccessible(true);
    litePullConsumerImpl = (DefaultLitePullConsumerImpl) field.get(litePullConsumer);
    field = DefaultLitePullConsumerImpl.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(litePullConsumerImpl, mQClientFactory);
    PullAPIWrapper pullAPIWrapper = litePullConsumerImpl.getPullAPIWrapper();
    field = PullAPIWrapper.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(pullAPIWrapper, mQClientFactory);
    Field fieldTrace = DefaultMQProducerImpl.class.getDeclaredField("mQClientFactory");
    fieldTrace.setAccessible(true);
    fieldTrace.set(traceProducer.getDefaultMQProducerImpl(), mQClientFactory);
    field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
    field.setAccessible(true);
    field.set(mQClientFactory, mQClientAPIImpl);
    field = MQClientInstance.class.getDeclaredField("mQAdminImpl");
    field.setAccessible(true);
    field.set(mQClientFactory, mQAdminImpl);
    field = DefaultLitePullConsumerImpl.class.getDeclaredField("rebalanceImpl");
    field.setAccessible(true);
    rebalanceImpl = (RebalanceImpl) field.get(litePullConsumerImpl);
    field = RebalanceImpl.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(rebalanceImpl, mQClientFactory);
    offsetStore = spy(litePullConsumerImpl.getOffsetStore());
    field = DefaultLitePullConsumerImpl.class.getDeclaredField("offsetStore");
    field.setAccessible(true);
    field.set(litePullConsumerImpl, offsetStore);
    traceProducer.getDefaultMQProducerImpl().getmQClientFactory().registerProducer(producerGroupTraceTemp, traceProducer.getDefaultMQProducerImpl());
    when(mQClientFactory.getMQClientAPIImpl().pullMessage(anyString(), any(PullMessageRequestHeader.class), anyLong(), any(CommunicationMode.class), nullable(PullCallback.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            PullMessageRequestHeader requestHeader = mock.getArgument(1);
            MessageClientExt messageClientExt = new MessageClientExt();
            messageClientExt.setTopic(topic);
            messageClientExt.setQueueId(0);
            messageClientExt.setMsgId("123");
            messageClientExt.setBody(new byte[] { 'a' });
            messageClientExt.setOffsetMsgId("234");
            messageClientExt.setBornHost(new InetSocketAddress(8080));
            messageClientExt.setStoreHost(new InetSocketAddress(8080));
            PullResult pullResult = createPullResult(requestHeader, PullStatus.FOUND, Collections.<MessageExt>singletonList(messageClientExt));
            return pullResult;
        }
    });
    when(mQClientFactory.findBrokerAddressInSubscribe(anyString(), anyLong(), anyBoolean())).thenReturn(new FindBrokerResult("127.0.0.1:10911", false));
    doReturn(Collections.singletonList(mQClientFactory.getClientId())).when(mQClientFactory).findConsumerIdList(anyString(), anyString());
    doReturn(123L).when(offsetStore).readOffset(any(MessageQueue.class), any(ReadOffsetType.class));
}
Also used : DefaultLitePullConsumerImpl(org.apache.rocketmq.client.impl.consumer.DefaultLitePullConsumerImpl) MessageClientExt(org.apache.rocketmq.common.message.MessageClientExt) CommunicationMode(org.apache.rocketmq.client.impl.CommunicationMode) InetSocketAddress(java.net.InetSocketAddress) RebalanceImpl(org.apache.rocketmq.client.impl.consumer.RebalanceImpl) FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult) PullResult(org.apache.rocketmq.client.consumer.PullResult) PullAPIWrapper(org.apache.rocketmq.client.impl.consumer.PullAPIWrapper) PullMessageRequestHeader(org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader) PullCallback(org.apache.rocketmq.client.consumer.PullCallback) Field(java.lang.reflect.Field) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ReadOffsetType(org.apache.rocketmq.client.consumer.store.ReadOffsetType) MQClientInstance(org.apache.rocketmq.client.impl.factory.MQClientInstance)

Example 5 with RebalanceImpl

use of org.apache.rocketmq.client.impl.consumer.RebalanceImpl in project rocketmq by apache.

the class DefaultLitePullConsumerTest method initDefaultLitePullConsumer.

private void initDefaultLitePullConsumer(DefaultLitePullConsumer litePullConsumer) throws Exception {
    Field field = DefaultLitePullConsumer.class.getDeclaredField("defaultLitePullConsumerImpl");
    field.setAccessible(true);
    litePullConsumerImpl = (DefaultLitePullConsumerImpl) field.get(litePullConsumer);
    field = DefaultLitePullConsumerImpl.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(litePullConsumerImpl, mQClientFactory);
    PullAPIWrapper pullAPIWrapper = litePullConsumerImpl.getPullAPIWrapper();
    field = PullAPIWrapper.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(pullAPIWrapper, mQClientFactory);
    field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
    field.setAccessible(true);
    field.set(mQClientFactory, mQClientAPIImpl);
    field = MQClientInstance.class.getDeclaredField("mQAdminImpl");
    field.setAccessible(true);
    field.set(mQClientFactory, mQAdminImpl);
    field = DefaultLitePullConsumerImpl.class.getDeclaredField("rebalanceImpl");
    field.setAccessible(true);
    rebalanceImpl = (RebalanceImpl) field.get(litePullConsumerImpl);
    field = RebalanceImpl.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(rebalanceImpl, mQClientFactory);
    offsetStore = spy(litePullConsumerImpl.getOffsetStore());
    field = DefaultLitePullConsumerImpl.class.getDeclaredField("offsetStore");
    field.setAccessible(true);
    field.set(litePullConsumerImpl, offsetStore);
    when(mQClientFactory.getMQClientAPIImpl().pullMessage(anyString(), any(PullMessageRequestHeader.class), anyLong(), any(CommunicationMode.class), nullable(PullCallback.class))).thenAnswer(new Answer<PullResult>() {

        @Override
        public PullResult answer(InvocationOnMock mock) throws Throwable {
            PullMessageRequestHeader requestHeader = mock.getArgument(1);
            MessageClientExt messageClientExt = new MessageClientExt();
            messageClientExt.setTopic(topic);
            messageClientExt.setQueueId(0);
            messageClientExt.setMsgId("123");
            messageClientExt.setBody(new byte[] { 'a' });
            messageClientExt.setOffsetMsgId("234");
            messageClientExt.setBornHost(new InetSocketAddress(8080));
            messageClientExt.setStoreHost(new InetSocketAddress(8080));
            PullResult pullResult = createPullResult(requestHeader, PullStatus.FOUND, Collections.<MessageExt>singletonList(messageClientExt));
            return pullResult;
        }
    });
    when(mQClientFactory.findBrokerAddressInSubscribe(anyString(), anyLong(), anyBoolean())).thenReturn(new FindBrokerResult("127.0.0.1:10911", false));
    doReturn(Collections.singletonList(mQClientFactory.getClientId())).when(mQClientFactory).findConsumerIdList(anyString(), anyString());
    doReturn(123L).when(offsetStore).readOffset(any(MessageQueue.class), any(ReadOffsetType.class));
}
Also used : DefaultLitePullConsumerImpl(org.apache.rocketmq.client.impl.consumer.DefaultLitePullConsumerImpl) MessageClientExt(org.apache.rocketmq.common.message.MessageClientExt) CommunicationMode(org.apache.rocketmq.client.impl.CommunicationMode) InetSocketAddress(java.net.InetSocketAddress) RebalanceImpl(org.apache.rocketmq.client.impl.consumer.RebalanceImpl) FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult) PullAPIWrapper(org.apache.rocketmq.client.impl.consumer.PullAPIWrapper) PullMessageRequestHeader(org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader) Field(java.lang.reflect.Field) MessageExt(org.apache.rocketmq.common.message.MessageExt) AssignedMessageQueue(org.apache.rocketmq.client.impl.consumer.AssignedMessageQueue) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ReadOffsetType(org.apache.rocketmq.client.consumer.store.ReadOffsetType) MQClientInstance(org.apache.rocketmq.client.impl.factory.MQClientInstance)

Aggregations

RebalanceImpl (org.apache.rocketmq.client.impl.consumer.RebalanceImpl)8 MQClientInstance (org.apache.rocketmq.client.impl.factory.MQClientInstance)7 FindBrokerResult (org.apache.rocketmq.client.impl.FindBrokerResult)5 Field (java.lang.reflect.Field)4 InetSocketAddress (java.net.InetSocketAddress)4 ReadOffsetType (org.apache.rocketmq.client.consumer.store.ReadOffsetType)4 CommunicationMode (org.apache.rocketmq.client.impl.CommunicationMode)4 DefaultLitePullConsumerImpl (org.apache.rocketmq.client.impl.consumer.DefaultLitePullConsumerImpl)4 PullAPIWrapper (org.apache.rocketmq.client.impl.consumer.PullAPIWrapper)4 MessageClientExt (org.apache.rocketmq.common.message.MessageClientExt)4 MessageExt (org.apache.rocketmq.common.message.MessageExt)4 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)4 PullMessageRequestHeader (org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 MQClientAPIImpl (org.apache.rocketmq.client.impl.MQClientAPIImpl)3 DefaultMQPullConsumerImpl (org.apache.rocketmq.client.impl.consumer.DefaultMQPullConsumerImpl)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 DefaultMQPullConsumer (org.apache.rocketmq.client.consumer.DefaultMQPullConsumer)2