Spring scheduled task configuration and Spring task Configuration

Source: Internet
Author: User

Spring scheduled task configuration and Spring task Configuration

1. The declaration and xsd for adding tasks to applicationContext. xml

1 xmlns:task="http://www.springframework.org/schema/task"
1 http://www.springframework.org/schema/task  http://www.springframework.org/schema/task/spring-task-4.0.xsd

Add in configuration

1 <task:annotation-driven/>

This is used to enable automatic annotation parsing.

 

2. Compile POJO

12345678910111213141516171819202122 @Componentpublic class DailyPiracyJob {         Logger logger = LoggerFactory.getLogger(this.getClass());         @Autowired    private AppInfoService appInfoService;     @Scheduled(cron = "0 0 23 * * ?")    public void scan() throws Exception {        try {            List<AppInfo> allAppList = appInfoService.selectAllAppInfo();            if(null != allAppList && allAppList.size() > 0){                for(AppInfo appInfo : allAppList){                    appInfoService.insertDailyPiracy(appInfo.getAppMd5());                }            }        catch (Exception e) {            logger.error("error when Channel Monitoring.", e);        }    }}

@ Compont annotation, which enables Spring context to scan and automatically inject the required bean

@ Scheudle core annotation. No return value exists. cron defines the task running interval. For details, refer to other tutorials on the Internet.

Note that default-lazy-init = "true" cannot be enabled in applicationContext. xml; otherwise, the annotation will become invalid.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.