Java Code Examples for org.apache.rocketmq.client.hook.ConsumeMessageContext#getMsgList()

The following examples show how to use org.apache.rocketmq.client.hook.ConsumeMessageContext#getMsgList() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OnsConsumeMessageHookImpl.java    From rocketmq-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
@Override
public void consumeMessageAfter(ConsumeMessageContext context) {
    if (context == null || context.getMsgList() == null || context.getMsgList().isEmpty()) {
        return;
    }
    OnsTraceContext subBeforeContext = (OnsTraceContext) context.getMqTraceContext();
    OnsTraceContext subAfterContext = new OnsTraceContext();
    subAfterContext.setTraceType(OnsTraceType.SubAfter);//
    subAfterContext.setRegionId(subBeforeContext.getRegionId());//
    subAfterContext.setGroupName(subBeforeContext.getGroupName());//
    subAfterContext.setRequestId(subBeforeContext.getRequestId());//
    subAfterContext.setSuccess(context.isSuccess());//
    // 批量消息全部处理完毕的平均耗时
    int costTime = (int) ((System.currentTimeMillis() - subBeforeContext.getTimeStamp()) / context.getMsgList().size());
    subAfterContext.setCostTime(costTime);//
    subAfterContext.setTraceBeans(subBeforeContext.getTraceBeans());
    localDispatcher.append(subAfterContext);
}
 
Example 2
Source File: OnsConsumeMessageHookImpl.java    From rocketmq-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@Override
    public void consumeMessageBefore(ConsumeMessageContext context) {
        if (context == null || context.getMsgList() == null || context.getMsgList().isEmpty()) {
            return;
        }
        OnsTraceContext onsTraceContext = new OnsTraceContext();
        context.setMqTraceContext(onsTraceContext);
        onsTraceContext.setTraceType(OnsTraceType.SubBefore);//
        onsTraceContext.setGroupName(context.getConsumerGroup());//
        List<OnsTraceBean> beans = new ArrayList<OnsTraceBean>();
        for (MessageExt msg : context.getMsgList()) {
            if (msg == null) {
                continue;
            }
            OnsTraceBean traceBean = new OnsTraceBean();
            traceBean.setTopic(msg.getTopic());//
            traceBean.setMsgId(msg.getMsgId());//
            traceBean.setTags(msg.getTags());//
            traceBean.setKeys(msg.getKeys());//
//            InetSocketAddress host;
//            // host = (InetSocketAddress) msg.getBornHost();
//            // traceBean.setClientHost(host.getHostName());//
//
//            host = (InetSocketAddress) msg.getStoreHost();
//            traceBean.setStoreHost(host.getHostName());//
            traceBean.setStoreTime(msg.getStoreTimestamp());//
            traceBean.setBodyLength(msg.getStoreSize());//
            traceBean.setRetryTimes(msg.getReconsumeTimes());//
            beans.add(traceBean);
        }
        onsTraceContext.setTraceBeans(beans);
        onsTraceContext.setTimeStamp(System.currentTimeMillis());
        localDispatcher.append(onsTraceContext);
    }
 
Example 3
Source File: ConsumeMessageTraceHookImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public void consumeMessageBefore(ConsumeMessageContext context) {
    if (context == null || context.getMsgList() == null || context.getMsgList().isEmpty()) {
        return;
    }
    TraceContext traceContext = new TraceContext();
    context.setMqTraceContext(traceContext);
    traceContext.setTraceType(TraceType.SubBefore);//
    traceContext.setGroupName(NamespaceUtil.withoutNamespace(context.getConsumerGroup()));//
    List<TraceBean> beans = new ArrayList<TraceBean>();
    for (MessageExt msg : context.getMsgList()) {
        if (msg == null) {
            continue;
        }
        String regionId = msg.getProperty(MessageConst.PROPERTY_MSG_REGION);
        String traceOn = msg.getProperty(MessageConst.PROPERTY_TRACE_SWITCH);

        if (traceOn != null && traceOn.equals("false")) {
            // If trace switch is false ,skip it
            continue;
        }
        TraceBean traceBean = new TraceBean();
        traceBean.setTopic(NamespaceUtil.withoutNamespace(msg.getTopic()));//
        traceBean.setMsgId(msg.getMsgId());//
        traceBean.setTags(msg.getTags());//
        traceBean.setKeys(msg.getKeys());//
        traceBean.setStoreTime(msg.getStoreTimestamp());//
        traceBean.setBodyLength(msg.getStoreSize());//
        traceBean.setRetryTimes(msg.getReconsumeTimes());//
        traceContext.setRegionId(regionId);//
        beans.add(traceBean);
    }
    if (beans.size() > 0) {
        traceContext.setTraceBeans(beans);
        traceContext.setTimeStamp(System.currentTimeMillis());
        localDispatcher.append(traceContext);
    }
}
 
Example 4
Source File: ConsumeMessageTraceHookImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public void consumeMessageAfter(ConsumeMessageContext context) {
    if (context == null || context.getMsgList() == null || context.getMsgList().isEmpty()) {
        return;
    }
    TraceContext subBeforeContext = (TraceContext) context.getMqTraceContext();

    if (subBeforeContext.getTraceBeans() == null || subBeforeContext.getTraceBeans().size() < 1) {
        // If subbefore bean is null ,skip it
        return;
    }
    TraceContext subAfterContext = new TraceContext();
    subAfterContext.setTraceType(TraceType.SubAfter);//
    subAfterContext.setRegionId(subBeforeContext.getRegionId());//
    subAfterContext.setGroupName(NamespaceUtil.withoutNamespace(subBeforeContext.getGroupName()));//
    subAfterContext.setRequestId(subBeforeContext.getRequestId());//
    subAfterContext.setSuccess(context.isSuccess());//

    // Caculate the cost time for processing messages
    int costTime = (int) ((System.currentTimeMillis() - subBeforeContext.getTimeStamp()) / context.getMsgList().size());
    subAfterContext.setCostTime(costTime);//
    subAfterContext.setTraceBeans(subBeforeContext.getTraceBeans());
    String contextType = context.getProps().get(MixAll.CONSUME_CONTEXT_TYPE);
    if (contextType != null) {
        subAfterContext.setContextCode(ConsumeReturnType.valueOf(contextType).ordinal());
    }
    localDispatcher.append(subAfterContext);
}