`
adamed
  • 浏览: 181782 次
社区版块
存档分类
最新评论

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
分享到:
评论

相关推荐

    Quartz Job Scheduling Framework第2章翻译初稿

    Quartz Job Scheduling Framework第2章翻译初稿 博文链接:https://adamed.iteye.com/blog/135880

    Quartz Job Scheduling Framework第11章翻译初稿

    博文链接:https://adamed.iteye.com/blog/135883

    Quartz Job Scheduling Framework第5章翻译初稿

    博文链接:https://adamed.iteye.com/blog/135881

    Quartz Job Scheduling Framework第7章翻译初稿

    博文链接:https://adamed.iteye.com/blog/135882

    Java 员工管理系统项目源代码(可做毕设项目参考)

    Java 员工管理系统项目是一个基于 Java 编程语言开发的桌面应用程序,旨在管理员工的信息、津贴、扣除和薪资等功能。该系统通过提供结构和工具集,使公司能够有效地管理其员工数据和薪资流程。 系统特点 员工管理:管理员可以添加、查看和更新员工信息。 津贴管理:管理员可以添加和管理员工的津贴信息。 扣除管理:管理员可以添加和管理员工的扣除信息。 搜索功能:可以通过员工 ID 搜索员工详细信息。 更新薪资:管理员可以更新员工的薪资信息。 支付管理:处理员工的支付和生成支付记录。 模块介绍 员工管理模块:管理员可以添加、查看和更新员工信息,包括员工 ID、名字、姓氏、年龄、职位和薪资等。 津贴管理模块:管理员可以添加和管理员工的津贴信息,如医疗津贴、奖金和其他津贴。 扣除管理模块:管理员可以添加和管理员工的扣除信息,如税收和其他扣除。 搜索功能模块:可以通过员工 ID 搜索员工详细信息。 更新薪资模块:管理员可以更新员工的薪资信息。 支付管理模块:处理员工的支付和生成支付记录 可以作为毕业设计项目参考

    CAD实验报告:制药车间动力控制系统图、烘烤车间电气控制图、JSJ型晶体管式时间继电器原理图、液位控制器电路图

    CAD实验报告:制药车间动力控制系统图、烘烤车间电气控制图、JSJ型晶体管式时间继电器原理图、液位控制器电路图

    使用 Arduino 和 Python 实时数据绘图的温度监控系统源码(可做毕设项目参考)

    项目简介: 本项目将教您如何使用 Arduino 和 Python 实时数据绘图来构建温度监控系统。通过这个项目,您将学习如何从 Arduino 到 Python 进行串行通信,并实时收集和监控温度数据。 项目目标: 实时监控和绘制温度数据。 提供用户友好的操作界面。 提高用户的编程技能,特别是Arduino和Python的应用能力。 项目功能 实时温度监控: 传感器每秒读取一次温度数据,并通过串行监视器发送到Python程序。 数据保存: Python程序将温度数据保存到CSV文件中。 实时数据绘图: 使用Matplotlib库实时绘制温度数据,温度在Y轴,时间在X轴。 项目优势 高效的数据监控: 实时监控和绘制温度数据,提高数据监控的效率。 用户友好: 界面简洁,操作简单,用户可以轻松使用该应用程序。 提高编程技能: 通过实践项目,提高对Arduino和Python的应用能力。 项目技术细节 项目详情: 项目名:使用 Arduino 和 Python 实时数据绘图的温度监控系统 项目平台:Arduino 和 Python 使用的编程语言:C++(Arduino)、Python ID

    软件测试-软件测试方案pdf

    本测试计划提供给深圳移动公司PMS核心小组成员,对PMS EXPRESS 系统进行功能测试。测试计划主要通过对基站项目管理过程的模拟,从项目的立项开始直至基站的验收交付以及知识沉淀,对基站建设全过程中涉及的管理内容进行模拟测 试。测试计划中设计了两个基站项目一明宁花园、椰风海岸。其中明宁花园按 原计划如期完工,而椰风海岸因为设备没能如期到货导致了个整个项目工期的延误。

    博物馆智能化系统的解决方案.pptx

    博物馆智能化系统的解决方案.pptx

    基于STM32的电子罗盘

    基于STM32的电子罗盘试验

    【matlab GUI仿真】说明:GUI界面设计,四旋翼飞机仿真 VR界面设计

    【matlab GUI仿真】说明:GUI界面设计,四旋翼飞机仿真。VR界面设计。 (GUI interface design, four rotor aircraft simulation. The VR interface design. ) 【matlab GUI仿真】说明:GUI界面设计,四旋翼飞机仿真。VR界面设计。

    基于VHDL的倒车雷达项目(免费提供全部源码)

    项目简介: 本项目实现了一个基于VHDL(VHSIC硬件描述语言)的倒车雷达系统。倒车雷达用于检测车辆后方障碍物的距离,以辅助驾驶员安全倒车。系统通过超声波传感器检测距离,并使用LED显示或蜂鸣器提示障碍物的接近程度。 项目模块: 传感器接口模块: 处理超声波传感器的信号。 发送触发信号,接收回波信号。 计算回波时间,进而计算距离。 距离计算模块: 根据传感器回波时间计算距离。 处理和转换距离数据,准备用于显示和警报。 警报显示模块: 基于计算出的距离提供视觉和听觉警报。 使用LED显示不同的距离范围。 使用蜂鸣器发出不同频率的警报声。 控制模块: 控制各模块的协调工作。 管理超声波传感器的触发和数据采集周期。

    2024年动力电池回收行业市场分析报告.pptx

    行业报告

    ssm框架在线课堂微信小程序源码+项目说明(高分毕设)

    毕业设计ssm框架在线课堂微信小程序源码+项目说明(高分毕设).zip 个人经导师指导并认可通过的高分设计项目,评审分98分。主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 毕业设计ssm框架在线课堂微信小程序源码+项目说明(高分毕设).zip 个人经导师指导并认可通过的高分设计项目,评审分98分。主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 毕业设计ssm框架在线课堂微信小程序源码+项目说明(高分毕设).zip 个人经导师指导并认可通过的高分设计项目,评审分98分。主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 项目主要功能: 该项目是一款针对在线课堂微信小程序的数据管理系统,旨在便捷用户对小程序中的信息进行有效管理。系统设计注重功能完善和用户体验,力求使用户能轻松找到所需信息。采用JAVA技术开发,并结合现有成熟源代码模板,确保了平台的可操作性和实用性。关键词:在线课堂微信小程序;JAVA。

    国内锂矿加速开发,四川锂矿详细梳理

    国内锂矿加速开发,四川锂矿详细梳理

    一个基于react框架的资源文件

    一个基于react框架的资源文件 练习,xiaoyoushop分支是小优后台,screen分支是大屏项目,react分支是基于react框架,使用ts语言,vite构建的一个后台管理系统项目 练习,xiaoyoushop分支是小优后台,screen分支是大屏项目,react分支是基于react框架,使用ts语言,vite构建的一个后台管理系统项目 练习,xiaoyoushop分支是小优后台,screen分支是大屏项目,react分支是基于react框架,使用ts语言,vite构建的一个后台管理系统项目 内容来源于网络分享。仅供学习使用。请勿商用。如有侵权,请联系我。我将立即删除

    MQD企业大学建设思路与年度工作重点.pptx

    MQD企业大学建设思路与年度工作重点.pptx

    Java语言基础入门教程 Java实训教程 13.反射 共38页.pptx

    Java语言基础入门教程 Java实训教程 13.反射 共38页.pptx

    网上购物商城数据库设计报告.docx

    网上购物商城数据库设计报告.docx

    单机成本核算统计表.docx

    单机成本核算统计表.docx

Global site tag (gtag.js) - Google Analytics