Search in sources :

Example 11 with DefaultMQProducer

use of com.alibaba.rocketmq.client.producer.DefaultMQProducer 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

DefaultMQProducer (com.alibaba.rocketmq.client.producer.DefaultMQProducer)11 MQClientException (com.alibaba.rocketmq.client.exception.MQClientException)5 Message (com.alibaba.rocketmq.common.message.Message)5 MQBrokerException (com.alibaba.rocketmq.client.exception.MQBrokerException)4 RemotingException (com.alibaba.rocketmq.remoting.exception.RemotingException)4 DefaultMQPushConsumer (com.alibaba.rocketmq.client.consumer.DefaultMQPushConsumer)3 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 MessageExt (com.alibaba.rocketmq.common.message.MessageExt)3 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 SendResult (com.alibaba.rocketmq.client.producer.SendResult)1 ConsoleLogger (com.creditease.monitor.log.ConsoleLogger)1 RocketmqHookProxy (com.creditease.uav.hook.rocketmq.RocketmqHookProxy)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1