Spring调度配置
程序员文章站
2022-06-15 11:20:27
...
当你新建web项目,配置好SSH环境后如要使用调度,先需要导入quartz-all-1.6.1.jar.这里的Demo是发短信:
首先,配置好applicationContext.xml:
其次,接下来就是applicationContext-quartz.xml,调度配置文件:
接下来,就是具体调度实现的方法了:
好了,一个调度这就行了,很简单的。关于cron表达式,可以看一下本人的这篇文章cron表达式,
不足之处请多多指教!
首先,配置好applicationContext.xml:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@***.***.***.***:**" /> <property name="username" value="***" /> <property name="password" value="***" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop> </props> </property> <property name="mappingDirectoryLocations"> <list> <value>classpath*:/com/*/model</value> </list> </property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!-- 配置事务传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="do*" propagation="REQUIRED" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <!-- 哪些类的哪些方法参与事务 --> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution(* com.*.service.*.*(..))" /> <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" /> </aop:config> <!-- dao模板(所有dao都将使用此模板) --> <bean id="daoTemplate" abstract="true"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <import resource="applicationContext-dao.xml" /> <import resource="applicationContext-service.xml" /> <import resource="applicationContext-action.xml" /> <import resource="applicationContext-quartz.xml" /> <import resource="applicationContext-webservice.xml" /> </beans>
其次,接下来就是applicationContext-quartz.xml,调度配置文件:
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- 配置需要定时执行的Spring bean --> <bean id="sendMMSScheduler" class="com.lx100ERP.schedule.SendMMSScheduler"> <property name="purOrderService" ref="purOrderService"></property> </bean> <!-- 配置quartz的任务,指定该任务执行哪一个bean的哪一个方法 --> <bean id="sendMMSByDayFactory" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 要调用的类 --> <property name="targetObject" ref="sendMMSScheduler" /> <!-- 要调用的方法 --> <property name="targetMethod" value="sendMMS" /> </bean> <!-- 配置quartz的触发器,指定该触发器触发的条件,以及该触发器加载哪些job --> <bean id="sendMMSByDayCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="sendMMSByDayFactory" /> </property> <!-- cron 表达式(秒,分,时,日,月,周):每隔10分钟执行一次 --> <property name="cronExpression" value="0 */1 0-23 * * ?" /> </bean> <!-- 配置quartz调度,指定加载哪些触发器 --> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <!-- 这里一定要放开,否则不会调成功--> <ref local="sendMMSByDayCronTrigger" /> </list> </property> </bean> </beans>
接下来,就是具体调度实现的方法了:
/** * 短信下发调度 * */ public class SendMMSScheduler { private PurchaseOrderService purOrderService; public void sendMMS() { //TODO //具体方法 } public void setPurOrderService(PurchaseOrderService purOrderService) { this.purOrderService = purOrderService; } }
好了,一个调度这就行了,很简单的。关于cron表达式,可以看一下本人的这篇文章cron表达式,
不足之处请多多指教!
上一篇: 密码学中的DES简化版
下一篇: 网络安全-常见网络安全攻击