Java Code Examples for io.openmessaging.consumer.MessageListener#Context

The following examples show how to use io.openmessaging.consumer.MessageListener#Context . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PushConsumerImpl.java    From rocketmq-4.3.0 with Apache License 2.0 4 votes vote down vote up
@Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> rmqMsgList,
            ConsumeConcurrentlyContext contextRMQ) {
            MessageExt rmqMsg = rmqMsgList.get(0);
            BytesMessage omsMsg = OMSUtil.msgConvert(rmqMsg);

            MessageListener listener = PushConsumerImpl.this.subscribeTable.get(rmqMsg.getTopic());

            if (listener == null) {
                throw new OMSRuntimeException("-1",
                    String.format("The topic/queue %s isn't attached to this consumer", rmqMsg.getTopic()));
            }

            final KeyValue contextProperties = OMS.newKeyValue();
//            让一段代码执行完毕后再往下执行
            final CountDownLatch sync = new CountDownLatch(1);

            contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ConsumeConcurrentlyStatus.RECONSUME_LATER.name());

            MessageListener.Context context = new MessageListener.Context() {
                @Override
                public KeyValue attributes() {
                    return contextProperties;
                }

                @Override
                public void ack() {
                    sync.countDown();
                    contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS,
                        ConsumeConcurrentlyStatus.CONSUME_SUCCESS.name());
                }
            };
            long begin = System.currentTimeMillis();
            listener.onReceived(omsMsg, context);
            long costs = System.currentTimeMillis() - begin;
            long timeoutMills = clientConfig.getRmqMessageConsumeTimeout() * 60 * 1000;
            try {
                sync.await(Math.max(0, timeoutMills - costs), TimeUnit.MILLISECONDS);
            } catch (InterruptedException ignore) {
            }

            return ConsumeConcurrentlyStatus.valueOf(contextProperties.getString(NonStandardKeys.MESSAGE_CONSUME_STATUS));
        }
 
Example 2
Source File: PushConsumerImpl.java    From rocketmq-read with Apache License 2.0 4 votes vote down vote up
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> rmqMsgList,
    ConsumeConcurrentlyContext contextRMQ) {
    MessageExt rmqMsg = rmqMsgList.get(0);
    BytesMessage omsMsg = OMSUtil.msgConvert(rmqMsg);

    MessageListener listener = PushConsumerImpl.this.subscribeTable.get(rmqMsg.getTopic());

    if (listener == null) {
        throw new OMSRuntimeException("-1",
            String.format("The topic/queue %s isn't attached to this consumer", rmqMsg.getTopic()));
    }

    final KeyValue contextProperties = OMS.newKeyValue();
    final CountDownLatch sync = new CountDownLatch(1);

    contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ConsumeConcurrentlyStatus.RECONSUME_LATER.name());

    MessageListener.Context context = new MessageListener.Context() {
        @Override
        public KeyValue attributes() {
            return contextProperties;
        }

        @Override
        public void ack() {
            sync.countDown();
            contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS,
                ConsumeConcurrentlyStatus.CONSUME_SUCCESS.name());
        }
    };
    long begin = System.currentTimeMillis();
    listener.onReceived(omsMsg, context);
    long costs = System.currentTimeMillis() - begin;
    long timeoutMills = clientConfig.getRmqMessageConsumeTimeout() * 60 * 1000;
    try {
        sync.await(Math.max(0, timeoutMills - costs), TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignore) {
    }

    return ConsumeConcurrentlyStatus.valueOf(contextProperties.getString(NonStandardKeys.MESSAGE_CONSUME_STATUS));
}
 
Example 3
Source File: SimpleMessageListener2.java    From joyqueue with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceived(Message message, MessageListener.Context context) {
    System.out.println(String.format("receive, message: %s", message));
    context.ack();
}
 
Example 4
Source File: SimpleMessageListener1.java    From joyqueue with Apache License 2.0 4 votes vote down vote up
@OMSMessageListener(queueName = "test_topic_0")
public void onReceived(Message message, MessageListener.Context context) {
    System.out.println(String.format("receive, message: %s", message));
    context.ack();
}
 
Example 5
Source File: MessageListener1.java    From joyqueue with Apache License 2.0 4 votes vote down vote up
@OMSMessageListener(queueName = "test_topic_0")
public void onReceived(Message message, MessageListener.Context context) {
    System.out.println(String.format("receive, message: %s", message));
    context.ack();
}
 
Example 6
Source File: MessageListener2.java    From joyqueue with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceived(Message message, MessageListener.Context context) {
    System.out.println(String.format("receive, message: %s", message));
    context.ack();
}
 
Example 7
Source File: PushConsumerImpl.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> rmqMsgList,
    ConsumeConcurrentlyContext contextRMQ) {
    MessageExt rmqMsg = rmqMsgList.get(0);
    BytesMessage omsMsg = OMSUtil.msgConvert(rmqMsg);

    MessageListener listener = PushConsumerImpl.this.subscribeTable.get(rmqMsg.getTopic());

    if (listener == null) {
        throw new OMSRuntimeException("-1",
            String.format("The topic/queue %s isn't attached to this consumer", rmqMsg.getTopic()));
    }

    final KeyValue contextProperties = OMS.newKeyValue();
    final CountDownLatch sync = new CountDownLatch(1);

    contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS, ConsumeConcurrentlyStatus.RECONSUME_LATER.name());

    MessageListener.Context context = new MessageListener.Context() {
        @Override
        public KeyValue attributes() {
            return contextProperties;
        }

        @Override
        public void ack() {
            sync.countDown();
            contextProperties.put(NonStandardKeys.MESSAGE_CONSUME_STATUS,
                ConsumeConcurrentlyStatus.CONSUME_SUCCESS.name());
        }
    };
    long begin = System.currentTimeMillis();
    listener.onReceived(omsMsg, context);
    long costs = System.currentTimeMillis() - begin;
    long timeoutMills = clientConfig.getRmqMessageConsumeTimeout() * 60 * 1000;
    try {
        sync.await(Math.max(0, timeoutMills - costs), TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignore) {
    }

    return ConsumeConcurrentlyStatus.valueOf(contextProperties.getString(NonStandardKeys.MESSAGE_CONSUME_STATUS));
}