1. 程式人生 > >spring配置簡單的job

spring配置簡單的job

news uart port edi eth 多少 lfa ng- job

第一步引包

<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>2.5.4</version>
</dependency>

第二步在spring.xml配置文件中加入
<!--配置需要啟動的job 可以多個-->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
scope="singleton">
<property name="triggers">
<list>
<!-- <ref bean="newsRbLogDetailCallTrgger"></ref>-->
<ref bean="newsTestDetailCallTrgger"></ref>
</list>
</property>
</bean>

<!--簡單的job-->
<bean id="testJob" class="cn.net.withub.monitor.job.testJob" scope="singleton"/><!--包名以及類名-->
<!--測試job 開始-->
<bean id="newsRbLogDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"
scope="singleton">
<property name="targetObject">
<ref bean="testJob" /><!--bean的id-->
</property>
<property name="targetMethod">
<value>test</value>
</property>
<property name="concurrent" value="false"/>
</bean>
<bean id="newsRbLogDetailCallTrgger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail">
<ref bean="newsRbLogDetail" />
</property>
<property name="startDelay">
<value>10000</value>
</property>
<property name="repeatInterval">
<value>1000</value> <!--多少毫秒執行任務-->
</property>
</bean>
<!--測試job 結束-->

第三部寫類與方法
public class testJob {
public void test(){
System.out.println("job開始啟動");
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String sj = sdf.format(date);
System.out.println(sj);
}

public void testRedisjob(){
System.out.println("job開始啟動");
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sj = sdf.format(date);
System.out.println(sj);
}
}
 
 

spring配置簡單的job