1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

Merge pull request #1 from alibaba/master

update
This commit is contained in:
余黄彬 2020-09-09 17:33:17 +08:00 committed by GitHub
commit 5478390f88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,11 +19,11 @@ package com.alibaba.cloud.seata.web;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import io.seata.common.util.StringUtils;
import io.seata.core.context.RootContext; import io.seata.core.context.RootContext;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.HandlerInterceptor;
/** /**
@ -41,45 +41,45 @@ public class SeataHandlerInterceptor implements HandlerInterceptor {
.getLogger(SeataHandlerInterceptor.class); .getLogger(SeataHandlerInterceptor.class);
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
Object handler) { String xid = RootContext.getXID();
String rpcXid = request.getHeader(RootContext.KEY_XID);
if (log.isDebugEnabled()) {
log.debug("xid in RootContext {} xid in RpcContext {}", xid, rpcXid);
}
String xid = RootContext.getXID(); if (StringUtils.isBlank(xid) && rpcXid != null) {
String rpcXid = request.getHeader(RootContext.KEY_XID); RootContext.bind(rpcXid);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("xid in RootContext {} xid in RpcContext {}", xid, rpcXid); log.debug("bind {} to RootContext", rpcXid);
} }
}
if (xid == null && rpcXid != null) { return true;
RootContext.bind(rpcXid); }
if (log.isDebugEnabled()) {
log.debug("bind {} to RootContext", rpcXid);
}
}
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception e) {
String rpcXid = request.getHeader(RootContext.KEY_XID); @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
if (StringUtils.isNotBlank(RootContext.getXID())) {
String rpcXid = request.getHeader(RootContext.KEY_XID);
if (StringUtils.isEmpty(rpcXid)) { if (StringUtils.isEmpty(rpcXid)) {
return; return;
} }
String unbindXid = RootContext.unbind(); String unbindXid = RootContext.unbind();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("unbind {} from RootContext", unbindXid); log.debug("unbind {} from RootContext", unbindXid);
} }
if (!rpcXid.equalsIgnoreCase(unbindXid)) { if (!rpcXid.equalsIgnoreCase(unbindXid)) {
log.warn("xid in change during RPC from {} to {}", rpcXid, unbindXid); log.warn("xid in change during RPC from {} to {}", rpcXid, unbindXid);
if (unbindXid != null) { if (unbindXid != null) {
RootContext.bind(unbindXid); RootContext.bind(unbindXid);
log.warn("bind {} back to RootContext", unbindXid); log.warn("bind {} back to RootContext", unbindXid);
} }
} }
} }
}
} }