Search in sources :

Example 6 with MessageExt

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

the class RocketMQPushConsumerAdapter method afterPreCap.

@Override
public void afterPreCap(InvokeChainContext context, Object[] args) {
    if (UAVServer.instance().isExistSupportor("com.creditease.uav.apm.supporters.SlowOperSupporter")) {
        String storeKey = (String) context.get(InvokeChainConstants.CLIENT_SPAN_THREADLOCAL_STOREKEY);
        Span span = this.spanFactory.getSpanFromContext(storeKey);
        if (span == null) {
            return;
        }
        @SuppressWarnings("unchecked") List<MessageExt> exts = (List<MessageExt>) args[0];
        String content;
        try {
            content = new String(exts.get(0).getBody(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            content = "unsupported encoding,defalut is utf-8.try to set ContentEncoding to fit.";
        }
        SlowOperContext slowOperContext = new SlowOperContext();
        slowOperContext.put(SlowOperConstants.PROTOCOL_MQ_RABBIT_BODY, content);
        Object[] params = { span, slowOperContext };
        UAVServer.instance().runSupporter("com.creditease.uav.apm.supporters.SlowOperSupporter", "runCap", span.getEndpointInfo().split(",")[0], InvokeChainConstants.CapturePhase.PRECAP, context, params);
    }
}
Also used : MessageExt(com.alibaba.rocketmq.common.message.MessageExt) SlowOperContext(com.creditease.uav.apm.slowoper.spi.SlowOperContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) List(java.util.List) Span(com.creditease.uav.apm.invokechain.span.Span)

Example 7 with MessageExt

use of com.alibaba.rocketmq.common.message.MessageExt 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 8 with MessageExt

use of com.alibaba.rocketmq.common.message.MessageExt in project incubator-skywalking by apache.

the class AbstractMessageConsumeInterceptor method beforeMethod.

@Override
public final void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
    List<MessageExt> msgs = (List<MessageExt>) allArguments[0];
    ContextCarrier contextCarrier = getContextCarrierFromMessage(msgs.get(0));
    AbstractSpan span = ContextManager.createEntrySpan(CONSUMER_OPERATION_NAME_PREFIX + msgs.get(0).getTopic() + "/Consumer", contextCarrier);
    span.setComponent(ComponentsDefine.ROCKET_MQ);
    span.setLayer(SpanLayer.MQ);
    for (int i = 1; i < msgs.size(); i++) {
        ContextManager.extract(getContextCarrierFromMessage(msgs.get(i)));
    }
}
Also used : MessageExt(com.alibaba.rocketmq.common.message.MessageExt) ContextCarrier(org.apache.skywalking.apm.agent.core.context.ContextCarrier) List(java.util.List) AbstractSpan(org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)

Example 9 with MessageExt

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

the class MetaSimpleClient method fetchOneBatch.

public List<MessageExt> fetchOneBatch() {
    List<MessageExt> ret = new ArrayList<MessageExt>();
    String subexpress = metaSpoutConfig.getSubExpress();
    for (Entry<MessageQueue, Long> entry : currentOffsets.entrySet()) {
        MessageQueue mq = entry.getKey();
        Long offset = entry.getValue();
        int fetchSize = 0;
        int oneFetchSize = Math.min(oneQueueFetchSize, 32);
        while (fetchSize < oneQueueFetchSize) {
            PullResult pullResult = null;
            try {
                pullResult = consumer.pullBlockIfNotFound(mq, subexpress, offset, oneFetchSize);
                offset = pullResult.getNextBeginOffset();
                PullStatus status = pullResult.getPullStatus();
                if (status == PullStatus.FOUND) {
                    List<MessageExt> msgList = pullResult.getMsgFoundList();
                    ret.addAll(msgList);
                    fetchSize += msgList.size();
                    continue;
                } else if (status == PullStatus.NO_MATCHED_MSG) {
                    continue;
                } else if (status == PullStatus.NO_NEW_MSG) {
                    break;
                } else if (status == PullStatus.OFFSET_ILLEGAL) {
                    break;
                } else {
                    break;
                }
            } catch (Exception e) {
                LOG.warn("Failed to fetch messages of " + mq + ":" + offset, e);
                break;
            }
        }
        backendOffset.put(mq, offset);
    }
    return ret;
}
Also used : MessageExt(com.alibaba.rocketmq.common.message.MessageExt) MessageQueue(com.alibaba.rocketmq.common.message.MessageQueue) ArrayList(java.util.ArrayList) PullStatus(com.alibaba.rocketmq.client.consumer.PullStatus) PullResult(com.alibaba.rocketmq.client.consumer.PullResult) FailedException(backtype.storm.topology.FailedException) MQClientException(com.alibaba.rocketmq.client.exception.MQClientException)

Example 10 with MessageExt

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

the class TestProcess method TestPull.

@Test
public void TestPull() throws MQClientException {
    // "192.168.1.44:9876";
    final String address = "192.168.1.45:9876";
    final String topic = "topicTest";
    final String consumerGroup = "cgr";
    final String subExpression = "*";
    PullConsumer consumer = new PullConsumer(address, consumerGroup);
    Set<MessageQueue> mqs = consumer.getMessageQueue(topic);
    for (MessageQueue mq : mqs) {
        System.out.println("Consume from the queue: " + mq);
        SINGLE_MQ: while (true) {
            try {
                PullResult pullResult = consumer.pullBlockIfNotFound(mq, subExpression, getMessageQueueOffset(mq), 32);
                System.out.println(pullResult);
                putMessageQueueOffset(mq, pullResult.getNextBeginOffset());
                switch(pullResult.getPullStatus()) {
                    case FOUND:
                        List<MessageExt> msgs = pullResult.getMsgFoundList();
                        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 + "\n");
                        }
                        break;
                    case NO_MATCHED_MSG:
                        break;
                    case NO_NEW_MSG:
                        break SINGLE_MQ;
                    case OFFSET_ILLEGAL:
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    for (Map.Entry<MessageQueue, Long> one : offseTable.entrySet()) {
        System.out.println(one.getValue());
    }
}
Also used : MessageExt(com.alibaba.rocketmq.common.message.MessageExt) MessageQueue(com.alibaba.rocketmq.common.message.MessageQueue) List(java.util.List) PullConsumer(cn.tellwhy.third.rocket.PullConsumer) HashMap(java.util.HashMap) Map(java.util.Map) PullResult(com.alibaba.rocketmq.client.consumer.PullResult) MQBrokerException(com.alibaba.rocketmq.client.exception.MQBrokerException) RemotingException(com.alibaba.rocketmq.remoting.exception.RemotingException) MQClientException(com.alibaba.rocketmq.client.exception.MQClientException) Test(org.junit.Test)

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