timer定时器在项目启动时执行了两次的原因及其解决办法

time定时器在项目启动时执行了两次的原因及其解决办法

今天在项目的时候用到了java中的定时器计划任务。可以根据设置在特定的时间或间隔时间做特定的事。

下面给出一个例子:

        @Override
	public void afterPropertiesSet() throws Exception {
			
		Timer timer = new Timer();
		
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
			    System.out.println("定时器启动!");
			}
		};
		
		//定时器即时启动task,60分钟重新运行一次task
		timer.scheduleAtFixedRate(task, 0, 60*60*1000);
	}

web.xml配置:



	
	sitms
	
	
	  contextConfigLocation
	  classpath:spring/applicationContext-*.xml
	
	
	  log4jRefreshInterval
	  10000
	
	
	  log4jConfigLocation
	  classpath:log4j.properties
	
	
	  webAppRootKey
	  sitms-rest.root
	
	
	  org.springframework.web.util.Log4jConfigListener
	
	
	  org.springframework.web.context.ContextLoaderListener
	
	
	  org.springframework.web.util.IntrospectorCleanupListener
	
	
	  CharacterEncodingFilter
	  org.springframework.web.filter.CharacterEncodingFilter
	  
	    encoding
	    utf-8
	  
	
	
	  CharacterEncodingFilter
	  /*
	
	
	  EncodingFilter
	  com.leaflink.sitms.filter.EncodingFilter
	
	
	  EncodingFilter
	  /*
	
	
	
	  sitms
	  org.springframework.web.servlet.DispatcherServlet
	  
	    contextConfigLocation
	    classpath:spring/springmvc.xml
	  
	  1
	
	 
	  sitms
	  /rest/*
	

配置的部分就是这样,然后我使用MyEclipse关联上Tomcat服务器。一切都是默认的设置,然后将本引用部署并启动Tomcat服务器。

这时候问题来了,我的任务类居然被创建了两次,下面是截取的部分日志数据:

2018-07-11 10:22:09,644 [localhost-startStop-1] DEBUG [org.mybatis.spring.mapper.ClassPathMapperScanner] - Creating MapperFactoryBean with name 'weChatLabelMapper' and 'com.leaflink.sitms.mapper.WeChatLabelMapper' mapperInterface
2018-07-11 10:22:09,863 [localhost-startStop-1] DEBUG [org.apache.ibatis.logging.LogFactory] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
2018-07-11 10:22:10,479 [localhost-startStop-1] DEBUG [org.mybatis.spring.SqlSessionFactoryBean] - Parsed configuration file: 'class path resource [mybatis/SqlMapConfig.xml]'
2018-07-11 10:22:10,480 [localhost-startStop-1] DEBUG [org.mybatis.spring.SqlSessionFactoryBean] - Property 'mapperLocations' was not specified or no matching resources found
定时器启动!
2018-07-11 10:22:12,439 [localhost-startStop-1] INFO  [org.springframework.web.context.ContextLoader] - Root WebApplicationContext: initialization completed in 3968 ms
七月 11, 2018 10:22:12 上午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'sitms'
2018-07-11 10:22:12,505 [localhost-startStop-1] INFO  [org.springframework.web.servlet.DispatcherServlet] - FrameworkServlet 'sitms': initialization started
2018-07-11 10:22:12,508 [localhost-startStop-1] INFO  [org.springframework.web.context.support.XmlWebApplicationContext] - Refreshing WebApplicationContext for namespace 'sitms-servlet': startup date [Wed Jul 11 10:22:12 CST 2018]; parent: Root WebApplicationContext
定时器启动!

可以看到,执行了两次定时器,分别再ContextLoader和DispatcherServlet中实例化了,让我们再看一下配置文件:


    org.springframework.web.context.ContextLoaderListener


    contextConfigLocation
    classpath:spring/applicationContext-*.xml

该节点指派的applicationContext*.xml是用于实例化除servlet之外的所有对象的,可以说项目中绝大多数的service和dao层操作都由ContextLoaderListener传递给Spring来进行实例化。


    sitms
    org.springframework.web.servlet.DispatcherServlet
    
        contextConfigLocation
        classpath:spring/springmvc.xml
    
    1


    sitms
    /rest/*

这个是用来处理所有servlet的,没有它就无法通过请求地址来调用相应的Controller。

那么问题来了,有时候不注重对象初始化的分类,尤其是使用这样的包扫描形式统一初始化,很容易造成满足条件的对象被初始化两次,那么在计划任务的时候被执行两次也就不奇怪了。

所以不要将所有的初始化操作都放到一个配置文件中,更不要重复配置。不仅浪费资源,还很容易导致莫名其妙的故障。

解决方法:在配置文件中扫描包中指定扫描某个包,不要重复扫描

原先的applicationContext-dao.xml

  
  

修改后:

  
  

原先的springmvc.xml


修改后:


修改后运行结果:
2018-07-11 10:44:19,638 [localhost-startStop-1] DEBUG [org.mybatis.spring.mapper.ClassPathMapperScanner] - Creating MapperFactoryBean with name 'weChatLabelMapper' and 'com.leaflink.sitms.mapper.WeChatLabelMapper' mapperInterface
2018-07-11 10:44:19,881 [localhost-startStop-1] DEBUG [org.apache.ibatis.logging.LogFactory] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
2018-07-11 10:44:20,650 [localhost-startStop-1] DEBUG [org.mybatis.spring.SqlSessionFactoryBean] - Parsed configuration file: 'class path resource [mybatis/SqlMapConfig.xml]'
2018-07-11 10:44:20,651 [localhost-startStop-1] DEBUG [org.mybatis.spring.SqlSessionFactoryBean] - Property 'mapperLocations' was not specified or no matching resources found
定时器启动!
2018-07-11 10:44:22,106 [localhost-startStop-1] INFO  [org.springframework.web.context.ContextLoader] - Root WebApplicationContext: initialization completed in 3298 ms
如果有什么不对的地方,欢迎指出!





你可能感兴趣的:(spring,java)