Search in sources :

Example 36 with ConsumeConcurrentlyStatus

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus 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 37 with ConsumeConcurrentlyStatus

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus 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 38 with ConsumeConcurrentlyStatus

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus 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 39 with ConsumeConcurrentlyStatus

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus 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)

Example 40 with ConsumeConcurrentlyStatus

use of org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus in project pinpoint by naver.

the class ConsumerMessageListenerConcurrentlyInterceptor method doInAfterTrace.

@Override
protected void doInAfterTrace(SpanEventRecorder recorder, Object target, Object[] args, Object result, Throwable throwable) {
    recorder.recordApi(methodDescriptor);
    if (result instanceof ConsumeConcurrentlyStatus) {
        final ConsumeConcurrentlyStatus status = (ConsumeConcurrentlyStatus) result;
        recorder.recordAttribute(RocketMQConstants.ROCKETMQ_SEND_STATUS_ANNOTATION_KEY, status.name());
    }
    if (throwable != null) {
        recorder.recordException(throwable);
    }
}
Also used : ConsumeConcurrentlyStatus(org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus)

Aggregations

ConsumeConcurrentlyStatus (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus)40 ConsumeConcurrentlyContext (org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext)38 MessageExt (org.apache.rocketmq.common.message.MessageExt)38 MessageListenerConcurrently (org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently)36 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 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 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2