文章标题
来源:互联网 发布:中国鸡蛋市场数据 编辑:程序博客网 时间:2024/06/05 10:07
spring 4.1.8+quartz 2.1.4 环境搭建
1.实体类:
StartQuartz.java
public class StartQuartz { public static void main(String[] args) throws Exception { AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-quartz.xml"); }}
QuartzTest.java
@Component("quartzTest") public class QuartzTest { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public void targetMethod(){ System.out.println("QuartzTest 成功打印:"+sdf.format(new Date())); }}
MyBean .java
@Component("myBean") public class MyBean { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); public void printMessage() { System.out.println("MyBean 成功打印:"+sdf.format(new Date())); } }
StartQuartz .java
public class StartQuartz { public static void main(String[] args) throws Exception { AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-quartz.xml"); }}
2.spring-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" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd "> <!-- 配置自动扫描 --> <context:component-scan base-package="com.yyc.ssm.quartz" /> <!-- 通过methodInvokingJobDetailFactory 来实现目标方法,这是QuarztTest.java类的配置信息 --> <bean id="simpleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="quartzTest" /> <property name="targetMethod" value="targetMethod" /> <!-- 配置为false 作用: 当上一下任务未执行完成,下一个任务来临的时候,下一个任务暂时等待,等上一个任务执行完成后再执行方法 --> <property name="concurrent" value="false" /> </bean> <!-- 配置定时表达器--> <bean id="taskTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean" > <property name="jobDetail" ref="simpleJobDetail" /> <!-- 配置执行的时间--> <property name="cronExpression" value="0/5 * * * * ?" /> </bean> <!-- mybean的 属性配置--> <bean id="mybeanMthod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="myBean" /> <property name="targetMethod" value="printMessage" /> <property name="concurrent" value="false" /> </bean> <!-- mybean 配置定时表达器--> <bean id="mybeanTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean" > <property name="jobDetail" ref="mybeanMthod" /> <!-- 配置执行的时间--> <property name="cronExpression" value="0/2 * * * * ?" /> </bean> <!-- 配置任务调度器 --> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="taskTrigger"/> <ref bean="mybeanTrigger"/> </list> </property> </bean></beans>
说明:可能出现的情况
1. 版本问题,看好版本,目前配置的是spring4.+quartz 2. 版本的,低版本的配置有不一样的地方。CronTriggerFactoryBean在spring3.*版本是不一样的。
阅读全文
0 0
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- jq ajax两个令人费解的参数
- 线程间各种操作
- altera小实验——ROM读取
- H5 video控件视频加载控制类
- 获取本地视频缩略图产生内存警告的问题
- 文章标题
- java数据结构与算法之平衡二叉树(AVL树)的设计与实现
- 屏幕适配autolayout_android
- iOS苹果个人开发者账号购买流程 2017版
- 25匹马赛跑
- 关于服务端访问报错后直接跳转到错误页而不显示返回信息问题即 <customErrors> 标记的“mode”属性设置为“Off”的问题的解决方案
- WinSocket实现的服务端与客户端的通信
- 20170706大数据工作
- ubuntu16.04怎么设置宽带连接