quartz框架之spring整合
来源:互联网 发布:复杂网络建模基本概念 编辑:程序博客网 时间:2024/06/05 14:45
**quartz框架之spring整合**
1.创建maven工程
2.导入jar包(pom.xml)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<dependencies>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<port>9003</port>
</configuration>
</plugin>
</plugins>
</build>
3.创建xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<web-app xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns=
"http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation=
"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id=
"WebApp_ID"
version=
"2.5"
>
<!
-- spring配置文件位置 -->
<context-param>
<param-
name
>contextConfigLocation</param-
name
>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!
-- spring核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>
index
.html</welcome-file>
<welcome-file>
index
.htm</welcome-file>
<welcome-file>
index
.jsp</welcome-file>
<welcome-file>
default
.html</welcome-file>
<welcome-file>
default
.htm</welcome-file>
<welcome-file>
default
.jsp</welcome-file>
</welcome-file-list>
</web-app>
4.创建applicationContext.xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "
>
<context:component-scan base-package=
"com.baidu"
/>
<!
-- job -->
<bean id=
"helloJob"
class=
"org.springframework.scheduling.quartz.JobDetailFactoryBean"
> <!
--不能使用ref-->
<property
name
=
"jobClass"
value=
"com.baidu.quartz.HelloJob"
/>
</bean>
<!
-- trigger -->
<bean id=
"simpleTrigger"
class=
"org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"
>
<property
name
=
"jobDetail"
ref=
"helloJob"
/>
<!
-- 3秒后第一次执行 -->
<property
name
=
"startDelay"
value=
"3000"
/>
<!
-- 5秒后重复执行 -->
<property
name
=
"repeatInterval"
value=
"5000"
/>
</bean>
<!
-- scheduler -->
<bean class=
"org.springframework.scheduling.quartz.SchedulerFactoryBean"
>
<property
name
=
"triggers"
>
<list>
<ref bean=
"simpleTrigger"
/>
</list>
</property>
</bean>
</beans>
5.编写代码
创建一个service
1
2
3
4
5
6
@Component
public
class HelloService {
public
void sayHello(){
System.
out
.println(
"say hello"
);
}
}
方法一:
创建helloJob
1
2
3
4
5
6
7
8
9
10
11
@Component
public
class HelloJob implements Job {
@Autowired
private HelloService helloService;
public
void
execute
(JobExecutionContext context)
throws JobExecutionException {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
helloService.sayHello();
System.
out
.println(
"test01"
);
}
}
方法二:
创建一个JobFactory类
1
2
3
4
5
6
7
8
9
10
11
12
@Component
public
class JobFactory extends AdaptableJobFactory{
@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle)
throws Exception {
Object jobInstance = super.createJobInstance(bundle);
autowireCapableBeanFactory.autowireBean(jobInstance);
return
jobInstance;
}
}
修改applicationContext.xml文件
1
2
3
4
5
6
7
8
9
<!
-- scheduler -->
<bean class=
"org.springframework.scheduling.quartz.SchedulerFactoryBean"
>
<property
name
=
"jobFactory"
ref=
"jobFactory"
></property>
<property
name
=
"triggers"
>
<list>
<ref bean=
"simpleTrigger"
/>
</list>
</property>
</bean>
产生空指针异常
产生的原因:
quartz交给spring管理时,底层对于jobclass的管理并没有交给spring,这样在使用spring管理时会产生连个对象,
阅读全文
0 0
- quartz框架之spring整合
- spring quartz框架整合
- 【淘淘】Spring整合Quartz框架
- quartz框架和spring整合
- Spring整合Quartz框架定时任务
- spring整合quartz框架定时任务实战
- spring之spring与Quartz整合(二)
- Spring+Quartz整合(2)之SimpleTrigger
- Spring之十一 整合quartz触发器
- Spring-----Spring整合Quartz
- Spring整合定时任务调度框架Quartz实战
- Spring 整合 Quartz 任务调度框架学习笔记
- Spring整合定时任务调度框架Quartz实
- Quartz任务调度框架和Spring的整合使用
- ssh框架整合之spring整合
- spring整合quartz
- spring整合quartz
- spring整合quartz
- vim 编辑python的一些设置
- Labview-运行崩溃,报错AccessViolation
- (二十)线程
- 快速排序和归并排序
- Android 设备唯一标识符之 MAC 地址
- quartz框架之spring整合
- 分配问题与匈牙利法
- Struts2 入门
- leetcode在线编程---树(java)
- Studio One 3.5.4 for Mac(音乐制作软件)附注册机 V3.5.4中文破解版
- Sublime Text 3 装了Anaconda 写Python代码出现框框的解决办法
- Ubuntu14.04系统10分钟搭建JavaWeb常用运行环境(jdk+tomcat+mysql+phpmyadmin)
- 推荐系统中的矩阵分解
- 数据结构实验之查找三:树的种类统计