mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
optimize code
This commit is contained in:
parent
8fa763f7d6
commit
29cbc0bfbe
@ -19,11 +19,11 @@ package com.alibaba.cloud.seata.web;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import io.seata.common.util.StringUtils;
|
||||
import io.seata.core.context.RootContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
/**
|
||||
@ -40,44 +40,33 @@ public class SeataHandlerInterceptor implements HandlerInterceptor {
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(SeataHandlerInterceptor.class);
|
||||
|
||||
private static final String BIND_XID = "BIND_XID";
|
||||
|
||||
private static final String UNBIND_XID = "UNBIND_XID";
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
|
||||
Object handler) {
|
||||
Object xidStatus = request.getAttribute(BIND_XID);
|
||||
if (null == xidStatus || !(boolean)xidStatus) {
|
||||
String xid = RootContext.getXID();
|
||||
String rpcXid = request.getHeader(RootContext.KEY_XID);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("xid in RootContext {} xid in RpcContext {}", xid, rpcXid);
|
||||
}
|
||||
|
||||
if (xid == null && rpcXid != null) {
|
||||
RootContext.bind(rpcXid);
|
||||
request.setAttribute(BIND_XID, true);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("bind {} to RootContext", rpcXid);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
|
||||
Object handler, Exception e) {
|
||||
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||
String xid = RootContext.getXID();
|
||||
String rpcXid = request.getHeader(RootContext.KEY_XID);
|
||||
|
||||
if (StringUtils.isEmpty(rpcXid)) {
|
||||
return;
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("xid in RootContext {} xid in RpcContext {}", xid, rpcXid);
|
||||
}
|
||||
|
||||
Object xidStatus = request.getAttribute(UNBIND_XID);
|
||||
if (null == xidStatus || !(boolean)xidStatus) {
|
||||
if (StringUtils.isBlank(xid) && rpcXid != null) {
|
||||
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) {
|
||||
if (StringUtils.isNotBlank(RootContext.getXID())) {
|
||||
String rpcXid = request.getHeader(RootContext.KEY_XID);
|
||||
|
||||
if (StringUtils.isEmpty(rpcXid)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String unbindXid = RootContext.unbind();
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("unbind {} from RootContext", unbindXid);
|
||||
@ -86,7 +75,6 @@ public class SeataHandlerInterceptor implements HandlerInterceptor {
|
||||
log.warn("xid in change during RPC from {} to {}", rpcXid, unbindXid);
|
||||
if (unbindXid != null) {
|
||||
RootContext.bind(unbindXid);
|
||||
request.setAttribute(UNBIND_XID, true);
|
||||
log.warn("bind {} back to RootContext", unbindXid);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user