淘宝商城(天猫)高级技术专家.3年研发+3年性能测试调优/系统测试+4年团队管理与测试架构、研发系统实践. 新舞台新气象, 深化测试基础架构及研发架构,希望能在某个技术领域成为真正的技术大牛。欢迎荐才http://bbs.51testing.com/viewthread.php?tid=120496&extra=&page=1 .邮件: jianzhao.liangjz@alibaba-inc.com,MSN:liangjianzhao@163.com.微博:http://t.sina.com.cn/1674816524

Spring+ quartz 在jboss内实现定时任务

上一篇 / 下一篇  2008-10-19 14:28:49 / 个人分类:java性能监控与调优

参考

 

http://www.cnblogs.com/phinecos/archive/2008/09/03/1282747.html

http://www.blogjava.net/hwpok/archive/2008/08/19/223124.html

http://www.cnblogs.com/phinecos/archive/2008/09/03/1283376.html

 

 

com\ali\schedule\ quartz.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"

   xsi:schemaLocation="http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

   <bean name="dataImportjobDetail"

       class="org.springframework.scheduling.quartz.JobDetailBean">

       <property name="jobClass"

           value="com.ali.schedule.DailyDataImportJob" />

       <property name="jobDataAsMap">

           <map>

               <entry key="dataImportService" value-ref="dataImportService" />

           </map>

       </property>

       <property name="applicationContextJobDataKey"

           value="applicationContext" />

   </bean>

 

   <bean id="dataImportService" class="com.ali.schedule.ProjDataImportService" />

   

  

   <bean id="dataImportTrigger"

       class="org.springframework.scheduling.quartz.CronTriggerBean">

       <property name="jobDetail" ref="dataImportjobDetail" />

       <property name="cronExpression" value="*/5 * * * * ?" />

   </bean>

   

   <bean id="scheduler"

       class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

       <property name="triggers">

           <list>

               <ref bean="dataImportTrigger" />

           </list>

       </property>

       <property name="schedulerContextAsMap">

           <map>

               <entry key="timeout" value="30" />

           </map>

       </property>

       <property name="quartzProperties">

           <props>

               <prop key="org.quartz.threadPool.class">

                   org.quartz.simpl.SimpleThreadPool

               </prop>

               <prop key="org.quartz.threadPool.threadCount">10</prop>

           </props>

       </property>

   </bean>

</beans>

 

 

值得注意的是:

<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  -->
    
<bean id="scheduler"  lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        
<property name="triggers">
            
<list>
              
</list>
        
</property>
    
</bean>
</beans>

 

调度程序:

 

packagecom.ali.schedule;

importorg.quartz.JobExecutionContext;

importorg.quartz.JobExecutionException;

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

importorg.springframework.scheduling.quartz.QuartzJobBean;

publicclassDailyDataImportJobextendsQuartzJobBean {

   publicDailyDataImportJob()

   {

      

   }

   protectedvoidexecuteInternal(JobExecutionContext jobcontext)throwsJobExecutionException

   {

      service.importData();

   }

   privateProjDataImportServiceservice;

   publicvoidsetDataImportService(ProjDataImportService service)

   {

      this.service=service;

   }

   publicstaticvoidmain(String[] args){

      //这里用于测试

     String configPath ="com/ali/schedule/quartz.xml";

      ApplicationContext ctx =newClassPathXmlApplicationContext(configPath);

      ctx.getBean("scheduler");

   }

 

}

 

 

真正执行任务程序:

packagecom.ali.schedule;

 

importorg.springframework.context.ApplicationContext;

 

importorg.springframework.context.support.ClassPathXmlApplicationContext;

importorg.springframework.context.support.FileSystemXmlApplicationContext;

 

importcom.ali.IIbatisDAOImpl;

 

publicclassProjDataImportService{

   publicvoidimportData() {

 

//当前路径下

      

      String[] locations = {"com/ali/db/database.xml"};

 

   

   ApplicationContext context =//new FileSystemXmlApplicationContext(locations);

      new ClassPathXmlApplicationContext(locations);

 

      

          IIbatisDAOImpl DAO = (IIbatisDAOImpl) context.getBean("IIbatisDAOImpl");

 

      DAO.importHisProjData();

 

   }

 

   publicstaticvoidmain(String[] args) {

 

      ProjDataImportServiceservice =newProjDataImportService();

      service.importData();

   }

 

}

 

 

依赖库:

需要加入spring.jar  quartz.jar   log4j-1.2.14.jar   commons-collections.jar   jta.jar   commons-logging.jar这几个包

 

如上都是在控制台应用下执行。由于spring工程、ibatis工程读取xml文件路径与JBOSS/TOMCAT web应用路径都不一样。控制台测试成功后,需要更改如上javaXML路径依赖。

 

WEB环境下使用调度程序,则需要在web.xml加载quartz.xml配置文件。

 

      <context-param>

             <param-name>contextConfigLocation</param-name>

             <param-value>

                    /WEB-INF/database.xml

                    /WEB-INF/applicationContext.xml

                   /WEB-INF/quartz.xml

             </param-value>

      </context-param>

 


TAG: spring quartz jboss 定时任务 java性能监控与调优

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar