Quartz.net is an open-source framework for running tasks.
I'm working on windform.ProgramPrepare to integrate the quartz.net job framework to execute the job.
It is found that there is a conflict between the results when integrated.
Problem description:
If it is okay to call quartz.net when the winform program is started directly,
If the dialog box form is used, quartz reports an error. It is estimated that the UI thread and quartz thread
Conflict.
At this time, I want to use remote tasks to handle this conflict. In winform, I only manage tasks.
Task execution uses a remote server to execute the task. In this way, we can make the server
A Windows service. In this way, the task execution can be performed using the service.
The winform program has been opened.
Knowledge point:
1. Configure quartz.net for remote execution.
2. Configure database storage tasks.
1. configure remote execution:
Add an application configuration file to the server project.
Note: The configured port is 555, and the service name bound to the server is quartzscheduler.
<Quartz>
<Add key = "quartz. schedporter. exporter. Type" value = "quartz. simpl. remotingschedulerexporter, quartz"/>
<Add key = "quartz. schedporter. exporter. Port" value = "555"/>
<Add key = "quartz. schedporter. exporter. bindname" value ="Quartzscheduler"/>
<Add key = "quartz. schedporter. exporter. channeltype" value = "TCP"/>
</Quartz>
The configuration on the client is as follows:
<Quartz>
<Add key = "quartz. scheduler. instancename" value = "quartzscheduler"/>
<Add key = "quartz. threadpool. Type" value = "quartz. simpl. simplethreadpool, quartz"/>
<Add key = "quartz. threadpool. threadcount" value = "5"/>
<Add key = "quartz. threadpool. threadpriority" value = "normal"/>
<Add key = "quartz. scheduler. Proxy" value = "true"/>
<Add key = "quartz. scheduler. Proxy. Address" value = "TCP: // localhost: 555/Quartzscheduler"/>
</Quartz>
Note:
The service name that configures quartz. scheduler. Proxy. address must be consistent with that of the server,QuartzschedulerThis is
Name bound to the server.
ClientCodeAs follows:
ischedulerfactory Sf = new stdschedulerfactory ();
isched1_sched = SF. getschedute ();
// computer a time that is on the next round minute
datetime runtime = triggerutils. getevenminutedate (datetime. utcnow);
// Define the job and tie it to our hellojob class
Jobdetail job = new jobdetail ("job1", "group1", typeof (job. hellojob ));
Jobdatamap map = new jobdatamap ();
Map. Add ("name", "test ");
Map. Add ("ID", 1 );
Job. jobdatamap = map;
// Trigger the job to run on the next round minute
Simpletrigger trigger = new simpletrigger ("trigger1", "group1", runtime );
// Tell quartz to schedule the job using our trigger
Sched. schedulejob (job, trigger );
At this time, we need to note that we implemented the ijob interface when we customized the task to realize that the custom task has completed our work.
The hellojob Assembly needs to be found when the server executes the task. Therefore, after compiling the task into a separate library project,
In the directory of the server-side executable program, the service program has found the assembly to be executed.
2. Configure the database storage service.
The advantage of configuring a task to the database is that after the server is restarted, the task can still be loaded in the database.
To make it easier for us to use a lightweight database to implement this, I chose sqllite.
When configuring sqllite, we may encounter an error where the system. Data. SQLite. dll assembly cannot be found.
After research, we found that the database supported by quartz.net is configured in impl \ adojobstore \ common \ dbproviders. Properties of the source code.
Find the sqllite code in the file.
Quartz. dbprovider. SQLite-10.assemblyName = system. Data. SQLite, version = 1.0.56.0, culture = neutral, publickeytoken = db937bc2d44ff139
Quartz. dbprovider. SQLite-10.connectionType = system. Data. SQLite. sqliteconnection, system. Data. SQLite, version = 1.0.56.0, culture = neutral, publickeytoken = db937bc2d44ff139
Quartz. dbprovider. SQLite-10.commandType = system. Data. SQLite. sqlitecommand, system. Data. SQLite, version = 1.0.56.0, culture = neutral, publickeytoken = db937bc2d44ff139
Quartz. dbprovider. SQLite-10.parameterType = system. Data. SQLite. sqliteparameter, system. Data. SQLite, version = 1.0.56.0, culture = neutral, publickeytoken = db937bc2d44ff139
# Quartz. dbprovider. SQLite-10.dataAdapterType = system. Data. SQLite. sqlitedataadapter, system. Data. SQLite, version = 1.0.56.0, culture = neutral, publickeytoken = db937bc2d44ff139
Quartz. dbprovider. SQLite-10.commandBuilderType = system. Data. SQLite. sqlitecommandbuilder, system. Data. SQLite, version = 1.0.56.0, culture = neutral, publickeytoken = db937bc2d44ff139
# Quartz. dbprovider. SQLite-10.commandBuilderDeriveParametersMethod = deriveparameters
Quartz. dbprovider. SQLite-10.parameterDbType = system. Data. SQLite. typeaffinity, system. Data. SQLite, version = 1.0.56.0, culture = neutral, publickeytoken = db937bc2d44ff139
Quartz. dbprovider. SQLite-10.parameterDbTypePropertyName = dbtype
# Quartz. dbprovider. SQLite-10.parameterIsNullableProperty = isnullable
Quartz. dbprovider. SQLite-10.parameterNamePrefix = @
Quartz. dbprovider. SQLite-10.exceptionType = system. Data. SQLite. sqliteexception, system. Data. SQLite, version = 1.0.56.0, culture = neutral, publickeytoken = db937bc2d44ff139
Quartz. dbprovider. The SQLite-10.useParameterNamePrefixInParameterCollection = true
Quartz. dbprovider. The SQLite-10.bindByName = true
The version I downloaded is 1.0.60.0. I replaced version = 1.0.56.0 with version = 1.0.60.0. In addition, I replaced publickeytoken with null.
After editing, recompile the source code project.
Create an SQLite database for mydb. DB and use the tables_sqlite. SQL script to create the tables required by quartz.
Edit the app. config file]
<Quartz>
<Add key = "quartz. jobstore. misfirethreshold" value = "60000" type = "codeph" text = "/codeph"/>
<Add key = "quartz. jobstore. Type" value = "quartz. impl. adojobstore. jobstoretx, quartz"/>
<Add key = "quartz. jobstore. driverdelegatetype" value = "quartz. impl. adojobstore. stdadodelegate, quartz"/>
<Add key = "quartz. jobstore. useproperties" value = "false"/>
<Add key = "quartz. jobstore. datasource" value ="Default"/>
<Add key = "quartz. jobstore. tableprefix" value = "qrtz _"/>
<Add key = "quartz. datasource. Default. connectionstring" value = "Data Source = mydb. DB"/>
<Add key = "quartz. jobstore. lockhandler. Type" value = "quartz. impl. adojobstore. updatelockrowsemaphore, quartz"/>
<Add key = "quartz. datasource.Default. Provider "value =" SQLite-10 "/>
</Quartz>
In this way, the server supports storing tasks using databases.