• spring-线程池(2)


    继承:http://www.cnblogs.com/crazylqy/p/4220743.html

    spring设置容器启动时运行线程类(可循环执行)

    修改以下两文件,

    1.spring设置容器启动时运行线程类

    <?xml version="1.0" encoding="UTF-8"?>  
    <beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="  
    http://www.springframework.org/schema/beans        
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/context                
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
     
    
    <!-- 托管线程 -->
    <bean id="messagePrinterTask" class="MessagePrinterTask">    
    </bean> 
    
    <!-- 这样设置容器一启动就自动运行某个线程 start -->
     <bean id="springScheduleExecutorTask" class="org.springframework.scheduling.concurrent.ScheduledExecutorTask">  
        <property name="runnable" ref="messagePrinterTask" /><!-- messagePrinterTask为线程类 -->  
        <!-- 容器加载10秒后开始执行 -->  
        <property name="delay" value="10000" />  
        <!-- 每次任务间隔 5秒,循环执行该线程,删除该设定就容器启动后只执行一次-->  
        <property name="period" value="5000" />  
      </bean>  
        
        <bean id="springScheduledExecutorFactoryBean" class="org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean">  
        <property name="scheduledExecutorTasks">  
          <list>  
            <ref bean="springScheduleExecutorTask" />  
          </list>  
        </property>  
       </bean>  
    <!-- 这样设置容器一启动就自动运行某个线程 end -->
      
      
    </beans>


    测试运行

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    /**
     * Hello world!
     *
     */
    public class App 
    {
        public static void main( String[] args )
        {
            ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");    
        }
    }

    结果

  • 相关阅读:
    VS2005在使用membership的时候,如何连接Access数据库?
    今天想开始写计划的项目,可是就是静不下心来,乱糟糟的!
    今天想开始写计划的项目,可是就是静不下心来,乱糟糟的!
    有钱真好
    网页左边和上面的空隙如何设置成为0
    vim 配色方案(目测有上百个)
    Git 远程仓库的管理和使用
    vim 使用图
    Python 编程挑战
    python 网络爬虫
  • 原文地址:https://www.cnblogs.com/crazylqy/p/4220795.html
Copyright © 2020-2023  润新知