spring定时器详解

1,配置spring定时器,

  第一步: 引入jar



  org.quartz-scheduler
  quartz
  2.2.1

第二部: 配置web.xml



	
		contextConfigLocation
		classpath:spring-base.xml
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
		characterEncodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			UTF-8
		
		
			forceEncoding
			true
		
	
	
		characterEncodingFilter
		/*
	
	
		springmvc
		org.springframework.web.servlet.DispatcherServlet
		
		
			contextConfigLocation
			
		
		
			contextConfigLocation
			classpath:spring-mvc.xml
		
		1
		true
	
	
		springmvc
		/
	


	
	
		org.springframework.web.util.IntrospectorCleanupListener
	
	
		com.init.StartInit
	

	

		index.jsp

	

第三步: 配置spring-mvc.xml,必须在spring-mvc.xml中加上此配置


	
	

第四步: 在spring可以扫描的包下,创建定时器类

@Component
public class DataQuartz {
	
	@Autowired
	private DataService dataServise;
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private SimpleDateFormat sdfDay = new SimpleDateFormat("d");

	//	"0 0 12 * * ?" 每天中午12点触发 
	//	"0 15 10 ? * *" 每天上午10:15触发 
	//	"0 15 10 * * ?" 每天上午10:15触发 
	//	"0 15 10 * * ? *" 每天上午10:15触发 
	//	"0 15 10 * * ? 2005" 2005年的每天上午10:15触发 
	//	"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发 
	//	"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发 
	//	"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 
	//	"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发 
	//	"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发 
	//	"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发 
	//	"0 15 10 15 * ?" 每月15日上午10:15触发 
	//	"0 15 10 L * ?" 每月最后一日的上午10:15触发 
	//	"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发 
	//	"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 
	//	"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发 
	//	 每隔5秒执行一次:*/5 * * * * ?
	//	   每隔1分钟执行一次:0 */1 * * * ?
	//	 每天23点执行一次:0 0 23 * * ?
	//	   每天凌晨1点执行一次:0 0 1 * * ?
	//	   每月1号凌晨1点执行一次:0 0 1 1 * ?
	//	   每月最后一天23点执行一次:0 0 23 L * ?
	//	   每周星期天凌晨1点实行一次:0 0 1 ? * L

	@Scheduled(cron="0 0 1 * * ?")   //每天凌晨1点执行一次  
	public void writAllDataExcel() {
        //在这里面就可以常见自己的定时代码
       }
 }     

 
  
 
  
 
  

 

注:

   1,  Scheduled 是一步起线程来处理自己的业务逻辑的,所以在定时器方法中,不可以使用HttpServletRequest request和HttpServletResponse response,并且在参数中都不可以

        使用

    2, 在webxml中必须添加定时器空配置,否则会导致两次进入定时方法(在引入spring配置文件时,不要使用spring-*.xml的方式,这样会吧spring-mvc.xml重复引入,这是造成两次进 

        入定时任务的原因)

你可能感兴趣的:(框架)