spring 3.0集成 quartz

最近在研究Spring中的定时任务功能,最好的办法当然是使用Quartz来实现。对于一个新手来说,花了我不少时间,这里我写个笔记,给大家参考。
我使用的是Maven来管理项目,需要的Jar包我给大家贴出来。

quartz-1.5.0-RC2.jar
commons-logging.jar
spring-core-3.0.5.RELEASE.jar
spring-beans-3.0.5.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-context-support-3.0.5.RELEASE.jar
spring-asm-3.0.5.RELEASE.jar
spring-expression-3.0.5.RELEASE.jar
spring.transaction-3.0.5.RELEASE.jar
spring-web-3.0.5.RELEASE.jar

Maven的pom.xml的配置:
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  5.     <modelVersion>4.0.0</modelVersion>  
  6.   
  7.     <groupId>QtzTest</groupId>  
  8.     <artifactId>QtzTest</artifactId>  
  9.     <version>1.0</version>  
  10.   
  11.     <properties>  
  12.         <springframework.version>3.0.5.RELEASE</springframework.version>  
  13.     </properties>  
  14.   
  15.     <dependencies>  
  16.         <dependency>  
  17.             <groupId>org.springframework</groupId>  
  18.             <artifactId>spring-context</artifactId>  
  19.             <version>${springframework.version}</version>  
  20.         </dependency>  
  21.   
  22.         <dependency>  
  23.             <groupId>org.springframework</groupId>  
  24.             <artifactId>spring-context-support</artifactId>  
  25.             <version>${springframework.version}</version>  
  26.         </dependency>  
  27.   
  28.         <dependency>  
  29.             <groupId>org.springframework</groupId>  
  30.             <artifactId>spring-tx</artifactId>  
  31.             <version>${springframework.version}</version>  
  32.         </dependency>  
  33.   
  34.         <dependency>  
  35.             <groupId>org.springframework</groupId>  
  36.             <artifactId>spring-web</artifactId>  
  37.             <version>${springframework.version}</version>  
  38.         </dependency>  
  39.   
  40.         <dependency>            <groupId>quartz</groupId>
                <artifactId>quartz</artifactId>
                <version>1.5.0-RC2</version>
  41.         </dependency>  
  42.     </dependencies>  
  43.   
  44.     <build>  
  45.         <finalName>${project.artifactId}</finalName>  
  46.         <plugins>  
  47.             <plugin>  
  48.                 <groupId>org.mortbay.jetty</groupId>  
  49.                 <artifactId>jetty-maven-plugin</artifactId>  
  50.                 <version>7.5.4.v20111024</version>  
  51.                 <configuration>  
  52.                     <scanIntervalSeconds>10</scanIntervalSeconds>  
  53.                     <webApp>  
  54.                         <contextPath>/${project.artifactId}</contextPath>  
  55.                     </webApp>  
  56.                 </configuration>  
  57.             </plugin>  
  58.         </plugins>  
  59.     </build>  
  60. </project>  

特别注意一点,与Spring整合必须使用Quartz1.8.5及以下版本,最初我拿2.1.3的,怎么搞都报错:
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.scheduling.quartz.CronTriggerBean] for bean with name 'mytrigger' defined in class path resource [applicationContext.xml]: problem with class file or dependent class; nested exception is java.lang.IncompatibleClassChangeError: class org.springframework.scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class

查看发现spring3.0.5中org.springframework.scheduling.quartz.CronTriggerBean继承了org.quartz.CronTrigger(public class CronTriggerBeanextends CronTrigger),而在quartz2.1.3中org.quartz.CronTrigger是个接口(publicabstract interface CronTrigger extends Trigger),而在quartz1.8.5及1.8.4中org.quartz.CronTrigger是个类(publicclass CronTrigger extends Trigger),从而造成无法在applicationContext中配置触发器。这是spring3和quartz2版本不兼容的一个bug。

在Spring中使用Quartz有两种方式实现:第一种是任务类继承QuartzJobBean,第二种则是在配置文件里定义任务类和要执行的方法,类和方法仍然是普通类。很显然,第二种方式远比第一种方式来的灵活。
第一种方式的JAVA代码:
Java代码  收藏代码
  1. package com.ncs.hj;  
  2.   
  3. import org.quartz.JobExecutionContext;  
  4. import org.quartz.JobExecutionException;  
  5. import org.springframework.scheduling.quartz.QuartzJobBean;  
  6.   
  7. public class SpringQtz extends QuartzJobBean{  
  8.     private static int counter = 0;  
  9.     protected void executeInternal(JobExecutionContext context) throws JobExecutionException {  
  10.         System.out.println();  
  11.         long ms = System.currentTimeMillis();  
  12.         System.out.println("\t\t" + new Date(ms));  
  13.         System.out.println(ms);  
  14.         System.out.println("(" + counter++ + ")");  
  15.         String s = (String) context.getMergedJobDataMap().get("service");  
  16.         System.out.println(s);  
  17.         System.out.println();  
  18.     }  
  19. }  

第二种方式的JAVA代码:
Java代码  收藏代码
  1. package com.ncs.hj;  
  2.   
  3. import org.quartz.JobExecutionContext;  
  4. import org.quartz.JobExecutionException;  
  5. import org.springframework.scheduling.quartz.QuartzJobBean;  
  6.   
  7. import java.util.Date;  
  8.   
  9. public class SpringQtz {  
  10.     private static int counter = 0;  
  11.     protected void execute()  {  
  12.         long ms = System.currentTimeMillis();  
  13.         System.out.println("\t\t" + new Date(ms));  
  14.         System.out.println("(" + counter++ + ")");  
  15.     }  
  16. }  

Spring的配置文件:
Xml代码  收藏代码
  1. <!------------ 配置调度程序quartz ,其中配置JobDetail有两种方式-------------->    
  2.     <!--方式一:使用JobDetailBean,任务类必须实现Job接口 -->     
  3.     <bean id="myjob" class="org.springframework.scheduling.quartz.JobDetailBean">    
  4.      <property name="name" value="exampleJob"></property>    
  5.      <property name="jobClass" value="com.ncs.hj.SpringQtz"></property>   
  6.      <property name="jobDataAsMap">  
  7. <map>  
  8.     <entry key="service"><value>simple is the beat</value></entry>  
  9. </map>  
  10. ;/property>  
  11.     </bean>   
  12.     <!--运行时请将方式一注释掉! -->    
  13.     <!-- 方式二:使用MethodInvokingJobDetailFactoryBean,任务类可以不实现Job接口,通过targetMethod指定调用方法-->    
  14.     <!-- 定义目标bean和bean中的方法 -->  
  15.     <bean id="SpringQtzJob" class="com.ncs.hj.SpringQtz"/>  
  16.     <bean id="SpringQtzJobMethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  17.     <property name="targetObject">  
  18.         <ref bean="SpringQtzJob"/>  
  19.     </property>  
  20.     <property name="targetMethod">  <!-- 要执行的方法名称 -->  
  21.         <value>execute</value>  
  22.     </property>  
  23. </bean>  
  24.   
  25. <!-- ======================== 调度触发器 ======================== -->  
  26. <bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean">  
  27.     <property name="jobDetail" ref="SpringQtzJobMethod"></property>  
  28.     <property name="cronExpression" value="0/5 * * * * ?"></property>  
  29. </bean>  
  30.   
  31. <!-- ======================== 调度工厂 ======================== -->  
  32. <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  33.     <property name="triggers">  
  34.         <list>  
  35.             <ref bean="CronTriggerBean"/>  
  36.         </list>  
  37.     </property>  
  38. </bean>    

关于cronExpression表达式,这里讲解一下:
字段 允许值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可选) 留空, 1970-2099 , - * /
表达式意义
"0 0 12 * * ?" 每天中午12点触发
"0 15 10 ? * *" 每天上午10:15触发
"0 15 10 * * ?" 每天上午10:15触发
"0 15 10 * * ? *" 每天上午10:15触发
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L * ?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
每天早上6点
0 6 * * *
每两个小时
0 */2 * * *
晚上11点到早上8点之间每两个小时,早上八点
0 23-7/2,8 * * *
每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 11 4 * 1-3
1月1日早上4点
0 4 1 1 *

最后别忘了在web.xml里面配置Spring:
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
  5.           http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  6.          version="2.5">  
  7.     <welcome-file-list>  
  8.         <welcome-file>index.html</welcome-file>  
  9.     </welcome-file-list>  
  10.   
  11.     <context-param>  
  12.         <param-name>contextConfigLocation</param-name>  
  13.         <param-value>/WEB-INF/spring-config.xml</param-value>  
  14.     </context-param>  
  15.   
  16.     <listener>  
  17.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  18.     </listener>  
  19. </web-app>  

运行结果:
Wed Feb 08 13:58:30 CST 2012
(0)
Wed Feb 08 13:58:35 CST 2012
(1)
Wed Feb 08 13:58:40 CST 2012
(2)
Wed Feb 08 13:58:45 CST 2012
(3)
Wed Feb 08 13:58:50 CST 2012
(4)
Wed Feb 08 13:58:55 CST 2012
(5)
Wed Feb 08 13:59:00 CST 2012

(6)



转自:http://kevin19900306.iteye.com/blog/1397744



<think>我们正在检查本地计算机是否安装Adobe Acrobat Reader DC。由于用户的问题是关于检测安装,我们可以通过几种方法来实现: 1. 检查注册表(Windows系统) 2. 检查应用程序目录(Windows、MacOS) 3. 使用命令行或脚本(如PowerShell、批处理、bash等) 考虑到用户可能使用不同的操作系统,我们将分别提供Windows和MacOS的检测方法。 注意:由于用户的问题中提到了Adobe Acrobat Reader DC,我们主要针对这个特定版本进行检测。 根据引用[5],我们知道Adobe Acrobat ReaderDC版本(最新版本)和XI版本(经典版本)。这里我们关注DC版本。 在Windows系统中,我们可以通过以下方式检测: - 检查注册表中Adobe Acrobat Reader DC安装信息。 - 检查程序安装目录(默认在C:\Program Files (x86)\Adobe\Acrobat Reader DC)。 在MacOS系统中,我们可以通过检查应用程序目录(/Applications/Adobe Acrobat Reader DC.app)。 另外,我们也可以尝试通过命令行调用Adobe Acrobat Reader DC的可执行文件,并检查返回的版本信息。 下面我们将分别提供Windows和MacOS下的检测方法。 一、Windows系统: 方法1:通过注册表 Adobe Acrobat Reader DC安装时会在注册表中创建键值,我们可以查看以下路径: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRd32.exe 如果存在这个键,那么我们可以获取其默认值,该值就是可执行文件的路径。 方法2:通过PowerShell脚本 我们可以使用PowerShell命令来检查: Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion | Where-Object { $_.DisplayName -like "*Adobe Acrobat Reader DC*" } 方法3:通过文件系统 检查默认安装路径是否存在: C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe 二、MacOS系统: 通常,我们可以检查应用程序目录: /Applications/Adobe Acrobat Reader DC.app 或者使用终端命令: mdfind "kMDItemKind == 'Application'" | grep "Adobe Acrobat Reader DC" 此外,我们也可以编写一个跨平台的脚本(如Python)来检测,但需要根据操作系统执行不同的检测逻辑。 下面我们提供几种检测方法的示例代码: 1. Windows批处理脚本(check_adobe_reader.bat): ```batch @echo off setlocal set "regKey=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRd32.exe" set "filePath=C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" reg query "%regKey%" 2>nul if %errorlevel% equ 0 ( echo Adobe Acrobat Reader DC is installed (found in registry). exit /b 0 ) if exist "%filePath%" ( echo Adobe Acrobat Reader DC is installed (found in file system). exit /b 0 ) echo Adobe Acrobat Reader DC is not installed. exit /b 1 ``` 2. Windows PowerShell脚本(check_adobe_reader.ps1): ```powershell # 检查注册表路径 $regPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRd32.exe" $installPath = "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" if (Test-Path $regPath) { Write-Host "Adobe Acrobat Reader DC is installed (found in registry)." exit 0 } if (Test-Path $installPath) { Write-Host "Adobe Acrobat Reader DC is installed (found in file system)." exit 0 } Write-Host "Adobe Acrobat Reader DC is not installed." exit 1 ``` 3. MacOS/Linux Shell脚本(check_adobe_reader.sh): ```bash #!/bin/bash # 检查应用程序目录 if [ -d "/Applications/Adobe Acrobat Reader DC.app" ]; then echo "Adobe Acrobat Reader DC is installed." exit 0 fi # 尝试使用mdfind(仅MacOS) if command -v mdfind >/dev/null 2>&1; then result=$(mdfind "kMDItemKind == 'Application' && kMDItemDisplayName == 'Adobe Acrobat Reader DC'") if [ -n "$result" ]; then echo "Adobe Acrobat Reader DC is installed." exit 0 fi fi echo "Adobe Acrobat Reader DC is not installed." exit 1 ``` 4. Python脚本(跨平台): ```python import os import sys import platform def check_windows(): # 检查注册表 try: import winreg key_path = r"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRd32.exe" try: winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path) return True except FileNotFoundError: pass except ImportError: pass # 检查文件路径 path = r"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" if os.path.exists(path): return True return False def check_macos(): path = "/Applications/Adobe Acrobat Reader DC.app" if os.path.exists(path): return True return False def main(): system = platform.system() installed = False if system == "Windows": installed = check_windows() elif system == "Darwin": installed = check_macos() else: # 对于Linux,Adobe Acrobat Reader DC不提供官方版本,但可能有其他方式安装,这里不处理 print("This script does not support Linux for Adobe Acrobat Reader DC.") sys.exit(1) if installed: print("Adobe Acrobat Reader DC is installed.") sys.exit(0) else: print("Adobe Acrobat Reader DC is not installed.") sys.exit(1) if __name__ == "__main__": main() ``` 注意:以上脚本都是示例,实际使用时可能需要根据具体环境调整(例如64位系统可能路径不同,或者用户自定义安装路径)。在Windows中,我们主要检查32位版本的路径,因为Adobe Acrobat Reader DC通常是32位的。在64位系统上,32位程序通常安装在“Program Files (x86)”目录下。 用户可以根据自己的操作系统选择合适的方法来检测。 另外,需要注意的是,如果用户安装的是其他版本(如Adobe Acrobat Pro DC),那么上述方法可能不适用。但问题明确要求检测Adobe Acrobat Reader DC。 最后,我们还可以通过尝试读取版本信息来进一步确认,但上述方法已经足够用于检测是否安装。 根据引用[5],用户可能下载了离线安装程序,因此我们也可以考虑检查其他可能的安装路径(如用户自定义路径),但通常上述默认路径已经覆盖了大多数情况。 如果用户需要更精确的检测(比如需要版本号),我们可以扩展脚本以获取版本信息。 以上方法供用户参考。</think>### 如何检测本地是否安装 Adobe Acrobat Reader DC 以下是几种可靠的检测方法,根据操作系统选择相应方案: --- #### **Windows 系统** 1. **通过注册表检测** ```powershell # PowerShell 命令 $regPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRd32.exe" if (Test-Path $regPath) { $installPath = (Get-ItemProperty $regPath).'(default)' Write-Host "已安装,路径: $installPath" } else { Write-Host "未安装 Adobe Acrobat Reader DC" } ``` 2. **检查安装目录** - 默认安装路径: ``` C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe ``` - 直接在文件资源管理器中检查该路径是否存在。 3. **命令行检测** ```cmd :: CMD 命令 where /R "C:\Program Files" AcroRd32.exe 2>nul if %errorlevel% equ 0 (echo 已安装) else (echo 未安装) ``` --- #### **macOS 系统** 1. **检查应用程序目录** ```bash # 终端命令 if [ -d "/Applications/Adobe Acrobat Reader DC.app" ]; then echo "已安装" else echo "未安装" fi ``` 2. **通过系统信息检测** ```bash # 列出所有 Adobe 应用 system_profiler SPApplicationsDataType | grep "Adobe Acrobat Reader DC" ``` --- #### **通用方法(跨平台)** 1. **尝试打开 PDF 文件** - 右键点击任意 PDF 文件 → "打开方式" → 检查是否有 **Adobe Acrobat Reader DC** 选项。 2. **通过控制面板/系统设置** - **Windows**:`控制面板 > 程序和功能` → 查找 Adobe Acrobat Reader DC - **macOS**:`系统设置 > 通用 > 存储空间 > 应用程序` → 查找 Adobe Acrobat Reader DC --- #### **编程检测(Python 示例)** ```python import winreg, os def check_adobe_reader(): try: # 检查注册表 key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRd32.exe") path = winreg.QueryValue(key, None) return True if os.path.exists(path) else False except FileNotFoundError: return False if check_adobe_reader(): print("Adobe Acrobat Reader DC安装") else: print("未检测到安装") ``` > **注意**: > - 以上方法优先检测 **Reader DC**(最新版),若需检测旧版(如 Reader XI),请替换路径中的 `DC` 为 `XI`[^5]。 > - 若通过编程检测,需以管理员权限运行脚本(Windows)。 --- ### 相关问题 1. **如何区分 Adobe Acrobat Reader DCAdobe Acrobat Pro DC?** > 两者功能差异主要体现在编辑能力上,Reader DC 仅支持查看/注释,Pro DC 支持PDF编辑/转换等高级功能[^4]。 2. **检测到已安装无法打开 PDF 文件,如何修复?** > 可尝试重设文件关联:`设置 > 应用 > 默认应用 > 按文件类型指定默认值 > 选择 .pdfAdobe Acrobat Reader DC`[^3]。 3. **Adobe Acrobat Reader DC 的离线安装包如何获取?** > 官方下载地址可通过分析在线安装页面获取,或直接使用提供的离线包链接[^5]。 4. **如何通过命令行静默安装 Adobe Acrobat Reader DC?** > 使用官方的 `AcroRdrDCxxxxx_xx_xx.exe /sAll` 命令进行无界面安装。 [^1]: Adobe Acrobat Reader DC 是处理 PDF 文件的可靠工具。 [^3]: Adobe Acrobat Reader DC 具备出色的文件兼容性。 [^4]: Adobe Acrobat Reader DC 提供基础阅读功能,Pro 版支持高级编辑。 [^5]: Adobe Acrobat Reader 的离线安装程序可通过分析在线地址获取。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值