Spring定時任務的簡單例子
最近專案需要弄多個定時任務用爬蟲批次抓取資料,用的spring定時job功能,簡單總結下來:
package util;
import org.apache.log4j.*;
public class TaskJob {
public static Logger log = Logger
.getLogger(TaskJob.class);
public void SayHello() {
//
TODO Auto-generated method stub
try
{
log.info("處理任務開始>........");
// 業務邏輯程式碼呼叫
System.out.println("時間[" + new
java.util.Date().toLocaleString()
+ "]----->大家好啊!");
log.info("處理任務結束!");
}
catch (Exception e) {
log.error("處理任務出現異常", e);
}
}
}
2.接下來的在spring 裡配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " /> <beans>
<bean
name="taskJob" class="util.TaskJob" />
<bean
id="methodInvokingJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
package util;
import org.apache.log4j.*;
public class TaskJob {
}
2.接下來的在spring 裡配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " /> <beans>