Skip to content

Instantly share code, notes, and snippets.

@uzresk
Last active August 29, 2015 14:10
Show Gist options
  • Save uzresk/570e7a453a30a91875d7 to your computer and use it in GitHub Desktop.
Save uzresk/570e7a453a30a91875d7 to your computer and use it in GitHub Desktop.
Quartz JDBC storeを使ってJOB管理(クラスタリングもやってみました) ref: http://qiita.com/uzresk/items/8a4cdbdbbfec67bcf658
postgres=# create database quartz;
CREATE DATABASE
quartz=# CREATE USER job WITH PASSWORD 'job';
CREATE ROLE
quartz=# CREATE SCHEMA job AUTHORIZATION job;
CREATE SCHEMA
quartz=# \c - job
データベース "quartz" にユーザ"job"として接続しました。
quartz=> \i tables_postgres.sql
psql:tables_postgres.sql:6: ERROR: table "qrtz_fired_triggers" does not exist
psql:tables_postgres.sql:7: ERROR: table "qrtz_paused_trigger_grps" does not
exist
psql:tables_postgres.sql:8: ERROR: table "qrtz_scheduler_state" does not exist
psql:tables_postgres.sql:9: ERROR: table "qrtz_locks" does not exist
psql:tables_postgres.sql:10: ERROR: table "qrtz_simple_triggers" does not exist
psql:tables_postgres.sql:11: ERROR: table "qrtz_cron_triggers" does not exist
psql:tables_postgres.sql:12: ERROR: table "qrtz_simprop_triggers" does not
exist
psql:tables_postgres.sql:13: ERROR: table "qrtz_blob_triggers" does not exist
psql:tables_postgres.sql:14: ERROR: table "qrtz_triggers" does not exist
psql:tables_postgres.sql:15: ERROR: table "qrtz_job_details" does not exist
psql:tables_postgres.sql:16: ERROR: table "qrtz_calendars" does not exist
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
psql:tables_postgres.sql:187: WARNING: there is no transaction in progress
quartz=> \d
リレーションの一覧
スキーマ | 名前 | 型 | 所有者
----------+--------------------------+----------+--------
job | qrtz_blob_triggers | テーブル | job
job | qrtz_calendars | テーブル | job
job | qrtz_cron_triggers | テーブル | job
job | qrtz_fired_triggers | テーブル | job
job | qrtz_job_details | テーブル | job
job | qrtz_locks | テーブル | job
job | qrtz_paused_trigger_grps | テーブル | job
job | qrtz_scheduler_state | テーブル | job
job | qrtz_simple_triggers | テーブル | job
job | qrtz_simprop_triggers | テーブル | job
job | qrtz_triggers | テーブル | job
(11 行)
java -classpath quartz-sample-0.0.1-SNAPSHOT.jar:/root/uzresk/quartz-examples/lib/* -Dorg.quartz.properties=/root/uzresk/quartz-examples/target/quartzauto.properties quartz.jdbc.simple.HelloJobStarter
package quartz.jdbc.simple;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloJob implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("hello " + new Date() + " :" + context.getJobDetail());
}
}
package quartz.jdbc.simple;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;
public class HelloJobStarter {
public static void main(String[] args) throws Exception {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
scheduler.shutdown();
}
}
org.quartz.scheduler.instanceId: AUTO
org.quartz.jobStore.isClustered=true
package quartz.jdbc.simple;
import java.util.TimeZone;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class RegistHelloJob {
public static void main(String[] args) throws Exception {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobDetail job = JobBuilder.newJob(HelloJob.class)
.withIdentity("helloJob", "jobGroup1")
.storeDurably()
.build();
CronTrigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("cronTrigger1", "triggerGroup1")
.withSchedule(
CronScheduleBuilder.cronSchedule("*/5 * * * * ?")
.inTimeZone(TimeZone.getTimeZone("Asia/Tokyo"))).startNow().build();
scheduler.scheduleJob(job, trigger);
System.out.println("JOBの登録が完了しました。");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment