Springboot timed task call

Compared with the spring framework, spring boot makes it easy to create a scheduled task through simplified configuration. By opening the corresponding annotation, you can perform the operation of the corresponding timed task.

1. Add the annotation @EnableScheuling to enable timing tasks in the startup class of spring boot

@SpringBootApplication
@Component
@ComponentScan
@EnableScheduling
public class ServerApplication {
    private static final Logger logger = LoggerFactory.getLogger("adminLogger");

	public static void main(String[] args) {
	   SpringApplication app = new SpringApplication(ServerApplication.class);  
       app.addListeners(new StartApplicationListener());  
       app.run(args);  
	}
    @Scheduled(cron = "20 46 11 * * ?" )
	public void timerToNow(){
	    AditionService aditionService = (AditionService)SpringUtil.getBean(AditionService.class);
		BaseResponse res = aditionService.timerToNow ();
		logger.info(res.toString());
	}
}

2. Define the annotation @Scheduled for scheduled tasks

Explain the parameters in this annotation here. The values ​​represented by each parameter are: seconds, minutes, hours, days, months, and weeks.

So what I define here is to execute a scheduled task at 11:46:20. Define your logical processing method in the public void timerToNow(){} method. I need to get the data one month ago at the defined time and delete the data in the corresponding table.

@Component
public class AditionService {

    @Autowired
    private PayAcceptOrderOldMapper acceptOrderOldMapper;
    
    @Autowired
    private PayAcceptOrderMapper acceptOrderMapper;

	@Transactional
	public BaseResponse timerToNow () {
		PayAcceptOrderOld payAcceptOrderOld =new PayAcceptOrderOld();
		int flag =acceptOrderOldMapper.insertAddition(payAcceptOrderOld);
		int delfalg = acceptOrderMapper.deleteMonthData ();
		if (flag == delfalg) {
			System.out.println("adminadmin");
			return BaseResponse.success ();
		}else{
			throw new RuntimeException("Failed to add business information");
		}
	}
}

It should be noted here that if you are operating with multiple tables, be sure to add transaction annotations and throw corresponding exceptions when errors occur.

Secondly, I want to say that the data in the first 30 days of querying one table data is added to another table, and the data in the corresponding table is deleted at the same time. In fact, it is the execution process of my code above.

INSERT INTO pay_accept_order_old SELECT
     t.*, getdate()
	 FROM
	 pay_accept_order t
	 WHERE
	 t.last_updated_time <= dateadd(day,-30, getdate())

Query the data in the order table 30 days ago and insert it into the orderold table. Due to the relationship of mybatis, the less than sign I directly escaped the character instead.

<delete id="deleteMonthData">
     delete from pay_accept_order WHERE last_updated_time <= dateadd(day,-30, getdate())
  </delete>

Delete the corresponding data 30 days ago, and judge by comparing the number of data operations between the two. If unsuccessful, the transaction will be rolled back.

Compared with the complicated configuration, springboot achieves the same result with a small amount of configuration.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324782792&siteId=291194637