Search in sources :

Example 1 with DefaultMQPushConsumer

use of com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer in project uavstack by uavorg.

the class DoTestRocketmqProxy method main.

public static void main(String[] args) {
    ConsoleLogger cl = new ConsoleLogger("test");
    cl.setDebugable(true);
    UAVServer.instance().setLog(cl);
    UAVServer.instance().putServerInfo(CaptureConstants.INFO_APPSERVER_VENDOR, ServerVendor.TOMCAT);
    RocketmqHookProxy p = new RocketmqHookProxy("test", Collections.emptyMap());
    p.doInstallDProxy(null, "testApp");
    DefaultMQProducer producer = new DefaultMQProducer("hookTest");
    producer.setNamesrvAddr("127.0.0.1:9876");
    try {
        producer.start();
        for (int i = 0; i < 100; i++) {
            Message msg = new Message("SELF_TEST_TOPIC", (i + "").getBytes());
            producer.send(msg);
        }
    } catch (MQClientException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (RemotingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MQBrokerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    DefaultMQPushConsumer pushConsumer = new DefaultMQPushConsumer("hookTest1");
    pushConsumer.setNamesrvAddr("127.0.0.1:9876");
    pushConsumer.setMessageModel(MessageModel.BROADCASTING);
    pushConsumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    pushConsumer.registerMessageListener(new MessageListenerOrderly() {

        @Override
        public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
            System.out.println("Consumer1 " + count1++);
            System.out.println(new String(msgs.get(0).getBody()));
            // TODO Auto-generated method stub
            return ConsumeOrderlyStatus.SUCCESS;
        }
    });
    pushConsumer.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            System.out.println("Consumer1 " + count1++);
            System.out.println(new String(msgs.get(0).getBody()));
            // TODO Auto-generated method stub
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    try {
        pushConsumer.subscribe("SELF_TEST_TOPIC", "*");
        pushConsumer.start();
    } catch (MQClientException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    DefaultMQPushConsumer pushConsumer2 = new DefaultMQPushConsumer("hookTest2");
    pushConsumer2.setNamesrvAddr("127.0.0.1:9876");
    pushConsumer2.setMessageModel(MessageModel.BROADCASTING);
    pushConsumer2.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    pushConsumer2.registerMessageListener(new MessageListenerOrderly() {

        @Override
        public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
            System.out.println("Consumer2 " + count2++);
            System.out.println(new String(msgs.get(0).getBody()));
            // TODO Auto-generated method stub
            return ConsumeOrderlyStatus.SUCCESS;
        }
    });
    pushConsumer2.registerMessageListener(new MessageListenerConcurrently() {

        @Override
        public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
            System.out.println("Consumer2 " + count2++);
            System.out.println(new String(msgs.get(0).getBody()));
            // TODO Auto-generated method stub
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    });
    try {
        pushConsumer2.subscribe("SELF_TEST_TOPIC", "*");
        pushConsumer2.start();
    } catch (MQClientException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    // pullConsumer.shutdown();
    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) RocketmqHookProxy(com.creditease.uav.hook.rocketmq.RocketmqHookProxy) 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) ConsoleLogger(com.creditease.monitor.log.ConsoleLogger) ConsumeOrderlyStatus(com.alibaba.rocketmq.client.consumer.listener.ConsumeOrderlyStatus) RemotingException(com.alibaba.rocketmq.remoting.exception.RemotingException) MQClientException(com.alibaba.rocketmq.client.exception.MQClientException)

Example 2 with DefaultMQPushConsumer

use of com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer 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("10.100.33.135: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("10.100.33.135: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 3 with DefaultMQPushConsumer

use of com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer in project warn-report by saaavsaaa.

the class PushConsumerServiceImpl method buildPushConsumer.

private DefaultMQPushConsumer buildPushConsumer(final String address, final String consumerGroup, final String topic, final String subExpression, final MessageListenerConcurrently messageListener) throws MQClientException {
    DefaultMQPushConsumer consumer = new DefaultMQPushConsumer();
    consumer.setNamesrvAddr(address);
    consumer.setConsumerGroup(consumerGroup);
    consumer.subscribe(topic, subExpression);
    consumer.registerMessageListener(messageListener);
    consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
    return consumer;
}
Also used : DefaultMQPushConsumer(com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer)

Example 4 with DefaultMQPushConsumer

use of com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer in project warn-report by saaavsaaa.

the class PushConsumerServiceImpl method run.

@Override
public void run() throws MQClientException, InterruptedException {
    System.out.println("case : PushConsumerServiceImpl start");
    RocketListener listener = new RocketListener(receive);
    DefaultMQPushConsumer consumer;
    try {
        consumer = buildPushConsumer(namesrvAddr, consumerGroup, topic, subExpression, listener);
        consumer.start();
        System.out.println("case : consumer start");
    } catch (Exception e) {
        consumer = buildPushConsumer(namesrvAddrBak, consumerGroup, topic, subExpression, listener);
        consumer.start();
        System.out.println("case : consumerbak start, reason : consumer error" + e.getMessage());
    }
}
Also used : DefaultMQPushConsumer(com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer) MQClientException(com.alibaba.rocketmq.client.exception.MQClientException) RocketListener(rocketDoubleWrite.listener.RocketListener)

Example 5 with DefaultMQPushConsumer

use of com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer 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)

Aggregations

DefaultMQPushConsumer (com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer)6 MQClientException (com.alibaba.rocketmq.client.exception.MQClientException)4 ConsumeConcurrentlyContext (com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext)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 MessageListenerConcurrently (com.alibaba.rocketmq.client.consumer.listener.MessageListenerConcurrently)3 MessageListenerOrderly (com.alibaba.rocketmq.client.consumer.listener.MessageListenerOrderly)3 MQBrokerException (com.alibaba.rocketmq.client.exception.MQBrokerException)3 DefaultMQProducer (com.alibaba.rocketmq.client.producer.DefaultMQProducer)3 Message (com.alibaba.rocketmq.common.message.Message)3 MessageExt (com.alibaba.rocketmq.common.message.MessageExt)3 RemotingException (com.alibaba.rocketmq.remoting.exception.RemotingException)3 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 ConsoleLogger (com.creditease.monitor.log.ConsoleLogger)1 RocketmqHookProxy (com.creditease.uav.hook.rocketmq.RocketmqHookProxy)1 RocketListener (rocketDoubleWrite.listener.RocketListener)1