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"); } }
结果