应用适配器模式---拆分同步事务为异步事务的一种尝试

业务场景:

位于service层的业务service类中的一个具体的方法:

private void addMyBussiness(String ii,SchoolBean schoolBean ,ClassBean classBean, TeachBean teachInfo) {

 // 添加班级(包括年级、学期,班级空间) + 添加群组 以及 本地的群组
 schoolService.addMyClass(classBean);

}


其中 addMyClass方法中除了对数据库操作(select,insert等)多以外,还根据操作的结果发起了第三方业务请求(http请求)执行接口的调用,
这样一来,这个事务方法未免就显得太庞大了,尤其是在当前事务会话期间,假如上一语句执行了insert into的操作(此时当前会话将持有这个锁,直到事务提交)
紧接着又发起额外的http请求,如果http请求及时响应,那么通常情况下不会暴露问题;如果http响应超过了锁等待超时的50毫秒,那么mysql就会报锁等待超时错误
。这种情况尤其在接口被同时大量调用执行时发生的频率就会提高,从而影响数据的完整性。
解决的办法是,拆分这个事务方法,将同步等待的操作变为异步执行。比如这个方法中,我们把“发起了第三方业务请求”这一系列操作放到事务提交之后去执行,
只有前一步骤操作成功才会发请求,即使这个请求后续执行失败没有回滚上一步也没关系,因为在目前这个业务场景中,这一步的数据不是非常紧要的操作,后续还有
批处理程序会批量完成,并保证记录日志即可。


改造:增加一个同步事务的适配器,在该适配器中根据传入的不同参数调用响应的线程去完成第三方业务的http请求.
实例代码如下:

     

  boolean addResult = schoolService.addMyClass(classBean);
if (addResult){

OtherSynchronizationAdapter otherSynchronizationAdapter = new OtherSynchronizationAdapter(otherInterfaceService,ii,
OtherInterfaceService.TAB_TEACHER,classBean);
TransactionSynchronizationManager.registerSynchronization(otherSynchronizationAdapter);

}// if addResult 



实例代码:



/**
 * 第三方请求事务拆分适配器
 *
 */
public class OtherSynchronizationAdapter extends TransactionSynchronizationAdapter {


/**第三方服务,由外部传入*/
private OtherInterfaceService otherInterfaceService;
/**第三方群组的添加类型  参见  <BR>1:OtherInterfaceService.TAB_STUDENT <BR> 2:OtherInterfaceService.TAB_TEACHER <BR>3: OtherInterfaceService.TAB_FAMILY */
private int tab;
/**调用者传入的classBean*/
private ClassBean classBean;
/**调用者传入的学号*/
private String ii;
/**调用者传入的家长号码*/
private String familyNumber;


public OtherSynchronizationAdapter(OtherInterfaceService otherInterfaceService,String ii,int tab,ClassBean classBean) {
super();
this.tab = tab;
this.classBean = classBean;
this.ii = ii;
this.otherInterfaceService = otherInterfaceService;

}

public OtherSynchronizationAdapter(OtherInterfaceService otherInterfaceService,String ii,int tab,ClassBean classBean,String familyIiNumber) {
super();
this.tab = tab;
this.classBean = classBean;
this.ii = ii;
this.otherInterfaceService = otherInterfaceService;
this.familyIiNumber =familyIiNumber;
}
@Override
public void afterCommit(){

if  (tab==OtherInterfaceService.TAB_TEACHER){
//创建一个发起请求的线程
OtherInterfaceTeacherRunable otherInterfaceRunable = new OtherInterfaceTeacherRunable(otherInterfaceService,ii,
classBean);
OtherInterfaceService.execOtherPool.submit(otherInterfaceRunable);//将线程扔到线程池
}//
if  (tab==OtherInterfaceService.TAB_STUDENT){
OtherInterfaceStudentRunable otherInterfaceRunable = null;
if (familyIiNumber!=null){
  otherInterfaceRunable = new OtherInterfaceStudentRunable(otherInterfaceService,ii,classBean,familyIiNumber);
}else{
  otherInterfaceRunable = new OtherInterfaceStudentRunable(otherInterfaceService,ii,classBean);// 
}
OtherInterfaceService.execOtherPool.submit(otherInterfaceRunable);
}//
}//end of afterCommit
public int getTab() {
return tab;
}
public void setTab(int tab) {
this.tab = tab;
}
public ClassBean getC2sClassBean() {
return classBean;
}
public void setC2sClassBean(ClassBean classBean) {
this.classBean = classBean;
}
public String getIi() {
return ii;
}
public void setIi(String ii) {
this.ii = ii;
}
public OtherInterfaceService getOtherInterfaceService() {
return otherInterfaceService;
}
public void setOtherInterfaceService(OtherInterfaceService otherInterfaceService) {
this.otherInterfaceService = otherInterfaceService;
}
}

 发起请求的线程类

/**
 * 发起请求的线程 for 教师
 *
 */
public class OtherInterfaceTeacherRunable implements Runnable {

private static Logger logger = Logger.getLogger(OtherInterfaceTeacherRunable.class);
        /**第三方服务,由外部传入*/
private OtherInterfaceService otherInterfaceService;
/**调用者传入的classBean*/
private ClassBean classBean;
/**调用者传入的ii号*/
private String ii;




/**
* 构造方法
* @param otherInterfaceService
* @param ii
* @param classBean
*/
public OtherInterfaceTeacherRunable(OtherInterfaceService otherInterfaceService,String ii,ClassBean classBean) {
super();
this.otherInterfaceService = otherInterfaceService;
this.classBean = classBean;
this.ii = ii;
}


@Override
public void run() {
 
  if (classBean==null){
  //System.out.println("[classBean,ii] cannot be null ...");
  logger.debug("[classBean,ii] cannot be null ...");
  return;
  }
 
 
try {
_handle();

} catch (Exception e1) {
e1.printStackTrace();
}
   
try {
                 Thread.sleep(50);
      } catch (InterruptedException e) {
                  e.printStackTrace();
     }
}




private void _handle() {

boolean addGroupResult = otherInterfaceService.addGroup(classBean.getClassid(),classBean.getClassid(), OtherInterfaceService.TAB_TEACHER, classBean.getGradenum() + classBean.getClassname());

if (addGroupResult){
// 第三方:添加 
otherInterfaceService.addUserToGroup(ii,classBean.getClassid(), OtherInterfaceService.TAB_TEACHER, classBean.getClassname());

}
}

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安优小青和他的程序生活

我的文档对您有很大的帮助吗?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值