Search in sources :

Example 1 with ConsumeConcurrentlyContext

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext in project rocketmq-externals by apache.

the class RocketMQConsoleTestBase method startTestMQConsumer.

protected void startTestMQConsumer() {
    // test online consumer
    consumer = new DefaultMQPushConsumer(TEST_CONSUMER_GROUP);
    consumerConfigInfo.setBrokerNameList(Lists.newArrayList(TestConstant.TEST_BROKER_NAME));
    SubscriptionGroupConfig subscriptionGroupConfig = new SubscriptionGroupConfig();
    subscriptionGroupConfig.setGroupName(TEST_CONSUMER_GROUP);
    subscriptionGroupConfig.setRetryQueueNums(RETRY_QUEUE_NUMS);
    consumerConfigInfo.setSubscriptionGroupConfig(subscriptionGroupConfig);
    consumerService.createAndUpdateSubscriptionGroupConfig(consumerConfigInfo);
    consumer.setNamesrvAddr(TestConstant.NAME_SERVER_ADDRESS);
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    try {
        consumer.subscribe(TEST_CONSOLE_TOPIC, "*");
        consumer.registerMessageListener(new MessageListenerConcurrently() {

            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                consoleTestBaseLog.info("op=consumeMessage message={}", JsonUtil.obj2String(msgs));
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) SubscriptionGroupConfig(org.apache.rocketmq.common.subscription.SubscriptionGroupConfig)

Example 2 with ConsumeConcurrentlyContext

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext in project rocketmq-externals by apache.

the class ConsumerTest method main.

public static void main(String[] args) {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("g00003");
    consumer.setNamesrvAddr("localhost:9876");
    try {
        consumer.subscribe("flink-sink2", "*");
    } catch (MQClientException e) {
        e.printStackTrace();
    }
    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            for (MessageExt msg : msgs) {
                System.out.println(msg.getKeys() + ":" + new String(msg.getBody()));
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    try {
        consumer.start();
    } catch (MQClientException e) {
        e.printStackTrace();
    }
}
Also used : ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 3 with ConsumeConcurrentlyContext

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext in project rocketmq by apache.

the class DefaultMQPushConsumerTest method init.

@Before
public void init() throws Exception {
    consumerGroup = "FooBarGroup" + System.currentTimeMillis();
    pushConsumer = new DefaultMQPushConsumer(consumerGroup);
    pushConsumer.setNamesrvAddr("127.0.0.1:9876");
    pushConsumer.setPullInterval(60 * 1000);
    pushConsumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            return null;
        }
    });
    DefaultMQPushConsumerImpl pushConsumerImpl = pushConsumer.getDefaultMQPushConsumerImpl();
    rebalancePushImpl = spy(new RebalancePushImpl(pushConsumer.getDefaultMQPushConsumerImpl()));
    Field field = DefaultMQPushConsumerImpl.class.getDeclaredField("rebalanceImpl");
    field.setAccessible(true);
    field.set(pushConsumerImpl, rebalancePushImpl);
    pushConsumer.subscribe(topic, "*");
    pushConsumer.start();
    mQClientFactory = spy(pushConsumerImpl.getmQClientFactory());
    field = DefaultMQPushConsumerImpl.class.getDeclaredField("mQClientFactory");
    field.setAccessible(true);
    field.set(pushConsumerImpl, mQClientFactory);
    field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
    field.setAccessible(true);
    field.set(mQClientFactory, mQClientAPIImpl);
    pullAPIWrapper = spy(new PullAPIWrapper(mQClientFactory, consumerGroup, false));
    field = DefaultMQPushConsumerImpl.class.getDeclaredField("pullAPIWrapper");
    field.setAccessible(true);
    field.set(pushConsumerImpl, pullAPIWrapper);
    pushConsumer.getDefaultMQPushConsumerImpl().getRebalanceImpl().setmQClientFactory(mQClientFactory);
    mQClientFactory.registerConsumer(consumerGroup, pushConsumerImpl);
    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));
            ((PullCallback) mock.getArgument(4)).onSuccess(pullResult);
            return pullResult;
        }
    });
    doReturn(new FindBrokerResult("127.0.0.1:10911", false)).when(mQClientFactory).findBrokerAddressInSubscribe(anyString(), anyLong(), anyBoolean());
    doReturn(Collections.singletonList(mQClientFactory.getClientId())).when(mQClientFactory).findConsumerIdList(anyString(), anyString());
    Set<MessageQueue> messageQueueSet = new HashSet<MessageQueue>();
    messageQueueSet.add(createPullRequest().getMessageQueue());
    pushConsumer.getDefaultMQPushConsumerImpl().updateTopicSubscribeInfo(topic, messageQueueSet);
    doReturn(123L).when(rebalancePushImpl).computePullFromWhere(any(MessageQueue.class));
}
Also used : MessageClientExt(org.apache.rocketmq.common.message.MessageClientExt) CommunicationMode(org.apache.rocketmq.client.impl.CommunicationMode) InetSocketAddress(java.net.InetSocketAddress) FindBrokerResult(org.apache.rocketmq.client.impl.FindBrokerResult) ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) DefaultMQPushConsumerImpl(org.apache.rocketmq.client.impl.consumer.DefaultMQPushConsumerImpl) Field(java.lang.reflect.Field) RebalancePushImpl(org.apache.rocketmq.client.impl.consumer.RebalancePushImpl) HashSet(java.util.HashSet) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) PullAPIWrapper(org.apache.rocketmq.client.impl.consumer.PullAPIWrapper) PullMessageRequestHeader(org.apache.rocketmq.common.protocol.header.PullMessageRequestHeader) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MQClientInstance(org.apache.rocketmq.client.impl.factory.MQClientInstance) Before(org.junit.Before)

Example 4 with ConsumeConcurrentlyContext

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext in project rocketmq by apache.

the class StatsBenchmarkConsumer method main.

public static void main(String[] args) throws MQClientException, IOException {
    Options options = ServerUtil.buildCommandlineOptions(new Options());
    CommandLine commandLine = ServerUtil.parseCmdLine("benchmarkConsumer", args, buildCommandlineOptions(options), new PosixParser());
    if (null == commandLine) {
        System.exit(-1);
    }
    final String topic = commandLine.hasOption('t') ? commandLine.getOptionValue('t').trim() : "BenchmarkTest";
    final String groupPrefix = commandLine.hasOption('g') ? commandLine.getOptionValue('g').trim() : "benchmark_consumer";
    final String isPrefixEnable = commandLine.hasOption('p') ? commandLine.getOptionValue('p').trim() : "true";
    final String filterType = commandLine.hasOption('f') ? commandLine.getOptionValue('f').trim() : null;
    final String expression = commandLine.hasOption('e') ? commandLine.getOptionValue('e').trim() : null;
    String group = groupPrefix;
    if (Boolean.parseBoolean(isPrefixEnable)) {
        group = groupPrefix + "_" + Long.toString(System.currentTimeMillis() % 100);
    }
    System.out.printf("topic: %s, group: %s, prefix: %s, filterType: %s, expression: %s%n", topic, group, isPrefixEnable, filterType, expression);
    final StatsBenchmarkConsumer statsBenchmarkConsumer = new StatsBenchmarkConsumer();
    final Timer timer = new Timer("BenchmarkTimerThread", true);
    final LinkedList<Long[]> snapshotList = new LinkedList<Long[]>();
    timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            snapshotList.addLast(statsBenchmarkConsumer.createSnapshot());
            if (snapshotList.size() > 10) {
                snapshotList.removeFirst();
            }
        }
    }, 1000, 1000);
    timer.scheduleAtFixedRate(new TimerTask() {

        private void printStats() {
            if (snapshotList.size() >= 10) {
                Long[] begin = snapshotList.getFirst();
                Long[] end = snapshotList.getLast();
                final long consumeTps = (long) (((end[1] - begin[1]) / (double) (end[0] - begin[0])) * 1000L);
                final double averageB2CRT = (end[2] - begin[2]) / (double) (end[1] - begin[1]);
                final double averageS2CRT = (end[3] - begin[3]) / (double) (end[1] - begin[1]);
                System.out.printf("Consume TPS: %d Average(B2C) RT: %7.3f Average(S2C) RT: %7.3f MAX(B2C) RT: %d MAX(S2C) RT: %d%n", consumeTps, averageB2CRT, averageS2CRT, end[4], end[5]);
            }
        }

        @Override
        public void run() {
            try {
                this.printStats();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, 10000, 10000);
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(group);
    consumer.setInstanceName(Long.toString(System.currentTimeMillis()));
    if (filterType == null || expression == null) {
        consumer.subscribe(topic, "*");
    } else {
        if (ExpressionType.TAG.equals(filterType)) {
            String expr = MixAll.file2String(expression);
            System.out.printf("Expression: %s%n", expr);
            consumer.subscribe(topic, MessageSelector.byTag(expr));
        } else if (ExpressionType.SQL92.equals(filterType)) {
            String expr = MixAll.file2String(expression);
            System.out.printf("Expression: %s%n", expr);
            consumer.subscribe(topic, MessageSelector.bySql(expr));
        } else {
            throw new IllegalArgumentException("Not support filter type! " + filterType);
        }
    }
    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            MessageExt msg = msgs.get(0);
            long now = System.currentTimeMillis();
            statsBenchmarkConsumer.getReceiveMessageTotalCount().incrementAndGet();
            long born2ConsumerRT = now - msg.getBornTimestamp();
            statsBenchmarkConsumer.getBorn2ConsumerTotalRT().addAndGet(born2ConsumerRT);
            long store2ConsumerRT = now - msg.getStoreTimestamp();
            statsBenchmarkConsumer.getStore2ConsumerTotalRT().addAndGet(store2ConsumerRT);
            compareAndSetMax(statsBenchmarkConsumer.getBorn2ConsumerMaxRT(), born2ConsumerRT);
            compareAndSetMax(statsBenchmarkConsumer.getStore2ConsumerMaxRT(), store2ConsumerRT);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    consumer.start();
    System.out.printf("Consumer Started.%n");
}
Also used : Options(org.apache.commons.cli.Options) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) PosixParser(org.apache.commons.cli.PosixParser) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer) LinkedList(java.util.LinkedList) IOException(java.io.IOException) MQClientException(org.apache.rocketmq.client.exception.MQClientException) ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) CommandLine(org.apache.commons.cli.CommandLine) Timer(java.util.Timer) TimerTask(java.util.TimerTask) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 5 with ConsumeConcurrentlyContext

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext in project rocketmq by apache.

the class PushConsumer method main.

public static void main(String[] args) throws InterruptedException, MQClientException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_1");
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    consumer.setMessageModel(MessageModel.BROADCASTING);
    consumer.subscribe("TopicTest", "TagA || TagC || TagD");
    consumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    consumer.start();
    System.out.printf("Broadcast Consumer Started.%n");
}
Also used : ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)

Aggregations

ConsumeConcurrentlyContext (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext)40 MessageExt (org.apache.rocketmq.common.message.MessageExt)40 ConsumeConcurrentlyStatus (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus)38 MessageListenerConcurrently (org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently)38 DefaultMQPushConsumer (org.apache.rocketmq.client.consumer.DefaultMQPushConsumer)33 MQClientException (org.apache.rocketmq.client.exception.MQClientException)11 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 CommandLine (org.apache.commons.cli.CommandLine)4 MessageQueue (org.apache.rocketmq.common.message.MessageQueue)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 File (java.io.File)2 IOException (java.io.IOException)2 Field (java.lang.reflect.Field)2 InetSocketAddress (java.net.InetSocketAddress)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2