websphere部署web项目_web项目多机部署之定时任务方案及源码

1.场景描述

老项目需要多机部署,项目中有几十个定时任务,一旦多机部署,定时任务就会重复执行,固定ip与错开时间方案都存在较大弊端,最终采用的方案是:AOP+排他锁的方式,软件老王已验证通过,介绍下,有需要的朋友可以参考下。

2.解决方案

软件老王基本方案是采用:AOP+排他锁的方式。

(1)目前老项目有几十个定时任务,采用AOP的方式,可以保证代码的无侵入(即使简单的微侵入,例如增加几行代码,测试验证的工作量也会比较大的)。
(2)采用排他锁的方式,保证批处理的高可用,不重复执行。

2.1 AOP编程

Aop的概念就不说了,就是面向切面编程,通俗点就是统一处理一类问题,比如日志、请求鉴权等,刚开始不确定是否可行,系统中的批处理是使用spring注解的方式@Scheduled进行批处理,采用aop对注解@Scheduled进行编程,统一拦截批处理,代码如下:

/** * 软件老王-AOP处理类 */@Aspect@Componentpublic class ScheduledAspect {    @Autowired    ScheduleService scheduleService ;    @Pointcut( "@annotation(org.springframework.scheduling.annotation.Scheduled)")    public void scheduled() {    }    @Around("scheduled()")    public Object scheduled(ProceedingJoinPoint pjd) {        Object result = null;        String taskName = pjd.getSignature().getName();        try {            if (scheduleService.isInvoke(taskName)){                return result;            }            result = pjd.proceed();            scheduleService.end(taskName);        } catch (Throwable e) {            throw new RuntimeException(e);        }        return result;    }}

说明:

(1)面向标签编程

  @Pointcut( "@annotation(org.springframework.scheduling.annotation.Scheduled)")

这样注解会拦截标签@Scheduled。

(2)使用aop的环绕标签 @Around("scheduled()")

@before标签拿不到执行完成状态,需要使用环绕标签@@Around,在标签中可以拿到执行完成后状态,以便放开锁。

 result = pjd.proceed();

(3)结合排他锁使用

   @Autowired    ScheduleService scheduleService ;

2.2 排他锁

排他锁,简单来说就是通过数据库总的标志位+版版号进行的控制.

软件老王的代码如下,:

/** * 软件老王-排他锁服务类 */@Servicepublic class ScheduleService {    @Autowired    ScheduleClusterMapper scheduleClusterMapper;    public boolean isInvoke(String taskName) {        boolean isValid = false;        try {            ScheduleCluster carIndexEntity = scheduleClusterMapper.selectByTaskName(taskName);            int execute = carIndexEntity.getExecute();            String ip = InetAddress.getLocalHost().getHostAddress();            long currentTimeMillis = System.currentTimeMillis();            long time = carIndexEntity.getUpdatedate().getTime();            if (execute == 0) {                isValid = start(taskName, carIndexEntity.getVersion(), ip);            }        } catch (UnknownHostException e) {            e.printStackTrace();        }        return isValid;    }    //执行锁机制,软件老王    public boolean start(String taskName, int version, String ip) {        ScheduleCluster scheduleCluster = new ScheduleCluster();        scheduleCluster.setVersion(version);        scheduleCluster.setExecuteIp(ip);        scheduleCluster.setUpdatedate(DateUtil.getCurrentTime());        scheduleCluster.setTaskName(taskName);        scheduleCluster.setExecute(1);        int count = scheduleClusterMapper.updateByTaskName(scheduleCluster);        if (count > 0) {            return true;        }        return false;    }    //执行解锁机制,软件老王    public void end(String taskName) {        ScheduleCluster scheduleCluster = new ScheduleCluster();        scheduleCluster.setUpdatedate(DateUtil.getCurrentTime());        scheduleCluster.setTaskName(taskName);        scheduleCluster.setExecute(0);        scheduleClusterMapper.updateNormalByTaskName(scheduleCluster);    }}

说明:

大的原理是在where条件后带上版本号,在update中更新version+1,这样通过影响数据库的影响条数,来判断是否拿到锁。

(1)主类中调用start方法,该方法是更新批处理状态,软件老王这里设置了一个小点,在updateByTaskName的mybatis方法中,有个version+1的更新;

(2)end方法放在更新完成后,释放锁。

(3)其实还有一个点,可以考虑下,需要有个机制,比如出现异常情况,刚好批处理执行中,重启服务了等,下次批处理执行前,假如锁还未释放,代码中增加释放锁的机制。

2.3 数据库相关

(1)数据库表设计

c0518f0e8c9d1dc2c923b1ff1f6584da.png

(2)mybatis相关方法

(1)第一个是start对应方法,执行锁和version增加。

     update t_schedule_cluster                execute_ip = #{executeIp,jdbcType=VARCHAR},                    version = #{version,jdbcType=INTEGER} + 1,                    execute = #{execute,jdbcType=INTEGER},                    status = #{status,jdbcType=VARCHAR},                    createby = #{createby,jdbcType=VARCHAR},                    createdate = #{createdate,jdbcType=TIMESTAMP},                    updateby = #{updateby,jdbcType=VARCHAR},                    updatedate = #{updatedate,jdbcType=TIMESTAMP},            where task_name = #{taskName,jdbcType=VARCHAR}    and version = #{version,jdbcType=INTEGER}    and status ='1'  

(2)第二个是释放锁,更改excute为0。

    update t_schedule_cluster                execute_ip = #{executeIp,jdbcType=VARCHAR},                    version = #{version,jdbcType=INTEGER},                    execute = #{execute,jdbcType=INTEGER},                    status = #{status,jdbcType=VARCHAR},                    createby = #{createby,jdbcType=VARCHAR},                    createdate = #{createdate,jdbcType=TIMESTAMP},                    updateby = #{updateby,jdbcType=VARCHAR},                    updatedate = #{updatedate,jdbcType=TIMESTAMP},            where task_name = #{taskName,jdbcType=VARCHAR}    and status ='1'

更多信息请关注@软件老王,关注不迷路,软件老王和他的IT朋友们,分享一些他们的技术见解和生活故事。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值