Incomplete - task 89: Resurrect Quartz sample
This commit is contained in:
15
spring-batch-samples/.settings/quartzLauncher.launch
Normal file
15
spring-batch-samples/.settings/quartzLauncher.launch
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
|
||||
<stringAttribute key="bad_container_name" value="\spring-batch-samples\.settings\j"/>
|
||||
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
|
||||
<listEntry value="/spring-batch-samples/src/main/java/org/springframework/batch/sample/quartz/QuartzBatchLauncher.java"/>
|
||||
</listAttribute>
|
||||
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
|
||||
<listEntry value="1"/>
|
||||
</listAttribute>
|
||||
<booleanAttribute key="org.eclipse.debug.core.appendEnvironmentVariables" value="true"/>
|
||||
<stringAttribute key="org.eclipse.debug.core.source_locator_id" value="org.eclipse.jdt.launching.sourceLocator.JavaSourceLookupDirector"/>
|
||||
<stringAttribute key="org.eclipse.debug.core.source_locator_memento" value="<?xml version="1.0" encoding="UTF-8"?> <sourceLookupDirector> <sourceContainers duplicates="false"> <container memento="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;javaProject name=&quot;spring-batch-core&quot;/&gt;&#13;&#10;" typeId="org.eclipse.jdt.launching.sourceContainer.javaProject"/> <container memento="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;javaProject name=&quot;spring-batch-execution&quot;/&gt;&#13;&#10;" typeId="org.eclipse.jdt.launching.sourceContainer.javaProject"/> <container memento="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;javaProject name=&quot;spring-batch-infrastructure&quot;/&gt;&#13;&#10;" typeId="org.eclipse.jdt.launching.sourceContainer.javaProject"/> <container memento="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;javaProject name=&quot;spring-batch-integration&quot;/&gt;&#13;&#10;" typeId="org.eclipse.jdt.launching.sourceContainer.javaProject"/> <container memento="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;javaProject name=&quot;spring-batch-samples&quot;/&gt;&#13;&#10;" typeId="org.eclipse.jdt.launching.sourceContainer.javaProject"/> <container memento="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;default/&gt;&#13;&#10;" typeId="org.eclipse.debug.core.containerType.default"/> </sourceContainers> </sourceLookupDirector> "/>
|
||||
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.springframework.batch.sample.quartz.QuartzBatchLauncher"/>
|
||||
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="spring-batch-samples"/>
|
||||
</launchConfiguration>
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright 2006-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.batch.sample.launch;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
|
||||
public class QuartzBatchLauncher {
|
||||
private static Log log = LogFactory.getLog(QuartzBatchLauncher.class);
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
if (args[0] == null) {
|
||||
log.error("Missing argument: provide a path to configuration file");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
new ClassPathXmlApplicationContext(args[0] + ".xml");
|
||||
|
||||
log.info("Quartz context initialized");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2006-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.batch.sample.quartz;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.springframework.batch.core.configuration.JobLocator;
|
||||
import org.springframework.batch.core.domain.JobParameters;
|
||||
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
|
||||
import org.springframework.batch.core.repository.JobRestartException;
|
||||
import org.springframework.batch.core.repository.NoSuchJobException;
|
||||
import org.springframework.batch.execution.launch.JobLauncher;
|
||||
import org.springframework.scheduling.quartz.QuartzJobBean;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class JobLauncherDetails extends QuartzJobBean {
|
||||
|
||||
private static Log log = LogFactory.getLog(JobLauncherDetails.class);
|
||||
|
||||
private JobLocator jobLocator;
|
||||
|
||||
private JobLauncher jobLauncher;
|
||||
|
||||
/**
|
||||
* Public setter for the {@link JobLocator}.
|
||||
* @param jobLocator the {@link JobLocator} to set
|
||||
*/
|
||||
public void setJobLocator(JobLocator jobLocator) {
|
||||
this.jobLocator = jobLocator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the {@link JobLauncher}.
|
||||
* @param jobLauncher the {@link JobLauncher} to set
|
||||
*/
|
||||
public void setJobLauncher(JobLauncher jobLauncher) {
|
||||
this.jobLauncher = jobLauncher;
|
||||
}
|
||||
|
||||
protected void executeInternal(JobExecutionContext context) {
|
||||
String jobName = (String) context.getJobDetail().getJobDataMap().get("jobName");
|
||||
JobParameters jobParameters = new JobParameters();
|
||||
try {
|
||||
jobLauncher.run(jobLocator.getJob(jobName), jobParameters);
|
||||
}
|
||||
catch (JobExecutionAlreadyRunningException e) {
|
||||
log.error("Could not execute job.", e);
|
||||
}
|
||||
catch (JobRestartException e) {
|
||||
log.error("Could not execute job.", e);
|
||||
}
|
||||
catch (NoSuchJobException e) {
|
||||
log.error("Could not execute job.", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright 2006-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.batch.sample.quartz;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.core.configuration.JobRegistry;
|
||||
import org.springframework.batch.core.domain.Job;
|
||||
import org.springframework.batch.core.repository.DuplicateJobException;
|
||||
import org.springframework.batch.sample.ClassPathXmlApplicationContextJobFactory;
|
||||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.xml.XmlBeanFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
|
||||
public class QuartzBatchLauncher {
|
||||
|
||||
private static Log log = LogFactory.getLog(QuartzBatchLauncher.class);
|
||||
|
||||
private JobRegistry registry;
|
||||
|
||||
private ResourceLoader resourceLoader;
|
||||
|
||||
private ApplicationContext parentContext = null;
|
||||
|
||||
/**
|
||||
* Public setter for the {@link JobRegistry}.
|
||||
* @param registry the registry to set
|
||||
*/
|
||||
public void setRegistry(JobRegistry registry) {
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.context.ResourceLoaderAware#setResourceLoader(org.springframework.core.io.ResourceLoader)
|
||||
*/
|
||||
public void setResourceLoader(ResourceLoader resourceLoader) {
|
||||
this.resourceLoader = resourceLoader;
|
||||
}
|
||||
|
||||
private void register(String[] paths) throws DuplicateJobException {
|
||||
for (int i = 0; i < paths.length; i++) {
|
||||
String path = paths[i];
|
||||
ConfigurableListableBeanFactory beanFactory = new XmlBeanFactory(resourceLoader.getResource(path),
|
||||
parentContext.getAutowireCapableBeanFactory());
|
||||
String[] names = beanFactory.getBeanNamesForType(Job.class);
|
||||
for (int j = 0; j < names.length; j++) {
|
||||
registry.register(new ClassPathXmlApplicationContextJobFactory(names[j], path, parentContext));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
final QuartzBatchLauncher launcher = new QuartzBatchLauncher();
|
||||
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
launcher.run();
|
||||
};
|
||||
}).start();
|
||||
|
||||
while (launcher.parentContext == null) {
|
||||
Thread.sleep(100L);
|
||||
}
|
||||
|
||||
// Paths to individual job configurations.
|
||||
final String[] paths = new String[] { "jobs/adhocLoopJob.xml", "jobs/footballJob.xml" };
|
||||
|
||||
launcher.register(paths);
|
||||
|
||||
log.info("Started Quartz scheduler.");
|
||||
System.in.read();
|
||||
}
|
||||
|
||||
private void run() {
|
||||
|
||||
/*
|
||||
* A simple execution environment with a Quartz scheduler. This will be
|
||||
* used as the parent context for loading job configurations.
|
||||
*/
|
||||
final ApplicationContext parent = new ClassPathXmlApplicationContext("quartz-job-launcher-context.xml");
|
||||
parent.getAutowireCapableBeanFactory().autowireBeanProperties(this,
|
||||
AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
|
||||
parent.getAutowireCapableBeanFactory().initializeBean(this, "quartzLauncher");
|
||||
this.parentContext = parent;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
|
||||
http://www.springframework.org/schema/aop
|
||||
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
|
||||
|
||||
<import resource="simple-container-definition.xml" />
|
||||
|
||||
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
|
||||
<property name="triggers">
|
||||
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
|
||||
<property name="jobDetail" ref="jobDetail" />
|
||||
<property name="cronExpression" value="0/10 * * * * ?" />
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
|
||||
<property name="jobClass" value="org.springframework.batch.sample.quartz.JobLauncherDetails" />
|
||||
<property name="group" value="quartz-batch" />
|
||||
<property name="jobDataAsMap">
|
||||
<map>
|
||||
<entry key="jobName" value="footballJob"/>
|
||||
<entry key="jobLocator" value-ref="jobConfigurationRegistry"/>
|
||||
<entry key="jobLauncher" value-ref="jobLauncher"/>
|
||||
</map>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
|
||||
<bean id="jobLauncher" class="org.springframework.batch.execution.launch.SimpleJobLauncher">
|
||||
<property name="jobRepository" ref="jobRepository" />
|
||||
<property name="taskExecutor">
|
||||
<bean class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user