Quartz作业调度定时完成数据库操作

来源:互联网 发布:java hadoop 开发教程 编辑:程序博客网 时间:2024/05/14 14:29
public class QuartzTest {
    public static void main(String args[]) throws SchedulerException, ParseException {
        JobDetail jobDetail= JobBuilder.newJob(TestJob.class)
                .withIdentity("testJob_1","group_1")
                .build();
 
        Trigger trigger= TriggerBuilder
                .newTrigger()
                .withIdentity("trigger_1","group_1")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInHours(24) //时间间隔24小时
                        .withRepeatCount(3)        //重复次数(将执行4次)
                        )
                .build();
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();
 
        sched.scheduleJob(jobDetail,trigger);
        sched.start();
    }

}


TestJob.java

public class TestJob implements Job {
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("-------Hello World! ------" + new Date());
        //do more...
        Connection connection = null;
        PreparedStatement ps=null;
        ResultSet rs = null;
        try {
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection(
"jdbc:postgresql://127.0.0.1:5432/emsprodb", "postgres",
"123");
ps = connection.prepareStatement("select * from worktastcreate()");
rs = ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}
    }


postgresql的worktastcreate()语句:

CREATE OR REPLACE FUNCTION worktastcreate()
  RETURNS integer AS
$BODY$
     begin
          INSERT INTO work_task(
            code, name_en, name_cn, create_by, update_by, create_datetime, 
            update_datetime, version, spec_code, incharge, assign_by, priority, 
            factory_code, location_code) values('ad', 'cn', 'en','rao','rao', now(), 
            now(), 1, 'xx', 'xx', 'xx', 1, 
            'xx', 'xx');
            return 0;
     end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION worktastcreate()
  OWNER TO postgres;

0 0
原创粉丝点击