• notice
  • Congratulations on the launch of the Sought Tech site

SpringBoot uses Timer to execute tasks after 2 hours of the specified time

/**
  * @Description
  * @Author songwp
  * @Date 2022/8/5 12:51
  * @Version 1.0.0
  **/
@Component
public class SalaryDetailTask {

     protected Logger log = LoggerFactory. getLogger(SalaryDetailTask. class);

     @Autowired
     protectedISalaryDetailServiceExtendsalaryDetailServiceExtend;

     /**
      * @param uploadId
      * @Date 2022/8/5 13:51
      * @Author songwp
      */
     public void TaskServiceImplExtend(String uploadId) {
         Calendar myDate = Calendar. getInstance();
         try {
             // Get salary details
             SalaryDetail salaryDetail = salaryDetailServiceExtend.lambdaQuery().eq(SalaryDetail::getRefUploadRecordId, uploadId).eq(SalaryDetail::getSendStatus, 1).list().get(0);
             // get sending time
             myDate.setTime(salaryDetail.getSendTime());
             // Task execution time (2 hours after sending time)
             myDate.add(Calendar.HOUR, 2);
             Date afterOneMinute = myDate. getTime();
             log.info("Scheduled (executor) at: afterOneMinute {}",afterOneMinute);
             Timer timer = new Timer();
             timer.schedule(new TimerTask() {
                 @Override
                 public void run() {
                     salaryDetailServiceExtend. lambdaUpdate()
                             .set(SalaryDetail::getConfirmStatus,1)
                             .eq(SalaryDetail::getConfirmStatus,0)
                             .eq(SalaryDetail::getRefUploadRecordId, uploadId)
                             .eq(SalaryDetail::getSendStatus,1)
                             .update();
                     log.info("The upload record id: uploadId: {}--The confirmation status of the salary slip information under it is updated from startStatus: {} --> endStatus: {} success!", uploadId, salaryDetail.getConfirmStatus( ),1);
                 }
             }, afterOneMinute);
         } catch (Exception e) {
             log.error("According to uploadId: {}, the salary details that were successfully pushed are not matched, and the task execution is abnormal!", uploadId, e);
         }
     }
}


Tags

Technical otaku

Sought technology together

Related Topic

1 Comments

author

buy atorvastatin sale & lt;a href="https://lipiws.top/"& gt;buy atorvastatin online& lt;/a& gt; purchase atorvastatin pills

Pkgbyp

2024-03-07

Leave a Reply

+