SPRING:定时器
程序员文章站
2022-06-09 10:48:41
...
SPRING:定时器
项目结构
applicationContext.xml
<?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">
<description>Spring公共配置文件</description>
<import resource="classpath:config/spring/applicationContext_service.xml"/>
<import resource="classpath:config/spring/applicationContext_quartz.xml"/>
</beans>
applicationContext_service.xml
<?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">
<description>添加spring的注解配置,省去大部分xml来配置和注入bean</description>
<context:component-scan base-package="spring.quartz.job" />
</beans>
applicationContext_quartz.xml
<?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">
<description>quartz配置文件</description>
<!-- spring3.2.4 集成quartz2.2配置 begin -->
<!-- 管理触发器,就相当于在java中使用Quartz代码中的调度器scheduler -->
<!-- 管理触发器,将lazy-init='false'那么容器启动就会执行调度程序 -->
<!-- 任务调度器生成器,Spring生成具有指定属性调度器对象 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="true">
<!-- 生成的调度器所管理的所有触发器 -->
<property name="triggers">
<list>
<ref bean="sendBirthSmsTaskTrigger"/>
</list>
</property>
</bean>
<!-- 触发器生成器类,用被指定的调度器调度生成指定规则的触发器对象 -->
<bean id="sendBirthSmsTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!-- 触发后要做的操作,这个操作由spring管理,只需要指定一个MethodInvokingJobDetailFactoryBean -->
<property name="jobDetail">
<ref bean="sendBirthSms" />
</property>
<!-- 触发规则,这里的触发规则是每隔1秒钟后触发一次 -->
<property name="cronExpression">
<value>*/1 * * * * ?</value>
</property>
</bean>
<!--任务生成器,由spring管理,只需要指定其对应的对象和方法即可,任务的执行即是某个对象调用一个方法 -->
<bean id="sendBirthSms" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 指定该任务对应的调用对象,这个对象所属的类无需实现任何接口-->
<property name="targetObject" ref="sendSmsBirth" />
<!-- 指定在targetObject对象中某个的方法 -->
<property name="targetMethod" value="executeTask" />
<!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
<property name="concurrent" value="false" />
</bean>
</beans>
SendSmsBirthJob.java
package spring.quartz.job;
import org.springframework.stereotype.Component;
@Component(value = "sendSmsBirth")
public class SendSmsBirthJob {
public void executeTask() {
System.out.println("SendSmsBirthJob.executeTask()");
}
}
SendThreadServer.java
package spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SendThreadServer {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext(
"classpath:config/spring/applicationContext.xml");
}
}
运行结果
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/gitworkspaces/springQuartzTest/lib/slf4j-log4j12-1.7.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/gitworkspaces/springQuartzTest/lib/slf4j-simple-1.7.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
SendSmsBirthJob.executeTask(): Thu Jul 13 15:14:31 CST 2017
SendSmsBirthJob.executeTask(): Thu Jul 13 15:14:32 CST 2017
SendSmsBirthJob.executeTask(): Thu Jul 13 15:14:33 CST 2017
SendSmsBirthJob.executeTask(): Thu Jul 13 15:14:34 CST 2017
SendSmsBirthJob.executeTask(): Thu Jul 13 15:14:35 CST 2017
SendSmsBirthJob.executeTask(): Thu Jul 13 15:14:36 CST 2017
SendSmsBirthJob.executeTask(): Thu Jul 13 15:14:37 CST 2017
SendSmsBirthJob.executeTask(): Thu Jul 13 15:14:38 CST 2017
SendSmsBirthJob.executeTask(): Thu Jul 13 15:14:39 CST 2017
上一篇: 程序人生、钓鱼人生—我的三年程序经验分享
下一篇: eclipse删除空行