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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章