下面列出了org.quartz.spi.ClassLoadHelper#initialize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void init() throws ParserConfigurationException, XPathException, ParseException, IOException, ValidationException, SchedulerException, SAXException, ClassNotFoundException {
boolean noFiles = files == null || files.isEmpty();
if (noFiles || !schedulerManager.isAutoProvisioning()) {
log.info("Not auto provisioning jobs: "+ ((noFiles)?"no files.":String.join(", ", files)));
return;
}
Scheduler scheduler = schedulerManager.getScheduler();
ClassLoadHelper clh = new CascadingClassLoadHelper();
clh.initialize();
for (String file : files ) {
XMLSchedulingDataProcessor proc = new XMLSchedulingDataProcessor(clh);
InputStream in = getClass().getResourceAsStream(file);
if (in == null) {
throw new IllegalArgumentException("Couldn't find resource on classpath: "+ file);
}
try {
proc.processStreamAndScheduleJobs(in, file, scheduler);
log.info("Successfully provisioned jobs/triggers from :"+ file);
} catch (ObjectAlreadyExistsException e) {
log.info("Not fully processing: "+ file+ " because some parts already exist");
}
}
}
public void init() throws ParserConfigurationException, XPathException, ParseException, IOException, ValidationException, SchedulerException, SAXException, ClassNotFoundException {
boolean noFiles = files == null || files.isEmpty();
if (noFiles || !schedulerManager.isAutoProvisioning()) {
log.info("Not auto provisioning jobs: "+ ((noFiles)?"no files.":String.join(", ", files)));
return;
}
Scheduler scheduler = schedulerManager.getScheduler();
ClassLoadHelper clh = new CascadingClassLoadHelper();
clh.initialize();
for (String file : files ) {
XMLSchedulingDataProcessor proc = new XMLSchedulingDataProcessor(clh);
InputStream in = getClass().getResourceAsStream(file);
if (in == null) {
throw new IllegalArgumentException("Couldn't find resource on classpath: "+ file);
}
try {
proc.processStreamAndScheduleJobs(in, file, scheduler);
log.info("Successfully provisioned jobs/triggers from :"+ file);
} catch (ObjectAlreadyExistsException e) {
log.info("Not fully processing: "+ file+ " because some parts already exist");
}
}
}
/**
* Called to give the ClassLoadHelper a chance to initialize itself,
* including the opportunity to "steal" the class loader off of the calling
* thread, which is the thread that is initializing Quartz.
*/
public void initialize() {
loadHelpers = new LinkedList<ClassLoadHelper>();
loadHelpers.add(new LoadingLoaderClassLoadHelper());
loadHelpers.add(new SimpleClassLoadHelper());
loadHelpers.add(new ThreadContextClassLoadHelper());
loadHelpers.add(new InitThreadContextClassLoadHelper());
for(ClassLoadHelper loadHelper: loadHelpers) {
loadHelper.initialize();
}
}
/**
* Called to give the ClassLoadHelper a chance to initialize itself,
* including the opportunity to "steal" the class loader off of the calling
* thread, which is the thread that is initializing Quartz.
*/
public void initialize() {
loadHelpers = new LinkedList();
loadHelpers.add(new LoadingLoaderClassLoadHelper());
loadHelpers.add(new SimpleClassLoadHelper());
loadHelpers.add(new ThreadContextClassLoadHelper());
loadHelpers.add(new InitThreadContextClassLoadHelper());
Iterator iter = loadHelpers.iterator();
while (iter.hasNext()) {
ClassLoadHelper loadHelper = (ClassLoadHelper) iter.next();
loadHelper.initialize();
}
}
@Before
public void setUp() throws Exception {
ClassLoadHelper loadHelper = new CascadingClassLoadHelper();
nodeAccess = mock(NodeAccess.class);
loadHelper.initialize();
this.jobStore = createJobStore();
this.jobStore.start();
this.jobStore.initialize(loadHelper, new SampleSignaler());
this.jobStore.schedulerStarted();
}
@SuppressWarnings("deprecation")
@Before
public void setUp() throws Exception {
this.fSignaler = new SampleSignaler();
ClassLoadHelper loadHelper = new CascadingClassLoadHelper();
loadHelper.initialize();
this.fJobStore = createJobStore("AbstractJobStoreTest");
this.fJobStore.initialize(loadHelper, this.fSignaler);
this.fJobStore.schedulerStarted();
this.fJobDetail = new JobDetailImpl("job1", "jobGroup1", MyJob.class);
this.fJobDetail.setDurability(true);
this.fJobStore.storeJob(this.fJobDetail, false);
}
@Test
public void testAcquireTriggers() throws Exception {
SchedulerSignaler schedSignaler = new SampleSignaler();
ClassLoadHelper loadHelper = new CascadingClassLoadHelper();
loadHelper.initialize();
JobStore store = createJobStore("testAcquireTriggers");
store.initialize(loadHelper, schedSignaler);
// Setup: Store jobs and triggers.
long MIN = 60 * 1000L;
Date startTime0 = new Date(System.currentTimeMillis() + MIN); // a min from now.
for (int i=0; i < 10; i++) {
Date startTime = new Date(startTime0.getTime() + i * MIN); // a min apart
JobDetail job = JobBuilder.newJob(MyJob.class).withIdentity("job" + i).build();
SimpleScheduleBuilder schedule = SimpleScheduleBuilder.repeatMinutelyForever(2);
OperableTrigger trigger = (OperableTrigger)TriggerBuilder.newTrigger().withIdentity("job" + i).withSchedule(schedule).forJob(job).startAt(startTime).build();
// Manually trigger the first fire time computation that scheduler would do. Otherwise
// the store.acquireNextTriggers() will not work properly.
Date fireTime = trigger.computeFirstFireTime(null);
Assert.assertEquals(true, fireTime != null);
store.storeJobAndTrigger(job, trigger);
}
// Test acquire one trigger at a time
for (int i=0; i < 10; i++) {
long noLaterThan = (startTime0.getTime() + i * MIN);
int maxCount = 1;
long timeWindow = 0;
List<OperableTrigger> triggers = store.acquireNextTriggers(noLaterThan, maxCount, timeWindow);
Assert.assertEquals(1, triggers.size());
Assert.assertEquals("job" + i, triggers.get(0).getKey().getName());
// Let's remove the trigger now.
store.removeJob(triggers.get(0).getJobKey());
}
}
@Test
public void testAcquireTriggersInBatch() throws Exception {
SchedulerSignaler schedSignaler = new SampleSignaler();
ClassLoadHelper loadHelper = new CascadingClassLoadHelper();
loadHelper.initialize();
JobStore store = createJobStore("testAcquireTriggersInBatch");
store.initialize(loadHelper, schedSignaler);
// Setup: Store jobs and triggers.
long MIN = 60 * 1000L;
Date startTime0 = new Date(System.currentTimeMillis() + MIN); // a min from now.
for (int i=0; i < 10; i++) {
Date startTime = new Date(startTime0.getTime() + i * MIN); // a min apart
JobDetail job = JobBuilder.newJob(MyJob.class).withIdentity("job" + i).build();
SimpleScheduleBuilder schedule = SimpleScheduleBuilder.repeatMinutelyForever(2);
OperableTrigger trigger = (OperableTrigger)TriggerBuilder.newTrigger().withIdentity("job" + i).withSchedule(schedule).forJob(job).startAt(startTime).build();
// Manually trigger the first fire time computation that scheduler would do. Otherwise
// the store.acquireNextTriggers() will not work properly.
Date fireTime = trigger.computeFirstFireTime(null);
Assert.assertEquals(true, fireTime != null);
store.storeJobAndTrigger(job, trigger);
}
// Test acquire batch of triggers at a time
long noLaterThan = startTime0.getTime() + 10 * MIN;
int maxCount = 7;
// time window needs to be big to be able to pick up multiple triggers when they are a minute apart
long timeWindow = 8 * MIN;
List<OperableTrigger> triggers = store.acquireNextTriggers(noLaterThan, maxCount, timeWindow);
Assert.assertEquals(7, triggers.size());
for (int i=0; i < 7; i++) {
Assert.assertEquals("job" + i, triggers.get(i).getKey().getName());
}
}
/**
* Register jobs and triggers (within a transaction, if possible).
*/
protected void registerJobsAndTriggers() throws SchedulerException {
TransactionStatus transactionStatus = null;
if (this.transactionManager != null) {
transactionStatus = this.transactionManager.getTransaction(TransactionDefinition.withDefaults());
}
try {
if (this.jobSchedulingDataLocations != null) {
ClassLoadHelper clh = new ResourceLoaderClassLoadHelper(this.resourceLoader);
clh.initialize();
XMLSchedulingDataProcessor dataProcessor = new XMLSchedulingDataProcessor(clh);
for (String location : this.jobSchedulingDataLocations) {
dataProcessor.processFileAndScheduleJobs(location, getScheduler());
}
}
// Register JobDetails.
if (this.jobDetails != null) {
for (JobDetail jobDetail : this.jobDetails) {
addJobToScheduler(jobDetail);
}
}
else {
// Create empty list for easier checks when registering triggers.
this.jobDetails = new LinkedList<>();
}
// Register Calendars.
if (this.calendars != null) {
for (String calendarName : this.calendars.keySet()) {
Calendar calendar = this.calendars.get(calendarName);
getScheduler().addCalendar(calendarName, calendar, true, true);
}
}
// Register Triggers.
if (this.triggers != null) {
for (Trigger trigger : this.triggers) {
addTriggerToScheduler(trigger);
}
}
}
catch (Throwable ex) {
if (transactionStatus != null) {
try {
this.transactionManager.rollback(transactionStatus);
}
catch (TransactionException tex) {
logger.error("Job registration exception overridden by rollback exception", ex);
throw tex;
}
}
if (ex instanceof SchedulerException) {
throw (SchedulerException) ex;
}
if (ex instanceof Exception) {
throw new SchedulerException("Registration of jobs and triggers failed: " + ex.getMessage(), ex);
}
throw new SchedulerException("Registration of jobs and triggers failed: " + ex.getMessage());
}
if (transactionStatus != null) {
this.transactionManager.commit(transactionStatus);
}
}
/**
* Register jobs and triggers (within a transaction, if possible).
*/
protected void registerJobsAndTriggers() throws SchedulerException {
TransactionStatus transactionStatus = null;
if (this.transactionManager != null) {
transactionStatus = this.transactionManager.getTransaction(new DefaultTransactionDefinition());
}
try {
if (this.jobSchedulingDataLocations != null) {
ClassLoadHelper clh = new ResourceLoaderClassLoadHelper(this.resourceLoader);
clh.initialize();
XMLSchedulingDataProcessor dataProcessor = new XMLSchedulingDataProcessor(clh);
for (String location : this.jobSchedulingDataLocations) {
dataProcessor.processFileAndScheduleJobs(location, getScheduler());
}
}
// Register JobDetails.
if (this.jobDetails != null) {
for (JobDetail jobDetail : this.jobDetails) {
addJobToScheduler(jobDetail);
}
}
else {
// Create empty list for easier checks when registering triggers.
this.jobDetails = new LinkedList<>();
}
// Register Calendars.
if (this.calendars != null) {
for (String calendarName : this.calendars.keySet()) {
Calendar calendar = this.calendars.get(calendarName);
getScheduler().addCalendar(calendarName, calendar, true, true);
}
}
// Register Triggers.
if (this.triggers != null) {
for (Trigger trigger : this.triggers) {
addTriggerToScheduler(trigger);
}
}
}
catch (Throwable ex) {
if (transactionStatus != null) {
try {
this.transactionManager.rollback(transactionStatus);
}
catch (TransactionException tex) {
logger.error("Job registration exception overridden by rollback exception", ex);
throw tex;
}
}
if (ex instanceof SchedulerException) {
throw (SchedulerException) ex;
}
if (ex instanceof Exception) {
throw new SchedulerException("Registration of jobs and triggers failed: " + ex.getMessage(), ex);
}
throw new SchedulerException("Registration of jobs and triggers failed: " + ex.getMessage());
}
if (transactionStatus != null) {
this.transactionManager.commit(transactionStatus);
}
}
/**
* Register jobs and triggers (within a transaction, if possible).
*/
protected void registerJobsAndTriggers() throws SchedulerException {
TransactionStatus transactionStatus = null;
if (this.transactionManager != null) {
transactionStatus = this.transactionManager.getTransaction(new DefaultTransactionDefinition());
}
try {
if (this.jobSchedulingDataLocations != null) {
ClassLoadHelper clh = new ResourceLoaderClassLoadHelper(this.resourceLoader);
clh.initialize();
XMLSchedulingDataProcessor dataProcessor = new XMLSchedulingDataProcessor(clh);
for (String location : this.jobSchedulingDataLocations) {
dataProcessor.processFileAndScheduleJobs(location, getScheduler());
}
}
// Register JobDetails.
if (this.jobDetails != null) {
for (JobDetail jobDetail : this.jobDetails) {
addJobToScheduler(jobDetail);
}
}
else {
// Create empty list for easier checks when registering triggers.
this.jobDetails = new LinkedList<JobDetail>();
}
// Register Calendars.
if (this.calendars != null) {
for (String calendarName : this.calendars.keySet()) {
Calendar calendar = this.calendars.get(calendarName);
getScheduler().addCalendar(calendarName, calendar, true, true);
}
}
// Register Triggers.
if (this.triggers != null) {
for (Trigger trigger : this.triggers) {
addTriggerToScheduler(trigger);
}
}
}
catch (Throwable ex) {
if (transactionStatus != null) {
try {
this.transactionManager.rollback(transactionStatus);
}
catch (TransactionException tex) {
logger.error("Job registration exception overridden by rollback exception", ex);
throw tex;
}
}
if (ex instanceof SchedulerException) {
throw (SchedulerException) ex;
}
if (ex instanceof Exception) {
throw new SchedulerException("Registration of jobs and triggers failed: " + ex.getMessage(), ex);
}
throw new SchedulerException("Registration of jobs and triggers failed: " + ex.getMessage());
}
if (transactionStatus != null) {
this.transactionManager.commit(transactionStatus);
}
}
/**
* Register jobs and triggers (within a transaction, if possible).
*/
protected void registerJobsAndTriggers() throws SchedulerException {
TransactionStatus transactionStatus = null;
if (this.transactionManager != null) {
transactionStatus = this.transactionManager.getTransaction(new DefaultTransactionDefinition());
}
try {
if (this.jobSchedulingDataLocations != null) {
ClassLoadHelper clh = new ResourceLoaderClassLoadHelper(this.resourceLoader);
clh.initialize();
XMLSchedulingDataProcessor dataProcessor = new XMLSchedulingDataProcessor(clh);
for (String location : this.jobSchedulingDataLocations) {
dataProcessor.processFileAndScheduleJobs(location, getScheduler());
}
}
// Register JobDetails.
if (this.jobDetails != null) {
for (JobDetail jobDetail : this.jobDetails) {
addJobToScheduler(jobDetail);
}
}
else {
// Create empty list for easier checks when registering triggers.
this.jobDetails = new LinkedList<JobDetail>();
}
// Register Calendars.
if (this.calendars != null) {
for (String calendarName : this.calendars.keySet()) {
Calendar calendar = this.calendars.get(calendarName);
getScheduler().addCalendar(calendarName, calendar, true, true);
}
}
// Register Triggers.
if (this.triggers != null) {
for (Trigger trigger : this.triggers) {
addTriggerToScheduler(trigger);
}
}
}
catch (Throwable ex) {
if (transactionStatus != null) {
try {
this.transactionManager.rollback(transactionStatus);
}
catch (TransactionException tex) {
logger.error("Job registration exception overridden by rollback exception", ex);
throw tex;
}
}
if (ex instanceof SchedulerException) {
throw (SchedulerException) ex;
}
if (ex instanceof Exception) {
throw new SchedulerException("Registration of jobs and triggers failed: " + ex.getMessage(), ex);
}
throw new SchedulerException("Registration of jobs and triggers failed: " + ex.getMessage());
}
if (transactionStatus != null) {
this.transactionManager.commit(transactionStatus);
}
}
@Test
public void testMatchers() throws Exception {
SchedulerSignaler schedSignaler = new SampleSignaler();
ClassLoadHelper loadHelper = new CascadingClassLoadHelper();
loadHelper.initialize();
JobStore store = createJobStore("testMatchers");
store.initialize(loadHelper, schedSignaler);
JobDetail job = JobBuilder.newJob(MyJob.class).withIdentity("job1", "aaabbbccc").build();
store.storeJob(job, true);
SimpleScheduleBuilder schedule = SimpleScheduleBuilder.simpleSchedule();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trig1", "aaabbbccc").withSchedule(schedule).forJob(job).build();
store.storeTrigger((OperableTrigger) trigger, true);
job = JobBuilder.newJob(MyJob.class).withIdentity("job1", "xxxyyyzzz").build();
store.storeJob(job, true);
schedule = SimpleScheduleBuilder.simpleSchedule();
trigger = TriggerBuilder.newTrigger().withIdentity("trig1", "xxxyyyzzz").withSchedule(schedule).forJob(job).build();
store.storeTrigger((OperableTrigger)trigger, true);
job = JobBuilder.newJob(MyJob.class).withIdentity("job2", "xxxyyyzzz").build();
store.storeJob(job, true);
schedule = SimpleScheduleBuilder.simpleSchedule();
trigger = TriggerBuilder.newTrigger().withIdentity("trig2", "xxxyyyzzz").withSchedule(schedule).forJob(job).build();
store.storeTrigger((OperableTrigger)trigger, true);
Set<JobKey> jkeys = store.getJobKeys(GroupMatcher.anyJobGroup());
Assert.assertEquals("Wrong number of jobs found by anything matcher", 3, jkeys.size());
jkeys = store.getJobKeys(GroupMatcher.jobGroupEquals("xxxyyyzzz"));
Assert.assertEquals("Wrong number of jobs found by equals matcher", 2, jkeys.size());
jkeys = store.getJobKeys(GroupMatcher.jobGroupEquals("aaabbbccc"));
Assert.assertEquals("Wrong number of jobs found by equals matcher", 1, jkeys.size());
jkeys = store.getJobKeys(GroupMatcher.jobGroupStartsWith("aa"));
Assert.assertEquals("Wrong number of jobs found by starts with matcher", 1, jkeys.size());
jkeys = store.getJobKeys(GroupMatcher.jobGroupStartsWith("xx"));
Assert.assertEquals("Wrong number of jobs found by starts with matcher", 2, jkeys.size());
jkeys = store.getJobKeys(GroupMatcher.jobGroupEndsWith("cc"));
Assert.assertEquals("Wrong number of jobs found by ends with matcher", 1, jkeys.size());
jkeys = store.getJobKeys(GroupMatcher.jobGroupEndsWith("zzz"));
Assert.assertEquals("Wrong number of jobs found by ends with matcher", 2, jkeys.size());
jkeys = store.getJobKeys(GroupMatcher.jobGroupContains("bc"));
Assert.assertEquals("Wrong number of jobs found by contains with matcher", 1, jkeys.size());
jkeys = store.getJobKeys(GroupMatcher.jobGroupContains("yz"));
Assert.assertEquals("Wrong number of jobs found by contains with matcher", 2, jkeys.size());
Set<TriggerKey> tkeys = store.getTriggerKeys(GroupMatcher.anyTriggerGroup());
Assert.assertEquals("Wrong number of triggers found by anything matcher", 3, tkeys.size());
tkeys = store.getTriggerKeys(GroupMatcher.triggerGroupEquals("xxxyyyzzz"));
Assert.assertEquals("Wrong number of triggers found by equals matcher", 2, tkeys.size());
tkeys = store.getTriggerKeys(GroupMatcher.triggerGroupEquals("aaabbbccc"));
Assert.assertEquals("Wrong number of triggers found by equals matcher", 1, tkeys.size());
tkeys = store.getTriggerKeys(GroupMatcher.triggerGroupStartsWith("aa"));
Assert.assertEquals("Wrong number of triggers found by starts with matcher", 1, tkeys.size());
tkeys = store.getTriggerKeys(GroupMatcher.triggerGroupStartsWith("xx"));
Assert.assertEquals("Wrong number of triggers found by starts with matcher", 2, tkeys.size());
tkeys = store.getTriggerKeys(GroupMatcher.triggerGroupEndsWith("cc"));
Assert.assertEquals("Wrong number of triggers found by ends with matcher", 1, tkeys.size());
tkeys = store.getTriggerKeys(GroupMatcher.triggerGroupEndsWith("zzz"));
Assert.assertEquals("Wrong number of triggers found by ends with matcher", 2, tkeys.size());
tkeys = store.getTriggerKeys(GroupMatcher.triggerGroupContains("bc"));
Assert.assertEquals("Wrong number of triggers found by contains with matcher", 1, tkeys.size());
tkeys = store.getTriggerKeys(GroupMatcher.triggerGroupContains("yz"));
Assert.assertEquals("Wrong number of triggers found by contains with matcher", 2, tkeys.size());
}