Quartz配置简介
Quartz是一个开源的作业调度框架,它完全由Java写成,并设计用于J2SE和J2EE应用中。它提供了巨大的灵活性而不牺牲简单性。你能够用它来为执行一个作业而创建简单的或复杂的调度。本系统结合通过Spring来集成Quartz。
Quartz官方网址:http://www.opensymphony.com/quartz
基本概念:
1. Job:Job是Quartz中的一个接口,顾名思义,它指定了我们需要执行的任务。下面是一个简单的Job实现:
java代码:
package quartz;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class DumbJob implements Job {
private int x;
public void execute(JobExecutionContext context) throws JobExecutionException {
try {
System.out.println(“Executing…” + x);
} catch (RuntimeException e) {
JobExecutionException e2 = new JobExecutionException(e);
…
Spring的MessageSource有两个常用的实现ReloadableResourceBundleMessageSource和ResourceBundleMessageSource。这两个类在配置上有些区别。
我原来常用ResourceBundleMessageSource,它的典型配置如下:
<bean id=”messageSource”
class=”org.springframework.context.support.ReloadableResourceBundleMessageSource“>
<property name=”parentMessageSource” ref=”bizMessageSource”/>
<property name=”basenames”>
<list>
<value>resources.cls-web-resources</value>
<value>resources.cls-web-resources-definitions</value>
<value>resources.cls-web-resources-menu</value>
</list>
</property>
</bean>
在比较一下ReloadableResourceBundleMessageSource的配置:
<bean id=”messageSource”
class=”org.springframework.context.support.ReloadableResourceBundleMessageSource“>
…
昨天和老婆又跑去游乐园玩了一圈。好久没去过游乐园了,发现里面还是多了很多新的项目,而且人也挺多。
最先去坐碰碰车,还是这个经典,而且也便宜,在5元一个人,刚上去都忘了怎么开了,撞的high啊。
然后老婆去坐翻滚列车,kao,我这次没去坐,想起上次坐的,还是又点吓人,20一人,我老婆自己去坐了一圈,下来直乱叫,hehe。
下面去玩激流探险,10元一个,还是挺好耍,不过就是太短了,还没过瘾就没了,而且把我屁股打湿了………….
最后,还是又去碰碰车,hehe,好耍哦。
今天把系统的Hibernate版本从3.05升级到了3.1.3,结果运行时出现ClassCastException。检查发现新版本的hibernate增强了在执行HQL操作时的传入的参数的检查(Binding Parameters)。比如HQL语句中有from xxx where beginTime >= :beginTime and endTime < :endTime,在原来的程序中,我直接把beginTime和endTime都已经直接转换到了timestamp格式的String类型,这样的操作在3.0.5版本时并没有任何错误,但在升级版本后,Hibernate会检查传入的beginTime和endTime必须是Calendar类型(或者是Date类型,看你Hibernate类型的配置),结果导致在运行时出现ClassCastException。
在Spring和Struts结合使用的环境下,一般使用org.springframework.web.struts.DelegatingRequestProcessor来把request通过Spring委托struts处理,这样同时可以获得Spring的IoC带来的方便。
比如:
Struts配置文件:
<action
path=”/SettlementAction”
type=”com.tmca.cls.admin.clearing.ui.struts.SettlementAction”
name=”SettlementForm”
scope=”request”
input=”/WEB-INF/jsp/pages/admin/clearing/settlement.jsp”
unknown=”false”
validate=”true”
>
</action>
Spring配置:
<bean
name=”/SettlementAction”
class=”com.tmca.cls.admin.clearing.ui.struts.SettlementAction”
>
…
</bean>
Spring会去在Struts中调用同名的Action,上面“/SettlementAction”首先被Spring配置,然后具体的request处理交给Struts的“/SettlementAction”这个Action。
但是这里有一个很严重的限制,本人现在还没有解决方案:Spring中“/SettlementAction”必须是Struts的Action类型。比如如果“com.tmca.cls.admin.clearing.ui.struts.SettlementAction”实现了Spring的“ApplicationListener”,或者被Proxy封装了,那么Spring会在运行时报错说该Bean不是一个Action类型的,而是一个“Proxy”类型的。也就是说,Spring中需要参与Struts的Action处理的类外围不能添加任何封装,它的instanceof Action结果必须返回True才能被Spring认可。
Spring本身有ApplicationEvent和ApplicationListener,ApplicationContext可以发布ApplicationEvent,然后ApplicationListener监听event并做出相应动作。但是这里的ApplicationEvent有个陷阱,它的传播范围和当前的ApplicationContext的级别有关,并不是系统中所有的ApplicationListener都可以收到所有的Event。
假设当前系统为一个典型的Struts+Spring+Hibernate系统,那么系统中至少会有两个ApplicationContext存在,一个时root ApplicationContext,一个是Servlet的ApplicationContext。root ApplicationContext中包含你所有在webApplicationContext.xml中定义的bean,Servlet的ApplicationContext则包含有所有在action-servlet.xml中定义的bean,需要注意的是root context中的bean是无法看到servlet context中的bean的。而在servlet context中的ApplicationListener也无法收到root context发布的ApplicationEvent。
问题出处:http://forum.javaeye.com/viewtopic.php?p=123790#123790
昨天我也遇到类似楼主说的Spring找不到被调用method的情况,把我吓了一跳:因为昨天运行的时候都没有出现这个问题。检查发现我在被调用的函数多加了一个参数,觉得可能是这个引起的问题。我查看了Spring的MethodInvoker的源代码,验证了我的想法。
java代码:
public void prepare() throws ClassNotFoundException, NoSuchMethodException {
…
if (this.arguments == null) {
this.arguments = new Object[0];
}
Class[] argTypes = new Class[this.arguments.length];
for (int i = 0; i < this.arguments.length; ++i) {
…