Search in sources :

Example 1 with PushConsumer

use of rocket.PushConsumer in project warn-report by saaavsaaa.

the class TestProcess method TestReceive.

@Test
public void TestReceive() throws InterruptedException, RemotingException, MQBrokerException, MQClientException {
    // "192.168.1.45:9876";
    final String address = "192.168.1.45:9876";
    final String topic = "testTopic2";
    MessageListenerConcurrently listener = new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            System.out.println(Thread.currentThread().getName() + " Receive New Messages: " + msgs);
            for (MessageExt ext : msgs) {
                String key = ext.getKeys();
                String keys = ext.getProperty("KEYS");
                String value = new String(ext.getBody());
                System.out.println("key : " + key + "; KEYS :" + keys + "; body : " + value);
            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    };
    final String consumerGroup = "cgr1";
    final String subExpression = "*";
    PushConsumer consumer = null;
    try {
        consumer = new PushConsumer(address, consumerGroup, topic, subExpression, listener);
    } catch (MQClientException e) {
        consumer = new PushConsumer("192.168.1.45:9876", consumerGroup, topic, subExpression, listener);
    }
    if (consumer != null) {
        Thread.sleep(1000);
    }
}
Also used : ConsumeConcurrentlyContext(com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext) MessageExt(com.alibaba.rocketmq.common.message.MessageExt) MessageListenerConcurrently(com.alibaba.rocketmq.client.consumer.listener.MessageListenerConcurrently) PushConsumer(rocket.PushConsumer) List(java.util.List) MQClientException(com.alibaba.rocketmq.client.exception.MQClientException) Test(org.junit.Test)

Aggregations

ConsumeConcurrentlyContext (com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext)1 MessageListenerConcurrently (com.alibaba.rocketmq.client.consumer.listener.MessageListenerConcurrently)1 MQClientException (com.alibaba.rocketmq.client.exception.MQClientException)1 MessageExt (com.alibaba.rocketmq.common.message.MessageExt)1 List (java.util.List)1 Test (org.junit.Test)1 PushConsumer (rocket.PushConsumer)1