java web开发常用5种自启动定时任务方式及区别

     首先把用到的四个类贴上来

   

public class CronTriggerTest
{
    /**
     * 以method invoke方式执行定时任务
     */
    public void sayHello()
    {
        System.out.println("方法1:methodinvoke start");
    }

}

public class JobQuartzTest extends QuartzJobBean
{
    private int timeout;

    public int getTimeout()
    {
        return timeout;
    }

    public void setTimeout(int timeout)
    {
        this.timeout = timeout;
    }

    /**
     * 以extends QuartzJobBean方式执行定时任务,企业开发中常用,因为可以注入上面timout,service等参数
     */
    @Override
    protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException
    {
        System.out.println("方法2:QuartzJobBean start;timerout value="+timeout);
    }

}

public class jobTest implements Job
{
    private int timeout;

    public int getTimeout()
    {
        return timeout;
    }

    public void setTimeout(int timeout)
    {
        this.timeout = timeout;
    }

    /**
     * 以implements job方式执行定时任务,不支持上面的属性注入,与implents quartzJobBean就这一点差别
     */
    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException
    {
        System.out.println("方法3: job test timout=" + timeout);
    }

}

public class TimerTaskTest extends TimerTask
{

    /**
     * 最简单的以timertask定时任务方式
     */
    @Override
    public void run()
    {
     System.out.println("方法4:timer task start");
    }

}

   我在spring配置文件中配置上面所有的

  



	
	
		
		
			
				
			
		
	


	
		
			
		
		
	
	

	
	
		
		
			
				
			
		
	


	
		
			
		
		
	
	


	
	
	
		
			
		
		
			sayHello
		
	

	
		
			
		
		
	
	

	


	

	
	
		
			
				
				

				
				

				
				
			
		
	
	



	
	

	
		
			
		
		
			1000
		
		
			120000
		
	

	
	
		
			
				
			
		
	
	

  执行结果:

 

方法1:methodinvoke start
方法2:QuartzJobBean start;timerout value=10
方法3: job test timout=0
方法4:timer task start

另外还有一种是通过在web.xml中配置的,如下:

public class webForTimerTask extends TimerTask
{
    private ServletContext context = null;

    @Override
    public void run()
    {
        System.out.println("web.xml 中执行timer task");
    }

    public webForTimerTask(ServletContext context)
    {
        this.context = context;
    }

}

 
public class WebTimerTaskTestListener implements ServletContextListener
{
    private Timer timer = null;

    public Timer getTimer()
    {
        return timer;
    }

    public void setTimer(Timer timer)
    {
        this.timer = timer;
    }

    @Override
    public void contextDestroyed(ServletContextEvent event)
    {
        timer.cancel();
        event.getServletContext().log("定时器销毁");
    }

    @Override
    public void contextInitialized(ServletContextEvent event)
    {
        timer = new Timer(true);
        event.getServletContext().log("定时器已启动");// 添加日志,可在tomcat日志中查看到
        timer.schedule(new webForTimerTask(event.getServletContext()), 0, 5 * 1000);// 调用
        // exportHistoryBean,0表示任务无延迟,5*1000表示每隔5秒执行任务
    }

}

web.xml中加入上面的监听类


	  	
		
			com.chenjun.WebTimerTaskTestListener
		
	

  执行结果:

2014-12-27 22:17:01 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/16  config=null
2014-12-27 22:17:01 org.apache.catalina.startup.Catalina start
信息: Server startup in 7308 ms
web.xml 中执行timer task
web.xml 中执行timer task
web.xml 中执行timer task

你可能感兴趣的:(======ssh,=======java,=======j2ee)