Struts 2 + Spring 3 + Quartz 1.8 Scheduler示例-程序员宅基地

技术标签: java  开发工具  web.xml  

在本教程中,我们将向您展示如何集成Struts 2 + Spring 3 + Quartz 1.8.6

为什么不石英2?
当前,Spring 3仍不支持Quartz 2 API,请参阅此SPR-8581错误报告 。 修正了错误后,将再次更新本文。

使用的工具

  1. 春天3.1.5。发布
  2. 石英1.8.6
  3. Struts2.3.4
  4. Struts2-spring-plugin 2.3.4
  5. Maven 3
  6. Eclipse 4.2

1.项目文件夹

这是最终的项目文件夹结构。

2.依赖库

集成与该Spring 2.5.6 + Quartz 1.6相似,只是Spring3的依赖项有些混乱,请阅读下面的Maven pom.xml,了解开发本教程所需的所有依赖项。

请参阅XML注释,以了解为什么需要该jar。

档案:pom.xml

...
<dependencies>

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

  <!-- Quartz framework -->
  <dependency>
	<groupId>org.quartz-scheduler</groupId>
	<artifactId>quartz</artifactId>
	<version>1.8.6</version>
  </dependency>

  <!-- Spring 3 dependencies -->
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>3.1.2.RELEASE</version>
  </dependency>

  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>3.1.2.RELEASE</version>
  </dependency>

  <!-- QuartzJobBean in spring-context-support.jar -->
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context-support</artifactId>
	<version>3.1.2.RELEASE</version>
  </dependency>

  <!-- Struts 2 + Spring 3 need this jar, ContextLoaderListener -->
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-web</artifactId>
	<version>3.1.2.RELEASE</version>
  </dependency>

  <!-- Spring + Quartz need transaction -->
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-tx</artifactId>
	<version>3.1.2.RELEASE</version>
  </dependency>

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

</dependencies>
  ...

3.Spring3 +石英

为了集成Spring3和Quartz,创建一个类扩展org.springframework.scheduling.quartz.QuartzJobBean ,并实现executeInternal()方法,就像在Quartz中创建调度程序作业一样。

文件: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
{
	 
	protected void executeInternal(JobExecutionContext context)
	throws JobExecutionException {
 
		System.out.println("Struts 2.3.4 + Quartz 1.8.6 + Spring 3.1.2");
 
	}
}

File:applicationContext.xml –将整个Quartz的调度程序和Spring详细信息放在applicationContext.xml 。 有关详细信息,请参见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-3.0.xsd">

  <!-- Spring Quartz Scheduler job -->
  <bean name="schedulerJob" class="org.springframework.scheduling.quartz.JobDetailBean">
	<property name="jobClass" value="com.mkyong.quartz.SchedulerJob" />
  </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>

  <!-- DI -->
  <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>

4.Struts2 +弹簧3

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

注意
有关详细信息,请阅读此Struts 2 + Spring集成示例

档案: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>

5.演示

完成后,启动Strut2时,它将调用Spring并运行定义的Quartz的作业。

Jul 24, 2012 4:49:07 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jul 24, 2012 4:49:07 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jul 24, 2012 4:49:07 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 3430 ms
Struts 2.3.4 + Quartz 1.8.6 + Spring 3.1.2  //run this every 10 seconds.
Struts 2.3.4 + Quartz 1.8.6 + Spring 3.1.2
Struts 2.3.4 + Quartz 1.8.6 + Spring 3.1.2

下载源代码

下载它– Struts2-Spring3-Quartz-Example.zip (20 KB)。

参考文献

  1. Struts 2 + Spring集成示例
  2. Struts 2 Spring插件文档
  3. QuartzJobBean JavaDoc
  4. Spring应该支持Quartz 2.0 CronTrigger接口
  5. Struts 2 + Spring 2.5.6 + Quartz集成示例

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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/cyan20115/article/details/106552304

智能推荐

git排除某文件的提交_sourcetree .git 强制忽略指定文件不提交-程序员宅基地

文章浏览阅读435次。在公司写项目,大部分都会用到 svn 或 git 提交代码到服务器。我们公司用的GIT,每个程序员有自己的独立分支,各写各的代码互不冲突,最终合并到主分支再解决相同代码冲突问题。这时候会遇到一些配置文件提交的问题,每个程序员在自己的电脑都有自己的环境,每个环境配置各不相同,这样导致提交代码的时候都把自己的配置文件一起提交上去,这样每次提交都需要单独合并解决配置问题的冲突,对于更新平凡的项目来说很麻..._sourcetree里可以忽略某些提交吗?

JedisPool的testOnBorrow、testOnReturn和testWhileIdle参数的实现原理_jedis testonborrow-程序员宅基地

文章浏览阅读8.2k次,点赞6次,收藏13次。前言我们平时使用jedispool来连接Redis的集群、sentinel或者主从服务器,经常会遇到testOnBorrow、testOnReturn和testWhileIdle这些参数的设置问题,我们知道连接Redis服务器的连接是维护在通用对象池中的,如果想要正确的、符合自己业务场景的设置这些参数,需要了解其底层原理。Jedispool和genericObjectPool的关系我们翻到j..._jedis testonborrow

【STM32】 4X4矩阵键盘电路_stm32 4x4矩阵键盘-程序员宅基地

文章浏览阅读7.1k次,点赞4次,收藏33次。【STM32】 4X4矩阵键盘电路_stm32 4x4矩阵键盘

java中下拉框select和单选按钮的回显_"<select name=\"cid\"> <option value=\"0\">请选择</op-程序员宅基地

文章浏览阅读1.9k次。前提:&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;1.下拉框select&lt;select name="departmentId" id="departmentId"&gt; &lt;option value="0"&gt;请选择部门&_" 请选择

ORACLE---Unit04: SQL(高级查询)-程序员宅基地

文章浏览阅读94次。---(重点复习:子查询、分页查询、decode)--- SQL(高级查询)--- 子查询--- 子查询是嵌套在其它SQL语句当中的,目的是为嵌套的SQL提供数据,以便其执行。---查看谁的工资高于CLARK?---1.先查CLARK的工资SELECT sal FROM emp_RR WHERE ename='CLARK';--->2450---2.工资高于CLARK的员工信息SEL..._"在子查询依赖于管道,其中一个不在where子句中,启用:set enable_pipeline=true;\","

TP5.1 搜索功能分页传参_tp5.1 分页传参前台-程序员宅基地

文章浏览阅读367次。1.html代码<form class="form-horizontal" action="{:url('admin/AuthRoles/lst')}" method="get"><div class="input-group input-group-sm"> <input type="text" id="search" name="search" styl..._tp5.1 分页传参前台

随便推点

Linux救援模式启动ssh服务,Linux服务器的紧急救援模式RescueLayer是什么?-程序员宅基地

文章浏览阅读941次。在某些情况下, 系统无法启动或系统崩溃,我们只能通过重装系统来处理. 但极少数高端机房, 能提供Linux服务器的紧急救援模式RescueLayer, 让你可以ssh进入服务器去尝试修复. 这里我们只是给出机房对RescueLayer的介绍, 便于参考. 我们并不提供RescueLayer的任何技术支持. 进入RescueLayer后的任何操作都需要您自己完成.The RescueLayer Ke..._rescue kernel

[cernRoot] how to get contours from a TH2D_cern root th2d-程序员宅基地

文章浏览阅读567次。here, i use a TMultiGraph to save the gotten contours, because the contours may be not closed in a TH2D, with the function TMultiGraph * GetContours(const char * fn, const char * th2dname, int nlevel)._cern root th2d

erdas正射校正、数据融合、影像镶嵌_erdas imagine制作dtm和dom-程序员宅基地

文章浏览阅读1.2w次,点赞15次,收藏104次。在几个传统影像处理软件中,erdas的处理速度往往是最快的(比起ENVI、argis),而且img格式稳定,不易变化,个人使用首推erdas,当然大规模生产的话还是任务订单式的GXL好,它在批处理的路上走得更远,以web方式提交任务,以集群方式处理数据,高并发的处理能力,估计很多传统做影像处理的人要失业,扯远了。下面是望神州公司的一个erdas操作教程,本人无意抄袭,只想给正在用ERDAS的人..._erdas imagine制作dtm和dom

idea连接数据库 The specified database user/password combination is rejected: com.mysql.cj报错_2021版idea连接阿里云mysql出现the specified database user/p-程序员宅基地

文章浏览阅读1.2w次,点赞4次,收藏3次。报错如下图:解决方法:jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC转载自:https://blog.csdn.net/weixin_39938635/article/details/90212775.._2021版idea连接阿里云mysql出现the specified database user/password combinatio

linux服务器安装anaconda总结_home/jovyan在哪里-程序员宅基地

文章浏览阅读701次。一、官网下载linux版安装包https://repo.anaconda.com/archive/Anaconda3-2019.10-Linux-x86_64.sh二、上传并进行安装将安装包拷贝至服务器,使用cd命令进入安装包所在位置,比如我拷到的是/home下面,就是cd /home。然后使用bash进行安装。cd /homebash Anaconda3-2019.10-Linux-x86_64.sh按照要求输入yes同意阅读协议,并一直按住enter键翻到底开始安装.._home/jovyan在哪里

C#并行编程高级教程:精通.NET 4 Parallel Extensions_.net4 parallel extension-程序员宅基地

文章浏览阅读5.5k次。基本信息原书名: Professional Parallel Programming with C#: Master Parallel Extensions with .NET 4原出版社: Wrox 作者: (美)Gaston Hillar [作译者介绍] 译者: 郑思遥 房佩慈 出版社:清华大学出版社 ISBN:9787302273561上架时间:2012-1_.net4 parallel extension

推荐文章

热门文章

相关标签