zoukankan      html  css  js  c++  java
  • 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");    
        }
    }

    结果

  • 相关阅读:
    java中的 equals 与 ==
    String类的内存分配
    SVN用命令行更换本地副本IP地址
    npoi 设置单元格格式
    net core 微服务框架 Viper 调用链路追踪
    打不死的小强 .net core 微服务 快速开发框架 Viper 限流
    net core 微服务 快速开发框架 Viper 初体验20201017
    Anno 框架 增加缓存、限流策略、事件总线、支持 thrift grpc 作为底层传输
    net core 微服务 快速开发框架
    Viper 微服务框架 编写一个hello world 插件02
  • 原文地址:https://www.cnblogs.com/crazylqy/p/4220795.html
Copyright © 2011-2022 走看看