Skip to content

Commit

Permalink
consumer调用前后,绑定与解绑LogProcessContext (#122)
Browse files Browse the repository at this point in the history
thanks ,relates to #121
  • Loading branch information
LemonCoo1 authored and skyesx committed May 14, 2019
1 parent 74d9dd8 commit f6334e2
Showing 1 changed file with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class RemoteServiceCaller {
private EasyTransMsgPublisher publisher;
private ObjectSerializer serializer;
private QueueTopicMapper queueTopicMapper;

private static final ThreadLocal<LogProcessContext> logProcessContextThreadLocal = new ThreadLocal<>();


public RemoteServiceCaller(EasyTransRpcConsumer consumer, EasyTransMsgPublisher publisher,
Expand All @@ -30,19 +32,34 @@ public RemoteServiceCaller(EasyTransRpcConsumer consumer, EasyTransMsgPublisher
}

public <P extends EasyTransRequest<R,?>,R extends Serializable> R call(String appId,String busCode, Integer callSeq,String innerMethod,P params,LogProcessContext logContext){
return consumer.call(appId, busCode, innerMethod,initEasyTransRequestHeader(callSeq,logContext), params);
try {
logProcessContextThreadLocal.set(logContext);
return consumer.call(appId, busCode, innerMethod,initEasyTransRequestHeader(callSeq,logContext), params);
}finally {
logProcessContextThreadLocal.remove();
}

}

public <P extends EasyTransRequest<R,?>,R extends Serializable> void callWithNoReturn(String appId,String busCode, Integer callSeq, String innerMethod,P params,LogProcessContext logContext){
consumer.callWithNoReturn(appId, busCode, innerMethod,initEasyTransRequestHeader(callSeq,logContext), params);
try {
logProcessContextThreadLocal.set(logContext);
consumer.callWithNoReturn(appId, busCode, innerMethod,initEasyTransRequestHeader(callSeq,logContext), params);
} finally {
logProcessContextThreadLocal.remove();
}
}


public EasyTransMsgPublishResult publish(String appid, String busCode, Integer callSeq, String key, EasyTransRequest<?, ?> request,LogProcessContext logContext){

String[] topicTag = queueTopicMapper.mapToTopicTag(appid, busCode);

return publisher.publish(topicTag[0], topicTag[1], key, initEasyTransRequestHeader(callSeq,logContext) ,serializer.serialization(request));
try{
logProcessContextThreadLocal.set(logContext);
String[] topicTag = queueTopicMapper.mapToTopicTag(appid, busCode);
return publisher.publish(topicTag[0], topicTag[1], key, initEasyTransRequestHeader(callSeq,logContext) ,serializer.serialization(request));
}finally {
logProcessContextThreadLocal.remove();
}

}

private Map<String,Object> initEasyTransRequestHeader(Integer callSeq, LogProcessContext logContext){
Expand All @@ -51,4 +68,8 @@ private Map<String,Object> initEasyTransRequestHeader(Integer callSeq, LogProces
header.put(EasytransConstant.CallHeadKeys.CALL_SEQ, callSeq);
return header;
}

public static LogProcessContext getCurrentLogProcessContext() {
return logProcessContextThreadLocal.get();
}
}

0 comments on commit f6334e2

Please sign in to comment.