Quartz手册 - Lesson 9: Job Stores

JobStore’s are responsible for keeping track of all the “work data” that you give to the scheduler: jobs, triggers, calendars, etc. Selecting the appropriate JobStore for your Quartz scheduler instance is an important step. Luckily, the choice should be a very easy one once you understand the differences between them. You declare which JobStore your scheduler should use (and it’s configuration settings) in the properties file (or object) that you provide to the SchedulerFactory that you use to produce your scheduler instance.

JobStore负责记录所有给到scheduler的"工作数据",例如:jobs,triggers,日历等等。给Quartz的scheduler选择合适的JobStore非常重要。幸运的是,当你知道不同JobStore区别的以后,挑选一个合适的非常容易。通过提供给SchedulerFactory的配置文件,你可以为你的scheduler实例声明一个JobStore。

Never use a JobStore instance directly in your code. For some reason many people attempt to do this. The JobStore is for behind-the-scenes use of Quartz itself. You have to tell Quartz (through configuration) which JobStore to use, but then you should only work with the Scheduler interface in your code.

不要再代码中直接使用JobStore实例。JobStore是给Quartz本身提供幕后工作的。你必须通过配置文件来告诉Quartz使用哪种类型的JobStore,在代码中只使用Scheduler的实例。

RAMJobStore

RAMJobStore is the simplest JobStore to use, it is also the most performant (in terms of CPU time). RAMJobStore gets its name in the obvious way: it keeps all of its data in RAM. This is why it’s lightning-fast, and also why it’s so simple to configure. The drawback is that when your application ends (or crashes) all of the scheduling information is lost - this means RAMJobStore cannot honor the setting of “non-volatility” on jobs and triggers. For some applications this is acceptable - or even the desired behavior, but for other applications, this may be disastrous.

RAMJobStore是JobStore中的最简单使用方式,其性能也是最优异的(就CPU时间来说)。与其命名一样,RAMJobStore将所有数据存储在RAM中。这就是它高效和易配置的原因。其缺点是当你的应用程序终止或崩溃的时候,所有的调度信息将丢失。对于一些程序可能能够容忍(甚至是其期望的方式),但是对于其他应用来说这是无法容忍的。

To use RAMJobStore (and assuming you’re using StdSchedulerFactory) simply specify the class name org.quartz.simpl.RAMJobStore as the JobStore class property that you use to configure quartz:

只需要简单的配置就可以使用RAMJobStore(确保使用的是StdSchedulerFactory)。

Configuring Quartz to use RAMJobStore

org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

There are no other settings you need to worry about.

你无需在担心其他的配置了。

JDBCJobStore

JDBCJobStore is also aptly named - it keeps all of its data in a database via JDBC. Because of this it is a bit more complicated to configure than RAMJobStore, and it also is not as fast. However, the performance draw-back is not terribly bad, especially if you build the database tables with indexes on the primary keys. On fairly modern set of machines with a decent LAN (between the scheduler and database) the time to retrieve and update a firing trigger will typically be less than 10 milliseconds.

正如其命名一样,JDBCJobStore将所有的数据通过JDBC存在了数据库中。也正因如此其配置比RAMJobStore复杂,速度也没那么快。然而其性能也不是那么糟糕,尤其是在对数据库表创建索引之后。一个比较流行的方式是将scheduler和数据库部署在同一个局域网中,检索和更新的时间通常小于10毫秒。

JDBCJobStore works with nearly any database, it has been used widely with Oracle, PostgreSQL, MySQL, MS SQLServer, HSQLDB, and DB2. To use JDBCJobStore, you must first create a set of database tables for Quartz to use. You can find table-creation SQL scripts in the “docs/dbTables” directory of the Quartz distribution. If there is not already a script for your database type, just look at one of the existing ones, and modify it in any way necessary for your DB. One thing to note is that in these scripts, all the the tables start with the prefix “QRTZ_” (such as the tables “QRTZ_TRIGGERS”, and “QRTZ_JOB_DETAIL”). This prefix can actually be anything you’d like, as long as you inform JDBCJobStore what the prefix is (in your Quartz properties). Using different prefixes may be useful for creating multiple sets of tables, for multiple scheduler instances, within the same database.

JDBCJobStore几乎可以用任意的数据库,例如Oracle、PostgreSQL、MySQL、MS SQLServer、HSQLDB和DB2。使用JDBCJobStore之前你需要先创建供Quartz使用的数据表。在"docs/dbTables"目录下可以找到相对应的建表语句。如果没有找到你需要的数据库建表语句,你可以参考类似的写法创建自己的。所有的表前缀都是"QRTZ_"(例如"QRTZ_TRIGGERS"和"QRTZ_JOB_DETAIL")。你也可以通过Quartz配置文件来指定任何你希望使用的前缀。使用不同的前缀在不同scheduler实例使用同一个数据库的时候很有帮助。

Once you’ve got the tables created, you have one more major decision to make before configuring and firing up JDBCJobStore. You need to decide what type of transactions your application needs. If you don’t need to tie your scheduling commands (such as adding and removing triggers) to other transactions, then you can let Quartz manage the transaction by using JobStoreTX as your JobStore (this is the most common selection).

在创建完表以后,你还有需要做出一个重要决定。是需要决定在应用程序中使用哪种事务。如果你不需要将调度命令绑定到其他事务中,你可以使用通过Quartz提供的JobStoreTX来管理事务(只是最常规的方式)。

If you need Quartz to work along with other transactions (i.e. within a J2EE application server), then you should use JobStoreCMT - in which case Quartz will let the app server container manage the transactions.

如果你想在Quartz在另一个事务中沿用(例如在J2EE的应用程序汇总),你可以使用JobStoreCMT,这样Quartz允许应用程序来管理事务。

The last piece of the puzzle is setting up a DataSource from which JDBCJobStore can get connections to your database. DataSources are defined in your Quartz properties using one of a few different approaches. One approach is to have Quartz create and manage the DataSource itself - by providing all of the connection information for the database. Another approach is to have Quartz use a DataSource that is managed by an application server that Quartz is running inside of - by providing JDBCJobStore the JNDI name of the DataSource. For details on the properties, consult the example config files in the “docs/config” folder.

最后一步是设置数据库的数据源。有几个不同的方法可以配置数据源。一种策略是提供数据库的所有连接信息,让Quartz自己创建和管理数据源。另一种策略是通过提供数据源的JNDI名称给JDBCJobStore,然后通过应用程序来管理数据源,最后将Quartz嵌入到应用程序中来使用。配置的详细信息可以在"docs/config"目录中来查看。

To use JDBCJobStore (and assuming you’re using StdSchedulerFactory) you first need to set the JobStore class property of your Quartz configuration to be either org.quartz.impl.jdbcjobstore.JobStoreTX or org.quartz.impl.jdbcjobstore.JobStoreCMT - depending on the selection you made based on the explanations in the above few paragraphs.

在使用JDBCJobStore之前,需要配置JobStore的class,可以用org.quartz.impl.jdbcjobstore.JobStoreTX或者org.quartz.impl.jdbcjobstore.JobStoreCMT,这取决于你对上面叙事的理解。

Configuring Quartz to use JobStoreTx


org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX

Next, you need to select a DriverDelegate for the JobStore to use. The DriverDelegate is responsible for doing any JDBC work that may be needed for your specific database. StdJDBCDelegate is a delegate that uses “vanilla” JDBC code (and SQL statements) to do its work. If there isn’t another delegate made specifically for your database, try using this delegate - we’ve only made database-specific delegates for databases that we’ve found problems using StdJDBCDelegate with (which seems to be most!). Other delegates can be found in the “org.quartz.impl.jdbcjobstore” package, or in its sub-packages. Other delegates include DB2v6Delegate (for DB2 version 6 and earlier), HSQLDBDelegate (for HSQLDB), MSSQLDelegate (for Microsoft SQLServer), PostgreSQLDelegate (for PostgreSQL), WeblogicDelegate (for using JDBC drivers made by Weblogic), OracleDelegate (for using Oracle), and others.

接下来你需要选择JobStore的驱动。DriverDelegate负责与你指定数据库进行JDBC的任务。StdJDBCDelegate通过JDBC代码(和SQL声明)来执行它的工作。如果你的数据库没有任何一种代表,可以使用这个策略 - 我们给使用StdJDBCDelegate有问题的数据库提供了特殊的代表。其他的代表可以在"org.quartz.impl.jdbcjobstore"包或其子包中找到,的例如DB2v6Delegate(给DB2版的第六版或早期版本)、HSQLDBDelegate(给HSQLDB)、MSSQLDelegate(给微软的SQLServer)、PostgreSQLDelegate(给PostgreSQL)、WeblogicDelegate(给Weblogic)、OracleDelegate(给Oracle)等等。

Once you’ve selected your delegate, set its class name as the delegate for JDBCJobStore to use.

你可以设置你想要的代表的类名给JDBCJobStore来使用:

Configuring JDBCJobStore to use a DriverDelegate


org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate

Next, you need to inform the JobStore what table prefix (discussed above) you are using.

接下来可以设置JobStore标的前缀: 

Configuring JDBCJobStore with the Table Prefix


org.quartz.jobStore.tablePrefix = QRTZ_

 And finally, you need to set which DataSource should be used by the JobStore. The named DataSource must also be defined in your Quartz properties. In this case, we’re specifying that Quartz should use the DataSource name “myDS” (that is defined elsewhere in the configuration properties).

最后,你需要配置数据源。你需要在Quartz配置文件中指定数据源。在这里,我们指定Quartz使用名字为"myDS"的数据源(myDS在其他的配置文件中定义)。

Configuring JDBCJobStore with the name of the DataSource to use


org.quartz.jobStore.dataSource = myDS

If your Scheduler is busy (i.e. nearly always executing the same number of jobs as the size of the thread pool, then you should probably set the number of connections in the DataSource to be the about the size of the thread pool + 2. 

如果你的Scheduler很忙碌(例如执行的任务数量和线程数相同,因此你可以将设置数据源连接数量时线程数量+2)。 

The "org.quartz.jobStore.useProperties" config parameter can be set to "true" (defaults to false) in order to instruct JDBCJobStore that all values in JobDataMaps will be Strings, and therefore can be stored as name-value pairs, rather than storing more complex objects in their serialized form in the BLOB column. This is much safer in the long term, as you avoid the class versioning issues that there are with serializing your non-String classes into a BLOB.

"org.quartz.jobStore.useProperties"配置参数可以设置为“true”(默认值为false),以指示JDBCJobStore将JobDataMaps中所有的值都指定为字符串,因此可以以名称-值对而存储,而不是存储更复杂的对象序列化形式的BLOB列。从长远来看这更安全,因为你避免序列化的类版本问题将非字符串类存到BLOB中。

TerracottaJobStore

TerracottaJobStore provides a means for scaling and robustness without the use of a database. This means your database can be kept free of load from Quartz, and can instead have all of its resources saved for the rest of your application.

TeracottaJobStore提供了一种不基于数据库的扩展性和健壮性。这说明你的数据库可以通过Quartz自由的负载,且也可以在应用程序重置期间替换所有的资源信息。

TerracottaJobStore can be ran clustered or non-clustered, and in either case provides a storage medium for your job data that is persistent between application restarts, because the data is stored in the Terracotta server. It’s performance is much better than using a database via JDBCJobStore (about an order of magnitude better), but fairly slower than RAMJobStore.

TerracottaJobStore能够以集群或者非集群的方式提供一个存储介质来保证任务数据能够在应用程序重启期间仍然得到持久化,这是应为数据都存储在了Terracotta服务中。这比通过JDBCJobStore使用数据源的方式有更好的性能(大约一个量级),但是比RAMJobStore的性能弱。

To use TerracottaJobStore (and assuming you’re using StdSchedulerFactory) simply specify the class name org.quartz.jobStore.class = org.terracotta.quartz.TerracottaJobStore as the JobStore class property that you use to configure quartz, and add one extra line of configuration to specify the location of the Terracotta server:

你只需要简单的指定类名org.quartz.jobStore.class = org.terracotta.quartz.TerracottaJobStore作为JobStore类配置就能使用TerracottaJobStore,同时还需要指定Teracotta服务的地址。

Configuring Quartz to use TerracottaJobStore


org.quartz.jobStore.class = org.terracotta.quartz.TerracottaJobStore
org.quartz.jobStore.tcConfigUrl = localhost:9510

More information about this JobStore and Terracotta can be found at http://www.terracotta.org/quartz

关于这类JobStore和Terracotta的更多信息你可以参考这里:http://www.terracotta.org/quartz

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值