Search in sources :

Example 16 with MessageExt

use of com.alibaba.rocketmq.common.message.MessageExt in project uavstack by uavorg.

the class TestRestService method testRocketmq.

@POST
@Path("testRocketmq")
public void testRocketmq(String jsonString) {
    DefaultMQProducer producer = new DefaultMQProducer("hookTest");
    producer.setNamesrvAddr("127.0.0.1:9876");
    Message msg = new Message("SELF_TEST_TOPIC", "test".getBytes());
    try {
        producer.start();
        for (int i = 0; i < 10; i++) {
            producer.send(msg);
        }
    } catch (MQClientException e) {
        e.printStackTrace();
    } catch (RemotingException e) {
        e.printStackTrace();
    } catch (MQBrokerException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    DefaultMQPushConsumer pushConsumer = new DefaultMQPushConsumer("hookTest");
    pushConsumer.setNamesrvAddr("127.0.0.1:9876");
    pushConsumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    pushConsumer.registerMessageListener(new MessageListenerOrderly() {

        @Override
        public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
            System.out.println("haha");
            return ConsumeOrderlyStatus.SUCCESS;
        }
    });
    pushConsumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            System.out.println("haha");
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    try {
        pushConsumer.subscribe("SELF_TEST_TOPIC", null);
        pushConsumer.start();
    } catch (MQClientException e1) {
        e1.printStackTrace();
    }
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    pushConsumer.shutdown();
    producer.shutdown();
}
Also used : MessageListenerConcurrently(com.alibaba.rocketmq.client.consumer.listener.MessageListenerConcurrently) Message(com.alibaba.rocketmq.common.message.Message) MessageListenerOrderly(com.alibaba.rocketmq.client.consumer.listener.MessageListenerOrderly) MQBrokerException(com.alibaba.rocketmq.client.exception.MQBrokerException) ConsumeConcurrentlyStatus(com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus) DefaultMQPushConsumer(com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer) ConsumeOrderlyContext(com.alibaba.rocketmq.client.consumer.listener.ConsumeOrderlyContext) DefaultMQProducer(com.alibaba.rocketmq.client.producer.DefaultMQProducer) ConsumeConcurrentlyContext(com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(com.alibaba.rocketmq.common.message.MessageExt) ConsumeOrderlyStatus(com.alibaba.rocketmq.client.consumer.listener.ConsumeOrderlyStatus) RemotingException(com.alibaba.rocketmq.remoting.exception.RemotingException) MQClientException(com.alibaba.rocketmq.client.exception.MQClientException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 17 with MessageExt

use of com.alibaba.rocketmq.common.message.MessageExt in project warn-report by saaavsaaa.

the class PullConsumerCluter method getMessage.

public List<MessageExt> getMessage(final String topic, final Long offset, final String subExpression) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
    List<MessageExt> result = new ArrayList<MessageExt>();
    Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues(topic);
    for (MessageQueue mq : mqs) {
        PullResult pullResult = consumer.pullBlockIfNotFound(mq, subExpression, offset, 1);
        if (pullResult.getPullStatus() == PullStatus.FOUND) {
            List<MessageExt> extList = pullResult.getMsgFoundList();
            result.addAll(extList);
        }
    }
    return result;
}
Also used : MessageExt(com.alibaba.rocketmq.common.message.MessageExt) MessageQueue(com.alibaba.rocketmq.common.message.MessageQueue) ArrayList(java.util.ArrayList) PullResult(com.alibaba.rocketmq.client.consumer.PullResult)

Example 18 with MessageExt

use of com.alibaba.rocketmq.common.message.MessageExt in project uavstack by uavorg.

the class RocketMQConsumer method handleRocketMqMessage.

private void handleRocketMqMessage(List<MessageExt> msgs, MQMessageListener messageListner) {
    for (MessageExt msg : msgs) {
        MQMessage ceMessage = new MQMessage();
        byte[] msgBody = msg.getBody();
        ceMessage.setMessage(msgBody);
        try {
            messageListner.handle(ceMessage);
        } catch (Exception e) {
            log.err(this, "MsgId=" + msg.getMsgId() + ",Topic=" + msg.getTopic() + ",MsgBornTimeStamp=" + msg.getBornTimestamp() + "处理异常:" + e.getMessage(), e);
        }
    }
}
Also used : MessageExt(com.alibaba.rocketmq.common.message.MessageExt) MQClientException(com.alibaba.rocketmq.client.exception.MQClientException)

Aggregations

MessageExt (com.alibaba.rocketmq.common.message.MessageExt)18 MQClientException (com.alibaba.rocketmq.client.exception.MQClientException)10 MessageQueue (com.alibaba.rocketmq.common.message.MessageQueue)6 List (java.util.List)6 Test (org.junit.Test)6 PullResult (com.alibaba.rocketmq.client.consumer.PullResult)5 ConsumeConcurrentlyContext (com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext)5 MessageListenerConcurrently (com.alibaba.rocketmq.client.consumer.listener.MessageListenerConcurrently)5 MQBrokerException (com.alibaba.rocketmq.client.exception.MQBrokerException)5 RemotingException (com.alibaba.rocketmq.remoting.exception.RemotingException)5 ArrayList (java.util.ArrayList)5 DefaultMQPushConsumer (com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer)3 ConsumeConcurrentlyStatus (com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus)3 ConsumeOrderlyContext (com.alibaba.rocketmq.client.consumer.listener.ConsumeOrderlyContext)3 ConsumeOrderlyStatus (com.alibaba.rocketmq.client.consumer.listener.ConsumeOrderlyStatus)3 MessageListenerOrderly (com.alibaba.rocketmq.client.consumer.listener.MessageListenerOrderly)3 DefaultMQProducer (com.alibaba.rocketmq.client.producer.DefaultMQProducer)3 Message (com.alibaba.rocketmq.common.message.Message)3 Map (java.util.Map)3 HashMap (java.util.HashMap)2