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.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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -40,44 +40,33 @@ public class SeataHandlerInterceptor implements HandlerInterceptor {
|
|||||||
private static final Logger log = LoggerFactory
|
private static final Logger log = LoggerFactory
|
||||||
.getLogger(SeataHandlerInterceptor.class);
|
.getLogger(SeataHandlerInterceptor.class);
|
||||||
|
|
||||||
private static final String BIND_XID = "BIND_XID";
|
|
||||||
|
|
||||||
private static final String UNBIND_XID = "UNBIND_XID";
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||||
Object handler) {
|
|
||||||
Object xidStatus = request.getAttribute(BIND_XID);
|
|
||||||
if (null == xidStatus || !(boolean)xidStatus) {
|
|
||||||
String xid = RootContext.getXID();
|
String xid = RootContext.getXID();
|
||||||
String rpcXid = request.getHeader(RootContext.KEY_XID);
|
String rpcXid = request.getHeader(RootContext.KEY_XID);
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("xid in RootContext {} xid in RpcContext {}", xid, rpcXid);
|
log.debug("xid in RootContext {} xid in RpcContext {}", xid, rpcXid);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (xid == null && rpcXid != null) {
|
if (StringUtils.isBlank(xid) && rpcXid != null) {
|
||||||
RootContext.bind(rpcXid);
|
RootContext.bind(rpcXid);
|
||||||
request.setAttribute(BIND_XID, true);
|
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
log.debug("bind {} to RootContext", rpcXid);
|
log.debug("bind {} to RootContext", rpcXid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
|
||||||
Object handler, Exception e) {
|
if (StringUtils.isNotBlank(RootContext.getXID())) {
|
||||||
|
|
||||||
String rpcXid = request.getHeader(RootContext.KEY_XID);
|
String rpcXid = request.getHeader(RootContext.KEY_XID);
|
||||||
|
|
||||||
if (StringUtils.isEmpty(rpcXid)) {
|
if (StringUtils.isEmpty(rpcXid)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Object xidStatus = request.getAttribute(UNBIND_XID);
|
|
||||||
if (null == xidStatus || !(boolean)xidStatus) {
|
|
||||||
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);
|
||||||
@ -86,7 +75,6 @@ public class SeataHandlerInterceptor implements HandlerInterceptor {
|
|||||||
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);
|
||||||
request.setAttribute(UNBIND_XID, true);
|
|
||||||
log.warn("bind {} back to RootContext", unbindXid);
|
log.warn("bind {} back to RootContext", unbindXid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user