1.使用MethodInvokingJobDetailFactoryBean

 MethodInvokingJobDetailFactoryBean 所指定的任务是普通的 java类.

SimpleJob2.java

public class SimpleJob2
{
    
public void execute()
    {
        System.out.println(
"SimpleJob2");
    }
}

 

SimpleJob2.xml:

注意:targetMethod 会去找指定job类中没有参数的指定方法.如:execute(){}

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
    
<bean name="simpleJob2" class="com.commonjob.SimpleJob2"></bean>

    
<bean name="methodInvokingJobDetail"
        class
="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        
<property name="targetObject" ref="simpleJob2" />
        
<property name="targetMethod" value="execute" />
    
</bean>


    
<bean id="simpleTrigger"
        class
="org.springframework.scheduling.quartz.SimpleTriggerBean">
        
<property name="jobDetail" ref="methodInvokingJobDetail" />
        
<!-- 10 seconds -->
        
<property name="startDelay">
            
<value>2000</value>
        
</property>
        
<!-- repeat every 50 seconds -->
        
<property name="repeatInterval">
            
<value>2000</value>
        
</property>
    
</bean>

    
<bean
        
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        
<property name="triggers">
            
<list>
                
<ref local="simpleTrigger" />
            
</list>
        
</property>
    
</bean>
</beans>

 


运行:主函数加载 spring 配置文件即可:
public static void main(String[] args)
{
 ApplicationContext cxt=new FileSystemXmlApplicationContext("config/SimpleJob2.xml");
}

在web中应用 只要在web.xml中加载配置文件即可。

评论
发表评论

您还没有登录,请登录后发表评论