Struts 2 + Spring 2.5.6 + Quartz 1.6调度程序集成示例

Struts 2 + Spring 2.5.6 + Quartz Scheduler集成示例

在本教程中,我们将向您展示如何将Struts 2 + Spring 2.5.6 + Quartz 1.6.5调度程序集成在一起。 关系看起来像这样:

Struts 2 <-- Plugin --> Spring <--(Helper)--> Quartz <---> Scheduler task

使用的工具

  1. Spring2.5.6
  2. 石英1.6.3
  3. Struts2.1.8
  4. Struts2-spring-plugin 2.1.8
  5. Maven 2
  6. Eclipse 3.6

注意
您可能也对此感兴趣-Struts 2 + Spring 3 + Quartz 1.8.6集成示例。

1.项目文件夹

这是项目文件夹的结构。

Struts 2 Spring Quartz integration example

2.依赖库

获取所有依赖库,您需要Spring,Struts2,Strut2-Spring-Plugin和Quartz jar文件。

档案:pom.xml

...
  <dependencies>

	<!-- Struts 2 -->
	<dependency>
          <groupId>org.apache.struts</groupId>
	  <artifactId>struts2-core</artifactId>
	  <version>2.1.8</version>
        </dependency>

	<!-- Quartz framework -->
	<dependency>
          <groupId>opensymphony</groupId>
	  <artifactId>quartz</artifactId>
	  <version>1.6.3</version>
	</dependency>
	 
	<!-- Quartz dependency library-->
	<dependency>
	  <groupId>commons-collections</groupId>
	  <artifactId>commons-collections</artifactId>
	  <version>3.2.1</version>
	</dependency>

	<!-- Spring framework --> 
	<dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring</artifactId>
	  <version>2.5.6</version>
	</dependency>

	<dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-web</artifactId>
	  <version>2.5.6</version>
	</dependency>

	<!-- Struts 2 + Spring plugins -->
	<dependency>
          <groupId>org.apache.struts</groupId>
	  <artifactId>struts2-spring-plugin</artifactId>
	  <version>2.1.8</version>
        </dependency>

  </dependencies>
  ...

3.计划任务

将所有调度程序逻辑放在此类中。

文件:SchedulerTask.java

package com.mkyong.quartz;
 
public class SchedulerTask {
	
   public void printSchedulerMessage() {
	
	   System.out.println("Struts 2 + Spring + Quartz ......");
	   
   }
}

4.弹簧+石英

为了集成Spring和Quartz,创建一个扩展org.springframework.scheduling.quartz.QuartzJobBean的类,通过setter方法引用调度程序任务( SchedulerTask.java ),然后将调度程序逻辑放入executeInternal()方法中。

文件:SchedulerJob.java

package com.mkyong.quartz;
 
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
 
public class SchedulerJob extends QuartzJobBean
{
	private SchedulerTask schedulerTask;
 
	public void setSchedulerTask(SchedulerTask schedulerTask) {
		this.schedulerTask = schedulerTask;
	}
 
	protected void executeInternal(JobExecutionContext context)
	throws JobExecutionException {
 
		schedulerTask.printSchedulerMessage();
 
	}
}

File:applicationContext.xml –创建一个applicationContext.xml文件,将所有Spring + Quartz集成内容放入其中。 阅读XML注释以获取详细信息。

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
   <!-- Scheduler task -->
   <bean name="schedulerTask" class="com.mkyong.quartz.SchedulerTask" />
 
   <!-- Scheduler job -->
   <bean name="schedulerJob" 
     class="org.springframework.scheduling.quartz.JobDetailBean">
 
     <property name="jobClass" value="com.mkyong.quartz.SchedulerJob" />
 
     <property name="jobDataAsMap">
	 <map>
	    <entry key="schedulerTask" value-ref="schedulerTask" />
	 </map>
      </property>
   </bean>
 
   <!-- Cron Trigger, run every 10 seconds -->
   <bean id="cronTrigger"
	class="org.springframework.scheduling.quartz.CronTriggerBean">
 
	<property name="jobDetail" ref="schedulerJob" />
	<property name="cronExpression" value="0/10 * * * * ?" />
 
   </bean>
 
   <!-- Scheduler -->
   <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
	<property name="jobDetails">
	   <list>
	      <ref bean="schedulerJob" />
	   </list>
	</property>
 
	<property name="triggers">
	    <list>
		<ref bean="cronTrigger" />
	    </list>
	</property>
   </bean>
 
</beans>

5. Struts 2 +弹簧

要集成Struts 2 + Spring ,只需将org.springframework.web.context.ContextLoaderListener侦听器类放在web.xml文件中。

档案:web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Struts 2 Web Application</display-name>
  
  <filter>
	<filter-name>struts2</filter-name>
	<filter-class>
	  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
	</filter-class>
  </filter>
  
  <filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>/*</url-pattern>
  </filter-mapping>
 
  <listener>
    <listener-class>
	  org.springframework.web.context.ContextLoaderListener
	</listener-class>
  </listener>
  
</web-app>

6.演示

启动Strut2时,它将调用Spring并运行定义的Quartz的作业–每10秒调用一次SchedulerTask.printSchedulerMessage()

INFO: ... initialized Struts-Spring integration successfully
16 Julai 2010 12:51:38 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
16 Julai 2010 12:51:38 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
16 Julai 2010 12:51:38 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/21  config=null
16 Julai 2010 12:51:38 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2110 ms
Struts 2 + Spring + Quartz ......
Struts 2 + Spring + Quartz ......
Struts 2 + Spring + Quartz ......
Struts 2 + Spring + Quartz ......
Struts 2 + Spring + Quartz ......

下载源代码

下载它– Struts2-Spring-Quartz-Integration-Example.zip

参考文献

  1. Struts 2 + Spring集成示例
  2. Struts 2 Spring插件文档
  3. Struts + Spring集成示例
  4. Struts + Quartz集成示例
  5. Struts + Spring + Quartz集成示例

翻译自: https://mkyong.com/struts2/struts-2-spring-quartz-scheduler-integration-example/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值