Quartz Job Scheduling Framework第10章翻译初稿 续

 

In the example in Listing 10.2, the helloWorld() method that was invoked on the EJB didn't defined any parameters. The EJBInvokedJob class enables you to pass arguments to an EJB method by specifying them using the EJB_ARGS_KEY and EJB_ARG_TYPES_KEY parameters shown in Table 10.1.

<o:p> </o:p>

列表10.2中定义的供EJB调用的helloWord()方法并没有定义任何参数。EJBInvokedJob类允许你想EJB方法中传递参数。方法是使用表10.1中的EJB_ARGS_KEY EJB_ARG_TYPES_KEY

<o:p> </o:p>

Listing 10.3 shows another simple example that passes an argument to a different version of helloWorld() EJB running on the Apache Geronimo J2EE server.

<o:p> </o:p>

列表 10.3 显示了另一个简单的示例,将参数传递给其他版本的运行在Apache Geronimo J2EE 服务器上的EJB helloWorld()

<o:p> </o:p>

Listing 10.3 is very similar to Listing 10.2, except that it includes the parameters EJB_ARGS_KEY and EJB_ARG_TYPES_KEY. Also, because it's running against the Geronimo J2EE application server, it needed to add the arguments for PRINCIPAL and CREDENTIALS.

<o:p> </o:p>

列表10.310.2非常类似除了10.3中包含参数EJB_ARGS_KEY EJB_ARG_TYPES_KEY。当然由于运行在Geronimo J2EE服务器上,它需要添加额外的参数(PRINCIPAL CREDENTIALS)。

<o:p> </o:p>

Listing 10.3. A Simple Example Using the EJBInvokerJob<o:p></o:p>

列表10.3 一个简单的使用EJBInvokerJob的例子<o:p></o:p>

 

java 代码
<o:p>
  1. package org.cavaness.quartzbook.chapter10;   
  2.   
  3.     
  4.   
  5. import java.util.Date;   
  6.   
  7.     
  8.   
  9. import org.apache.commons.logging.Log;   
  10.   
  11. import org.apache.commons.logging.LogFactory;   
  12.   
  13. import org.quartz.JobDetail;   
  14.   
  15. import org.quartz.Scheduler;   
  16.   
  17. import org.quartz.SchedulerException;   
  18.   
  19. import org.quartz.Trigger;   
  20.   
  21. import org.quartz.TriggerUtils;   
  22.   
  23. import org.quartz.impl.StdSchedulerFactory;   
  24.   
  25. import org.quartz.jobs.ee.ejb.EJBInvokerJob;   
  26.   
  27.     
  28.   
  29. public class Listing_10_3 {   
  30.   
  31.      static Log logger = LogFactory.getLog(Listing_10_3.class);   
  32.   
  33.     
  34.   
  35.      public static void main(String[] args) {   
  36.   
  37.     
  38.   
  39.           Listing_10_3 example = new Listing_10_3();   
  40.   
  41.     
  42.   
  43.           try {   
  44.   
  45.     
  46.   
  47.               // Create a Scheduler and schedule the Job   
  48.   
  49.               Scheduler scheduler = example.createScheduler();   
  50.   
  51.               example.scheduleJob(scheduler);   
  52.   
  53.     
  54.   
  55.               // Start the Scheduler running   
  56.   
  57.               scheduler.start();   
  58.   
  59.     
  60.   
  61.               logger.info("Scheduler started at " + new Date());   
  62.   
  63.     
  64.   
  65.           } catch (SchedulerException ex) {   
  66.   
  67.                logger.error(ex);   
  68.   
  69.           }   
  70.   
  71.     
  72.   
  73.     
  74.   
  75.     
  76.   
  77.      }   
  78.   
  79.     
  80.   
  81.      // Schedule the EJBInvokerJob   
  82.   
  83.      private void scheduleJob(Scheduler scheduler)   
  84.   
  85.           throws SchedulerException {   
  86.   
  87.     
  88.   
  89.           // Create a JobDetail for the Job   
  90.   
  91.           JobDetail jobDetail = new JobDetail("HelloWorldJob",   
  92.   
  93.                     Scheduler.DEFAULT_GROUP,   
  94.   
  95.                     org.quartz.jobs.ee.ejb.EJBInvokerJob.class);   
  96.   
  97.     
  98.   
  99.           // Load all of the necessary EJB parameters   
  100.   
  101.           loadJobDataMap(jobDetail);   
  102.   
  103.     
  104.   
  105.           // Create a trigger that fires every 10 seconds, forever   
  106.   
  107.           Trigger trigger = TriggerUtils.makeSecondlyTrigger(10);   
  108.   
  109.     
  110.   
  111.           trigger.setName("helloWorldTrigger");   
  112.   
  113.           // Start the trigger firing from now   
  114.   
  115.           trigger.setStartTime(new Date());   
  116.   
  117.     
  118.   
  119.           // Associate the trigger with the job in the scheduler   
  120.   
  121.           scheduler.scheduleJob(jobDetail, trigger);   
  122.   
  123.     
  124.   
  125.      }   
  126.   
  127.     
  128.   
  129.      /*  
  130.  
  131.       * Configure the EJB parameters in the JobDataMap  
  132.  
  133.       * 在JobDataMap中配置EJB参数  
  134.  
  135.       */  
  136.   
  137.      public JobDetail loadJobDataMap(JobDetail jobDetail) {   
  138.   
  139.           jobDetail.getJobDataMap().put(   
  140.   
  141.                EJBInvokerJob.EJB_JNDI_NAME_KEY, "ejb/Test");   
  142.   
  143.     
  144.   
  145.           jobDetail.getJobDataMap().put(EJBInvokerJob.EJB_METHOD_KEY,   
  146.   
  147.                     "helloWorld");   
  148.   
  149.     
  150.   
  151.           Object[] args = new Object[1];   
  152.   
  153.           args[0] = " from Quartz";   
  154.   
  155.           jobDetail.getJobDataMap().put(   
  156.   
  157.                EJBInvokerJob.EJB_ARGS_KEY, args);   
  158.   
  159.     
  160.   
  161.           Class[] argTypes = new Class[1];   
  162.   
  163.           argTypes[0] = java.lang.String.class;   
  164.   
  165.           jobDetail.getJobDataMap().put(   
  166.   
  167.                EJBInvokerJob.EJB_ARG_TYPES_KEY, argTypes);   
  168.   
  169.     
  170.   
  171.           jobDetail.getJobDataMap().put(   
  172.   
  173.                EJBInvokerJob.PROVIDER_URL, "127.0.0.1:4201");   
  174.   
  175.     
  176.   
  177.           jobDetail.getJobDataMap().put(   
  178.   
  179.                EJBInvokerJob.INITIAL_CONTEXT_FACTORY,   
  180.   
  181.                          "org.openejb.client.RemoteInitialContextFactory");   
  182.   
  183.           jobDetail.getJobDataMap().put(   
  184.   
  185.                EJBInvokerJob.PRINCIPAL, "system");   
  186.   
  187.     
  188.   
  189.           jobDetail.getJobDataMap().put(   
  190.   
  191.                EJBInvokerJob.CREDENTIALS, "manager");   
  192.   
  193.     
  194.   
  195.           return jobDetail;   
  196.   
  197.      }   
  198.   
  199.     
  200.   
  201.      /*  
  202.  
  203.       * return an instance of the Scheduler from the factory  
  204.  
  205.       */  
  206.   
  207.      public Scheduler createScheduler() throws SchedulerException {   
  208.   
  209.           return StdSchedulerFactory.getDefaultScheduler();   
  210.   
  211.      }   
  212.   
  213. }   
  214.   
  215.     

 

EJBInvokerJob Parameters and Serialization<o:p></o:p>

EJBInvokerJob参数与串行化<o:p></o:p>

Because of the typical serialization problems that are associated with Java and distributed applications, you should stick to passing Strings and primitives to your EJB methods. If you need to pass more complex types, your code must serialize the objects between client and server properly. For more in-depth information on Java serialization, check out Sun's Serialization specification at http://java.sun.com/j2se/1.5.0/docs/guide/serialization.

<o:p> </o:p>

由于典型的串行化问题都与JAVA分布式应用有关,所以你最好只传递字符串和基本类型到EJB方法中。如果你需要传递复杂类型你需要在客户端和服务器代码中恰当的串行化对象。更加深入的关于JAVA串行化的资料点击Sun的串行化规范:http://java.sun.com/j2se/1.5.0/docs/guide/serialization

<o:p> </o:p>

<o:p> </o:p>

Because Quartz needs to get a reference to the home and remote interfaces for the EJB, you need to deploy some J2EE client JARs with your external Quartz application. The JARs you need to add depend on which J2EE container you're using. If you're using WebLogic, for example, you'll probably just put the weblogic.jar with the Quartz application. For Geronimo, several are involved. Check with the server documentation to be sure.

<o:p> </o:p>

应为Quartz需要为EJB获取本地与远程的接口,你必须将一些J2EE客户端jar包导入到外部Quartz应用中。添加的Jar包依赖于你使用的J2EE容器。例如,如果你使用WebLogic你需要将weblogic.jar导入到Quartz应用中。如果使用Geronimo导入其他几个被调用的包。你需要查看服务器支持文档。

<o:p> </o:p>

Running Quartz Within the J2EE Application Server<o:p></o:p>

J2EE应用服务器中部署Quartz<o:p></o:p>

Running Quartz as a J2EE client is a little more involved than running Quartz as an external J2SE application. This is mostly because deploying applications within the container is somewhat more complicated. In addition, the J2EE specification puts some constraints on components within the container. One of the biggest guidelines that the specification gives involves who and what can create Java threads. Because it's the container's responsibility to manage all resources, it can't allow just anything or anyone to create threads. If it did, it would have a harder time managing the environment and keeping things stable. Quartz creates its own worker threads, so you need to follow some steps to make sure things work properly.

<o:p> </o:p>

与作为外部J2SE应用相比将Quartz作为一个J2EE客户端比较复杂。这是因为在容器中部署应用就比较复杂。另外J2EE规范对容器内的组件加以约束。影响最大的准则是规范规定了谁或者什么可以创建JAVA线程。因为容器通过创建线程管理资源。所以不允许任何人或物私自创建线程。如果这样做了容器将不能管理环境也不能保持容器中事务的稳定。Quartz创建自己的工作线程。这样你就必须按下面的步骤配置以保证Quartz正常运行。

<o:p> </o:p>

Assume that a stateless session bean such as the one from Listing 10.1 is already deployed in the container. The easiest way to deploy Quartz within the container is to build a WAR file that contains all the necessary files and then use the admin tools, or Eclipse, to deploy the Web application within the container.

<o:p> </o:p>

加入要向容器中部署一个列表10.1那样的无状态会话bean。最简单的方法是将Quartz建立一个包含所有必要文件的war文件使用管理员工具或EclipseWeb应用部署到容器中。

<o:p> </o:p>

The Web application directory structure is just like that of any other Web application. You need to add the following files to it:

<o:p> </o:p>

Web应用文件夹与其他Web应用相同。你需要相其中添加下面的文件。

<o:p> </o:p>

  • web.xml (put in WEB-INF)
  • quartz.properties (put in WEB-INF/classes)
  • quartz_jobs.xml (put in WEB-INF/classes)
  • Quartz binary (put in WEB-INF/lib)
  • Third-party libraries (put in WEB-INF/lib)

Because you are building a Web application, you need to add the requisite web.xml deployment descriptor. Listing 10.4 shows the web.xml for our client application that will be installed within the container.

<o:p> </o:p>

因为你要建立Web应用。你必须添加Web.xml文件。列表10.4显示了将要部署到容器中的客户端应用的web.xml

<o:p> </o:p>

Listing 10.4. The web.xml for the Quartz J2EE Client Application<o:p></o:p>

列表10.4 Quartz J2EE客户端应用的web.xml<o:p></o:p>

xml 代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3.     
  4.   
  5. <web-app>  
  6.   
  7.   <servlet>  
  8.   
  9.     <servlet-name>QuartzServlet</servlet-name>  
  10.   
  11.     <servlet-class>  
  12.   
  13.       org.quartz.ee.servlet.QuartzInitializerServlet   
  14.   
  15.     </servlet-class>  
  16.   
  17.     <load-on-startup>1</load-on-startup>  
  18.   
  19.   </servlet>  
  20.   
  21.     
  22.   
  23.   <servlet-mapping>  
  24.   
  25.     <servlet-name>QuartzServlet</servlet-name>  
  26.   
  27.     <url-pattern>/servlet/QuartzServlet</url-pattern>  
  28.   
  29.   </servlet-mapping>  
  30.   
  31. </web-app>  
  32.   
  33.     

The Quartz framework includes a Java servlet called QuartzInitializerServlet that, when invoked, initializes the Quartz Scheduler and loads job information. In Listing 10.4, we've set the <load-on-startup> parameter to have a value of 1 so the servlet will be loaded and initialized when the container is started. By using the servlet to start the Quartz Scheduler, we avoid the issues of thread permission because the container will allow servlets to create user threads.

<o:p> </o:p>

Quartz 包含一个Java ServletQuartzInitializerServlet)当它被用来初始化Quartz Scheduler并加载job信息。在列表10.4中我们设置<load-on-startup>参数为1,当容器初始化时这个servlet就会被加载并初始化。通过使用servlet启动Quartz Scheduler我们避免了线程权限问题,因为容器允许servlet创建用户权限。<o:p></o:p>

<o:p> </o:p>

QuartzInitializerListener Added to Quartz<o:p></o:p>

QuartzInitializerListener添加到Quartz<o:p></o:p>

Recently, a new class called QuartzInitializerListener was added to Quartz that implements the javax.servlet.ServletContextListener interface. This class can be used as an alternative to the QuartzInitializerServlet mentioned earlier.

最近一个新类QuartzInitializerListener被添加到Quartz中改类实现javax.servlet.ServletContextListener接口,QuartzInitializerListener可以作为刚刚提到的QuartzInitializerServlet替代方案。

<o:p> </o:p>

Next, you need to put the standard quartz.properties file into the WEB-INF/classes directory of the Web application. There's nothing special about this version of the properties file; it's essentially what we did in past chapters. However, here we use the JobInitializationPlugin (this was shown in Chapter 8, "Using Quartz Plug-Ins," and is designed to load job information from an XML file). By default, the plug-in looks for a file called quartz_jobs.xml and loads the jobs found in the file. As Chapter 8 described, using this particular plug-in keeps you from having to write job-loading code and be forced to recompile when changes occur. The quartz_jobs.xml file for this example is shown in Listing 10.5.

<o:p> </o:p>

下一步你需要将quartz.properties文件添加到WEB-INF/classes文件夹中。Properties文件没有版本限制与我们在前面章节讲解的配置文件没有区别。然而,这里我们使用了JobInitializationPlugin(在第8章 使用Quartz插件 做详细讲解),它被设计用来从XML文件中加载Job信息。默认情况下,插件寻找quartz_jobs.xml文件并从中加载job信息。正如第8章描述的那样,这个特殊的插件使你不必编写job加载代码,也就不用在job发生改变时重新编译代码。这个例子的quartz_jobs.xml如列表10.5

<o:p> </o:p>

Listing 10.5. The quartz_jobs.xml Used Within the J2EE Client<o:p></o:p>

列表10.5 J2EE客户端使用的quartz_jobs.xml<o:p></o:p>

xml 代码
  1. <?xml version='1.0' encoding='utf-8'?>  
  2.   
  3.     
  4.   
  5. <quartz>  
  6.   
  7.   <job>  
  8.   
  9.     <job-detail>  
  10.   
  11.         <name>HelloWorldJob</name>  
  12.   
  13.         <group>DEFAULT</group>  
  14.   
  15.         <job-class>org.quartz.jobs.ee.ejb.EJBInvokerJob</job-class>  
  16.   
  17.         <volatility>false</volatility>  
  18.   
  19.         <durability>false</durability>  
  20.   
  21.         <recover>false</recover>  
  22.   
  23.     
  24.   
  25.         <job-data-map allows-transient-data="true">  
  26.   
  27.           <entry>  
  28.   
  29.             <key>ejb</key>  
  30.   
  31.             <value>ejb/Test</value>  
  32.   
  33.           </entry>  
  34.   
  35.           <entry>  
  36.   
  37.             <key>java.naming.factory.initial</key>  
  38.   
  39.             <value>org.openejb.client.RemoteInitialContextFactory</value>  
  40.   
  41.           </entry>  
  42.   
  43.           <entry>  
  44.   
  45.             <key>java.naming.provider.url</key>  
  46.   
  47.             <value>127.0.0.1:4201</value>  
  48.   
  49.           </entry>  
  50.   
  51.           <entry>  
  52.   
  53.             <key>method</key>  
  54.   
  55.      &nb
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值