java quartz

 

import java.util.ArrayList;
import java.util.Date;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;

public class TimerTest {

    public static void main(String... strings) {
        SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();

        Scheduler sched;
        try {

            // sched = schedFact.getScheduler();
            sched = StdSchedulerFactory.getDefaultScheduler();

            JobDetail jobDetail = new JobDetail("myJob", // job name
                                                sched.DEFAULT_GROUP, // job group (you can also specify 'null' to
                                                // use the default group)
                                                TestJob.class); // the java class to execute

            jobDetail.getJobDataMap().put("jobSays", "Hello World!");
            jobDetail.getJobDataMap().put("myFloatValue", 3.141f);
            jobDetail.getJobDataMap().put("myStateData", new ArrayList());

            Trigger trigger = TriggerUtils.makeDailyTrigger(0, 1);
            trigger.setStartTime(new Date());
            trigger.setName("myTrigger");

//Add the given JobDetail to the Scheduler,
//and associate the given Trigger with it.
            sched.scheduleJob(jobDetail, trigger);             sched.start();
            // sched.shutdown();

        } catch (SchedulerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}










import java.util.ArrayList;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class TestJob implements Job {

    public void execute(JobExecutionContext context) throws JobExecutionException {
        // TODO Auto-generated method stub
        System.out.println("........................................");
        String instName = context.getJobDetail().getName();
        String instGroup = context.getJobDetail().getGroup();

        JobDataMap dataMap = context.getJobDetail().getJobDataMap();

        String jobSays = dataMap.getString("jobSays");
        float myFloatValue = dataMap.getFloat("myFloatValue");
        ArrayList state = (ArrayList) dataMap.get("myStateData");
        state.add(new Date());

        System.out.println("Instance " + instName + " of DumbJob says: " + jobSays);
    }

}
 

你可能感兴趣的:(java,quartz)