Where The Streets Have No Name

Spring Job Scheduling 서비스 본문

Developement/Java

Spring Job Scheduling 서비스

highheat 2011. 10. 20. 11:01
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="jobDetailBean"
		class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass" value="egovframework.rte.job.SayHelloJob" />
		<property name="jobDataAsMap">
			<map>
				<entry key="name" value="JobDetail"/>
			</map>
		</property>
  	</bean>

	<bean id="cronJob" class="org.springframework.scheduling.quartz.CronTriggerBean">
	  	<property name="jobDetail" ref="jobDetailBean" />
	   	<property name="cronExpression" value="*/10 * * * * ?" />
	</bean>
	
	<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>		
				<ref bean="cronJob" />
			</list>
		</property>
	</bean>
</beans>
package egovframework.rte.job;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Service;

public class SayHelloJob extends QuartzJobBean  {

	private String name;
	 
	public void setName (String name) {
		this.name = name;	
	}
	
	@Override
	protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
		long now = System.currentTimeMillis();

		SimpleDateFormat sdfNow = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strNow = sdfNow.format(new Date(now));

		System.out.println("Hello, " + name + ", " + strNow);
	}
}