Search in sources :

Example 36 with ConsumeConcurrentlyContext

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

the class DefaultMQPushConsumerTest method testPullMessage_Success.

@Test
public void testPullMessage_Success() throws InterruptedException, RemotingException, MQBrokerException {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final MessageExt[] messageExts = new MessageExt[1];
    pushConsumer.getDefaultMQPushConsumerImpl().setConsumeMessageService(new ConsumeMessageConcurrentlyService(pushConsumer.getDefaultMQPushConsumerImpl(), new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            messageExts[0] = msgs.get(0);
            countDownLatch.countDown();
            return null;
        }
    }));
    PullMessageService pullMessageService = mQClientFactory.getPullMessageService();
    pullMessageService.executePullRequestImmediately(createPullRequest());
    countDownLatch.await();
    assertThat(messageExts[0].getTopic()).isEqualTo(topic);
    assertThat(messageExts[0].getBody()).isEqualTo(new byte[] { 'a' });
}
Also used : ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) PullMessageService(org.apache.rocketmq.client.impl.consumer.PullMessageService) MessageExt(org.apache.rocketmq.common.message.MessageExt) MessageListenerConcurrently(org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently) ConsumeMessageConcurrentlyService(org.apache.rocketmq.client.impl.consumer.ConsumeMessageConcurrentlyService) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 37 with ConsumeConcurrentlyContext

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

the class DefaultMQPushConsumerTest method createPushConsumer.

private DefaultMQPushConsumer createPushConsumer() {
    DefaultMQPushConsumer pushConsumer = new DefaultMQPushConsumer(consumerGroup);
    pushConsumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            return null;
        }
    });
    return pushConsumer;
}
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)

Example 38 with ConsumeConcurrentlyContext

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

the class DefaultMQPushConsumerImplTest method checkConfigTest.

@Test
public void checkConfigTest() throws MQClientException {
    // test type
    thrown.expect(MQClientException.class);
    // test message
    thrown.expectMessage("consumeThreadMin (10) is larger than consumeThreadMax (9)");
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("test_consumer_group");
    consumer.setConsumeThreadMin(10);
    consumer.setConsumeThreadMax(9);
    consumer.registerMessageListener(new MessageListenerConcurrently() {

        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            System.out.println(" Receive New Messages: " + msgs);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    DefaultMQPushConsumerImpl defaultMQPushConsumerImpl = new DefaultMQPushConsumerImpl(consumer, null);
    defaultMQPushConsumerImpl.start();
}
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) Test(org.junit.Test)

Example 39 with ConsumeConcurrentlyContext

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

the class SqlConsumer method main.

public static void main(String[] args) {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_4");
    try {
        consumer.subscribe("TopicTest", MessageSelector.bySql("(TAGS is not null and TAGS in ('TagA', 'TagB'))" + "and (a is not null and a between 0  3)"));
    } catch (MQClientException e) {
        e.printStackTrace();
        return;
    }
    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;
        }
    });
    try {
        consumer.start();
    } catch (MQClientException e) {
        e.printStackTrace();
        return;
    }
    System.out.printf("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) MQClientException(org.apache.rocketmq.client.exception.MQClientException)

Example 40 with ConsumeConcurrentlyContext

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

the class ConsumeMessageConcurrentlyService method consumeMessageDirectly.

@Override
public ConsumeMessageDirectlyResult consumeMessageDirectly(MessageExt msg, String brokerName) {
    ConsumeMessageDirectlyResult result = new ConsumeMessageDirectlyResult();
    result.setOrder(false);
    result.setAutoCommit(true);
    List<MessageExt> msgs = new ArrayList<MessageExt>();
    msgs.add(msg);
    MessageQueue mq = new MessageQueue();
    mq.setBrokerName(brokerName);
    mq.setTopic(msg.getTopic());
    mq.setQueueId(msg.getQueueId());
    ConsumeConcurrentlyContext context = new ConsumeConcurrentlyContext(mq);
    this.resetRetryTopic(msgs);
    final long beginTime = System.currentTimeMillis();
    log.info("consumeMessageDirectly receive new message: {}", msg);
    try {
        ConsumeConcurrentlyStatus status = this.messageListener.consumeMessage(msgs, context);
        if (status != null) {
            switch(status) {
                case CONSUME_SUCCESS:
                    result.setConsumeResult(CMResult.CR_SUCCESS);
                    break;
                case RECONSUME_LATER:
                    result.setConsumeResult(CMResult.CR_LATER);
                    break;
                default:
                    break;
            }
        } else {
            result.setConsumeResult(CMResult.CR_RETURN_NULL);
        }
    } catch (Throwable e) {
        result.setConsumeResult(CMResult.CR_THROW_EXCEPTION);
        result.setRemark(RemotingHelper.exceptionSimpleDesc(e));
        log.warn(String.format("consumeMessageDirectly exception: %s Group: %s Msgs: %s MQ: %s", RemotingHelper.exceptionSimpleDesc(e), ConsumeMessageConcurrentlyService.this.consumerGroup, msgs, mq), e);
    }
    result.setSpentTimeMills(System.currentTimeMillis() - beginTime);
    log.info("consumeMessageDirectly Result: {}", result);
    return result;
}
Also used : ConsumeConcurrentlyContext(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(org.apache.rocketmq.common.message.MessageExt) ConsumeMessageDirectlyResult(org.apache.rocketmq.common.protocol.body.ConsumeMessageDirectlyResult) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) ArrayList(java.util.ArrayList) ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus)

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