• 定时任务,Java(二)


     
    public class SimpleQuartzJob implements Job{
     
        @Override
        public void execute(JobExecutionContext arg0) throws JobExecutionException {
            String key = "hello1";
            String value = (String)arg0.getJobDetail().getJobDataMap().get(key);
            System.out.println("key is " + key + ", value is " + value);
        }
     
    }
    
    
    
    
    
    
    public class Main {
        private Scheduler scheduler;
        
        public void task() throws SchedulerException
        {
            try {
                // Initiate a Schedule Factory
                if (DirectSchedulerFactory.getInstance().getAllSchedulers().isEmpty()) {
                    DirectSchedulerFactory.getInstance().createVolatileScheduler(5);
                }
                // Retrieve a scheduler from schedule factory
                this.scheduler = DirectSchedulerFactory.getInstance().getScheduler();
            }
            catch (final SchedulerException e) {
                System.out.println("scheduler构造出错");
            }
            
            
            // Initiate JobDetail with executable job class
            JobDetail jobDetail = newJob(SimpleQuartzJob.class).build();
            jobDetail.getJobDataMap().put("hello1", "value1");
            // Initiate Trigger with an expression 
            Trigger trigger = newTrigger().withSchedule(cronSchedule("0/5 * * * * ?")).forJob(jobDetail).build();
            
            this.scheduler.start();
            this.scheduler.scheduleJob(jobDetail, trigger);
            
        }
        
     
        public static void main(String[] args){
     
            Main o = new Main();
            try {
                o.task();
            } catch (SchedulerException e) {
                System.out.println("main()函数出错");
            }
        }
     
    }
  • 相关阅读:
    关于python做人工智能的一个网页(很牛逼)
    python操作Excel模块openpyxl
    linux环境下安装mysql
    爬虫框架scrapy
    爬虫
    2017.2.6 开工大吉
    C# List集合Group by查询
    Datatable 列查询,统计值
    调用WCF Client客户端测试
    Oracle数据库导入导出命令
  • 原文地址:https://www.cnblogs.com/alipayhutu/p/2536617.html
Copyright © 2020-2023  润新知