Quartz.NET教程_Lesson 9: JobStores

课程9:作业存储器

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 IJobStore implementation 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.

作业存储器负责的是用来给你的调度器组件存储并提供工作数据:作业,触发器,日历等等。为你的Quartz调度器实例选择合适的IJobStore(作业存储器接口)实现是非常重要的一步。幸运的是,你一旦理解不同存储器之间的区别,使用他们就是一个很容易的事情。你可以声明你的调度器使用哪个存储器(和其配置设定)并将信息存在属性配置文件中,并用来提供给SchedulerFactory(调度器工厂)用来创建你的调度器实例。

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.
永远不要直接在代码中使用作业存储器实例。因为各种原因,有很多人都尝试这么做。存储器本身应该是在Quartz框架中处于幕后的角色。你必须要告诉你的Quartz框架(通过配置)去具体使用哪个存储器,但是你应该在你的代码中通过调度器的接口来进行使用。


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 disasterous.
内存存储器是使用最简单的存储器,同时其也是性能最好的存储器(从CPU计算时间来看)。内存存储器的命名是一个很直白的方式:其将所有的数据都存储在内存中。这就是其为什么速度如此之快,而且与此同时配置简单的原因。而劣势就是当你的应用程序结束了生命周期(或者崩溃)所有调度任务中的信息都会丢失 - 这意味着内存存储器并不不能兑现作业和触发器的“非易失性”。对于某些应用而言这是可以接受的 - 甚至是被期望的行为,但是对于另一些应用来说,则可能意味着灾难性的后果。

Configuring Quartz to use RAMJobStore
配置Quartz框架使用内存存储器

quartz.jobStore.type = Quartz.Simpl.RAMJobStore, Quartz

To use RAMJobStore (and assuming you’re using StdSchedulerFactory) you don’t need to do anything special. Default configuration of Quartz.NET uses RAMJobStore as job store implementation.
为了使用内存存储器(假设你正在使用StdSchedulerFactory工厂)你并不需要做什么特别的配置。Quartz.NET框架默认的配置使用内存存储器作为作业存储的实现。


ADO.NET Job Store (AdoJobStore)
ADO.NET作业存储

AdoJobStore is also aptly named - it keeps all of its data in a database via ADO.NET. 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.
AdoJobStore的命名也是很巧妙滴 - 它通过ADO.NET将所有的数据存储在数据库中。因为这样的方式进行配置比内存作业存储器稍微复杂一些,而且也没有那么快。然而,其性能弱点也并非那么差,尤其是当你为你数据库的主键创建了索引之后。

To use AdoJobStore, you must first create a set of database tables for Quartz.NET to use. You can find table-creation SQL scripts in the “database/dbtables” directory of the Quartz.NET 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 AdoJobStore what the prefix is (in your Quartz.NET properties). Using different prefixes may be useful for creating multiple sets of tables, for multiple scheduler instances, within the same database.
在使用AdoJobStore之前,你必须为 Quartz.NET创建一组数据表。你可以在Quartz.NET框架的压缩包的“database/dbtables”目录中找到详细的SQL执行语句。如果没有针对你使用的数据库的执行语句,你可以将其中任意一种数据库的脚本单独取出,修改成对应的数据库。有件事情需要注意,在这些脚本中,所有数据表都以“QRTZ_”开头,比如“QRTZ_TRIGGERS”,“QRTZ_JOB_DETAIL”。这个前缀实际上可以是任何你希望的值,只要你告知你的AdoJobStore那个是固定前缀就行(在你的Quartz.NET属性文件中配置)。在使用不同的前缀的时候,当你想要在同一个数据库中创建几组不同的表格,为不同的调度器实例提供数据的时候很有用。

Currently the only option for the internal implementation of job store is JobStoreTX which creates transactions by itself. This is different from Java version of Quartz where there is also option to choose JobStoreCMT which uses J2EE container managed transactions.
当前作业存储器内部实现的唯一选项是JobStoreTX,其自己创建事务。这个和java版本的Quartz不太一样,在java的版本中,还有另一个选择JobStoreCMT,其使用J2EE容器管理具体的事务。

The last piece of the puzzle is setting up a data source from which AdoJobStore can get connections to your database. Data sources are defined in your Quartz.NET properties. Data source information contains the connection string and ADO.NET delegate information.
谜语的最后一步是设置一个数据源,这样一来你的AdoJobStore存储器就可以连接上你的数据库。数据源是通过Quartz.NET的属性进行定义的。数据源的信息包含数据库连接字符串和ADO.NET代理信息。

Configuring Quartz to use JobStoreTx
配置Quart框架使用JobStoreTx


quartz.jobStore.type = Quartz.Impl.AdoJobStore.JobStoreTX, Quartz

Next, you need to select a IDriverDelegate implementation for the JobStore to use. The DriverDelegate is responsible for doing any ADO.NET work that may be needed for your specific database. StdAdoDelegate is a delegate that uses “vanilla” ADO.NET code (and SQL statements) to do its work. If there isn’t another delegate made specifically for your database, try using this delegate - special delegates usually have better performance or workarounds for database specific issues. Other delegates can be found in the “Quartz.Impl.AdoJobStore” namespace, or in its sub-namespaces.
接下来,你需要为你的作业存储器选择一个IDriverDelegate的接口实现。DriverDelegate(驱动代理?)的作用是对你的特定的数据库进行必须的ADO.NET操作。StdAdoDelegate代理使用“vanilla”ADO.NET代码实现其操作。如果没有另一个特定的代理操作你的数据库,你可以试试这个 - 特定的代理通常对于特定的数据库处理细节有更好的性能。另一些代理可以在命名空间“Quartz.Impl.AdoJobStore”中找到,或者其子空间中找到。


NOTE: Quartz.NET will issue warning if you are using the default StdAdoDelegate as it has poor performance when you have a lot of triggers to select from. Specific delegates have special SQL to limit result set length (SQLServerDelegate uses TOP n, PostgreSQLDelegate LIMIT n, OracleDelegate ROWCOUNT() <= n etc.).
注意: 当你在查询大量的触发器信息的时候,Quartz.NET框架会发出警告,因为StdAdoDelegate标准代理的性能相对较差。特定的代理有一些特定的SQL语句去限制结果集的长度(SQLServerDelegate uses TOP n, PostgreSQLDelegate LIMIT n, OracleDelegate ROWCOUNT() <= n etc.)。

Once you’ve selected your delegate, set its class name as the delegate for AdoJobStore to use.
一旦你选择了你自己的代理,一定要将类名设置为代理以供AdoJobStore存储器使用。


Configuring AdoJobStore to use a DriverDelegate
配置AdoJobStore使用DriverDelegate


quartz.jobStore.driverDelegateType = Quartz.Impl.AdoJobStore.StdAdoDelegate, Quartz

Next, you need to inform the JobStore what table prefix (discussed above) you are using.
接下来,你需要通知作业存储器,你正在使用的表前缀是什么。

Configuring AdoJobStore with the Table Prefix
给AdoJobStore存储器配置表前缀

quartz.jobStore.tablePrefix = QRTZ_

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

Configuring AdoJobStore with the name of the data source to use
给AdoJobStore配置使用的数据源的名称

quartz.jobStore.dataSource = myDS

One last thing that is needed for the configuration is to set data source connection string information and database provider. Connection string is the standard ADO.NET connection which is driver specific. Database provider is an abstraction of database drivers to create loose coupling between database drivers and Quartz.
配置工作中,你需要做的最后一件事情是设置数据源的数据库连接字符串信息和数据库提供者。连接字符串是标准的ADO.NET连接串。数据库提供者是一个抽象的数据库驱动,用来帮你的数据库驱动和你的Quartz框架进行解耦。

Setting Data Source’s Connection String And Database Provider
设置数据源的数据库连接字符串和数据库提供者

 quartz.dataSource.myDS.connectionString = Server=localhost;Database=quartz;Uid=quartznet;Pwd=quartznet
 quartz.dataSource.myDS.provider = MySql-50

Currently following database providers are supported:
当前支持下面的这些数据库:

    SqlServer-20 - SQL Server driver for .NET Framework 2.0
    OracleODP-20 - Oracle’s Oracle Driver
    OracleODPManaged-1123-40 Oracle’s managed driver for Oracle 11
    OracleODPManaged-1211-40 Oracle’s managed driver for Oracle 12
    MySql-50 - MySQL Connector/.NET v. 5.0 (.NET 2.0)
    MySql-51 - MySQL Connector/:NET v. 5.1 (.NET 2.0)
    MySql-65 - MySQL Connector/:NET v. 6.5 (.NET 2.0)
    SQLite-10 - SQLite ADO.NET 2.0 Provider v. 1.0.56 (.NET 2.0)
    Firebird-201 - Firebird ADO.NET 2.0 Provider v. 2.0.1 (.NET 2.0)
    Firebird-210 - Firebird ADO.NET 2.0 Provider v. 2.1.0 (.NET 2.0)
    Npgsql-20 - PostgreSQL Npgsql

You can and should use latest version of driver if newer is available, just create an assembly binding redirect
你可以并且应该使用最新的数据库驱动,如果有最新的版本,只要创建一个编译绑定重定向。

If your Scheduler is very 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 data source to be the about the size of the thread pool + 1.This is commonly configured int the ADO.NET connection string - see your driver implementation for details.
如果你的调度器很忙(比如总是在执行最高线程数的作业量),那么你应该将你的数据库源的连接次数设置为比你的线程池大1.这个在 ADO.NET连接字符串的配置中是很常见的设置 - 在你的驱动实现中可以看到更多的实现细节。

The “quartz.jobStore.useProperties” config parameter can be set to “true” (defaults to false) in order to instruct AdoJobStore 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.

“quartz.jobStore.useProperties”参数可以被设置为“真”,这样的话在构建AdoJobStore存储前期的时候,所有的值都会是字符串类型,可以被以键值对的形式进行存储,而不是其他任意一种类型复杂对象的BLOB列的序列化后的类型。长期来看这样的形式会更加的安全,因为你可以避免当你的非字符串类型序列化成类文件时出现的版本问题。
(注:这段翻译因为本人数据库底层的知识不足,无法完美处理,等过段时间再弄吧。竟然连“Vanilla”是啥都不知道,擦~~~)

Configuring AdoJobStore to use strings as JobDataMap values (recommended)
配置AdoJobStore 使用字符串作为JobDataMap值(推荐)

quartz.jobStore.useProperties = true
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章