티스토리 뷰
출처 : http://wiki.dev.daewoobrenic.co.kr/mediawiki/index.php/Spring_Quartz_Batch
WEB-INF\web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/job-bean.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
WEB-INF\classes\job-bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<import resource="classpath:simple-job-launcher-context.xml"/>
<import resource="classpath:myHelloJob.xml"/>
<bean id="helloJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="quartz.example.HelloJob" />
<property name="jobDataAsMap">
<map>
<entry key="launcher" value-ref="jobLauncher"/>
<entry key="job" value-ref="myHelloJob"/>
</map>
</property>
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="helloJob" />
<property name="startDelay" value="10000" />
<property name="repeatInterval" value="50000" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger"/>
</list>
</property>
</bean>
</beans>
WEB-INF\classes\simple-job-launcher-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<import resource="classpath:data-source-context.xml"/>
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<property name="databaseType" value="MYSQL" />
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transctionManager" />
</bean>
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<bean id="simpleJob" class="org.springframework.batch.core.job.SimpleJob" abstract="true">
<property name="jobRepository" ref="jobRepository" />
</bean>
<bean id="taskletStep" class="org.springframework.batch.core.step.tasklet.TaskletStep" abstract="true">
<property name="jobRepository" ref="jobRepository" />
<property name="transactionManager" ref="transctionManager" />
</bean>
</beans>
WEB-INF\classes\data-source-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<import resource="classpath:data-source-context-init.xml"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:5141/batch"></property>
<property name="username" value="sa"></property>
<property name="password" value="sqldba"></property>
</bean>
<bean id="transctionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
lazy-init="true">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
WEB-INF\classes\data-source-context-init.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<bean id="initializingFactoryBean" class="quartz.example.InitializingDataSourceFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="initScript">
<value>classpath:schema-mysql.sql</value>
</property>
</bean>
</beans>
WEB-INF\classes\myHelloJob.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<bean id="myHelloJob" parent="simpleJob">
<property name="name" value="myHelloJob" />
<property name="steps">
<list>
<bean id="firstHello" parent="taskletStep">
<property name="tasklet">
<bean class="quartz.example.MyHello">
<property name="message" value="Hi~" />
</bean>
</property>
</bean>
</list>
</property>
</bean>
</beans>
package quartz.example;
public class InitializingDataSourceFactoryBean extends AbstractFactoryBean {
private Resource initScript;
private Resource destroyScript;
DataSource dataSource;
protected Object createInstance() throws Exception {
Assert.notNull(dataSource);
try {
doExecuteScript(destroyScript);
}
catch (Exception e) {
logger.debug("Could not execute destroy script [" + destroyScript + "]", e);
}
doExecuteScript(initScript);
return dataSource;
}
private void doExecuteScript(final Resource scriptResource) {
try {
if (scriptResource == null || !scriptResource.exists())
return;
TransactionTemplate transactionTemplate = new TransactionTemplate(new DataSourceTransactionManager(dataSource));
if (initScript != null) {
transactionTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String[] scripts;
try {
scripts = StringUtils.delimitedListToStringArray(stripComments(IOUtils.readLines(scriptResource
.getInputStream())), ";");
}
catch (IOException e) {
throw new BeanInitializationException("Cannot load script from [" + initScript + "]", e);
}
for (int i = 0; i < scripts.length; i++) {
String script = scripts[i].trim();
if (StringUtils.hasText(script)) {
jdbcTemplate.execute(scripts[i]);
}
}
return null;
}
});
}
} catch (Exception e) {
logger.debug("기존에 스키마가 생성된 경우 오류가 발생 할 수 있으며, 오류 발생시 무시", e);
}
}
private String stripComments(List list) {
StringBuffer buffer = new StringBuffer();
for (Iterator iter = list.iterator(); iter.hasNext();) {
String line = (String) iter.next();
if (!line.startsWith("//") && !line.startsWith("--")) {
buffer.append(line + "\n");
}
}
return buffer.toString();
}
package quartz.example;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.InitializingBean;
public class MyHello implements InitializingBean, Tasklet {
private static Log _log = LogFactory.getLog(MyHello.class);
private String message = "";
public void setMessage(String message) {
this.message = message;
}
@Override
public void afterPropertiesSet() throws Exception {
_log.info("========= " + this.getClass().getSimpleName() + "=============" + message);
}
@Override
public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception {
_log.info(">>>>>>>>>>> " + this.getClass().getSimpleName() + "=====" + message );
return RepeatStatus.FINISHED;
}
}
package quartz.example;
import java.util.Date;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.scheduling.quartz.QuartzJobBean;
//public class HelloJob implements Job {
public class HelloJob extends QuartzJobBean {
private static Log _log = LogFactory.getLog(HelloJob.class);
private JobLauncher launcher;
private Job job;
private int count = 0;
public void setLauncher(JobLauncher launcher) {
this.launcher = launcher;
}
public void setJob(Job job) {
this.job = job;
}
public HelloJob() {
}
protected void executeInternal(JobExecutionContext context) {
_log.info("<<<<<<<< " + " Hello World! - " + new Date() + " >>>>>>>>");
System.out.println("aaaa");
JobParametersBuilder jobParameterBulider = new JobParametersBuilder();
jobParameterBulider.addDate("date", new Date());
try {
JobExecution jobExecution = launcher.run(job, jobParameterBulider.toJobParameters());
for(Iterator<StepExecution> iterator =
jobExecution.getStepExecutions().iterator(); iterator.hasNext();) {
StepExecution stepExecution = iterator.next();
}
} catch (JobExecutionAlreadyRunningException e) {
e.printStackTrace();
} catch (JobRestartException e) {
e.printStackTrace();
} catch (JobInstanceAlreadyCompleteException e) {
e.printStackTrace();
}
}
}
- Total
- Today
- Yesterday
- 동경
- Spring
- flex
- Delphi
- KOBA
- 송주경
- ble
- MySQL
- JavaScript
- 튜닝쇼 2008
- sas2009
- oracle
- ubuntu
- Java
- Linux
- Xcode
- SAS
- BPI-M4
- ffmpeg
- koba2010
- Delphi Tip
- 지스타2007
- android
- 서울오토살롱
- Mac
- Spring MVC
- 레이싱모델 익스트림 포토 페스티벌
- 일본여행
- 전예희
- NDK
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |