context.Trigger.JobDataMap values via xml configuration

别说谁变了你拦得住时间么 提交于 2019-12-11 03:18:53

问题


In quartz_jobs.xml, I can set some parameters for the job.....

   <job>
        <name>MyJob</name>
        <group>MyJob</group>
        <description>My Job</description>
        <job-type>MyAssembly.MyJob, MyAssembly</job-type>
        <durable>true</durable>
        <recover>false</recover>
        <job-data-map>
            <entry>
                <key>JobMapDataKeyOne</key>
                <value>JobMapDataValueOne</value>
            </entry>
            <entry>
                <key>JobMapDataKeyTwo</key>
                <value>JobMapDataValueTwo</value>
            </entry>
        </job-data-map>

    </job>

and here is the code:

public class MyJob: IJob
{

    public virtual void Execute(IJobExecutionContext context)
    {

        JobKey key = context.JobDetail.Key;

        JobDataMap jbDataMap = context.JobDetail.JobDataMap;

        string jobMapDataValueOne = jbDataMap.GetString("JobMapDataKeyOne");
        string jobMapDataValueTwo = jbDataMap.GetString("JobMapDataKeyOne");

    }
}

Now, I can "code up a job and trigger" (not using .xml setup) (code not seen).... and I can get the below to work. (And have populated values for triggerParameter001Value and triggerParameter002Value ).

public class MyJob: IJob
{

    public virtual void Execute(IJobExecutionContext context)
    {

        JobKey key = context.JobDetail.Key;

        JobDataMap trgDataMap = context.Trigger.JobDataMap;

        string triggerParameter001Value = trgDataMap.GetString("TriggerParameter001Key");
        string triggerParameter002Value = trgDataMap.GetString("TriggerParameter002Key");

    }
}

But I don't see a way to pass parameters for the Trigger...defined in the xml.

I searched for

"trigger-data-map"

and

"jobtrigger-data-map"

to no avail.

I fished around the "http://quartznet.sourceforge.net/JobSchedulingData" xsd as well. Is this just missing in the xml? Am I missing something?


回答1:


Ok. This one was SNEAKY!

The below will NOT work: (note the position of "job-data-map" element ~under the "simple" element)

   <job>
        <name>MyJob</name>
        <group>MyJobGroup</group>
        <description>My Job</description>
        <job-type>MyAssembly.MyJob, MyAssembly</job-type>
        <durable>true</durable>
        <recover>false</recover>
        <job-data-map>
            <entry>
                <key>JobMapDataKeyOne</key>
                <value>JobMapDataValueOne</value>
            </entry>
            <entry>
                <key>JobMapDataKeyTwo</key>
                <value>JobMapDataValueTwo</value>
            </entry>
        </job-data-map>


    </job>

        <trigger>

            <simple>
                <name>MyTrigger</name>
                <group>MyTriggerJobGroup</group>
                <description>MyTriggerDescription</description>
                <job-name>MyJob</job-name>
                <job-group>MyJobGroup</job-group>


                <!--<start-time>1982-06-28T18:15:00.0Z</start-time>-->
                <!--<end-time>2020-05-04T18:13:51.0Z</end-time>-->
                <misfire-instruction>SmartPolicy</misfire-instruction>
                <!-- repeat indefinitely every 10 seconds -->
                <repeat-count>-1</repeat-count>
                <repeat-interval>5000</repeat-interval>


                <job-data-map>
                    <entry>
                        <key>TriggerParameter001Key</key>
                        <value>TriggerParameter001Value</value>
                    </entry>
                    <entry>
                        <key>TriggerParameter002Key</key>
                        <value>TriggerParameter002Value</value>
                    </entry>

                </job-data-map>


            </simple>

        </trigger>

The above xml was giving me an error like this:

Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin.ProcessFile Error Error scheduling jobs: The element 'simple' in namespace 'http://quartznet.sourceforge.net/JobSchedulingData' has invalid child element 'job-data-map' in namespace 'http://quartznet.sourceforge.net/JobSchedulingData'.

........................

The below WILL work (note the position change of "job-data-map" (still under "simple" element, but moved "up" some)

   <job>
        <name>MyJob</name>
        <group>MyJobGroup</group>
        <description>My Job</description>
        <job-type>MyAssembly.MyJob, MyAssembly</job-type>
        <durable>true</durable>
        <recover>false</recover>
        <job-data-map>
            <entry>
                <key>JobMapDataKeyOne</key>
                <value>JobMapDataValueOne</value>
            </entry>
            <entry>
                <key>JobMapDataKeyTwo</key>
                <value>JobMapDataValueTwo</value>
            </entry>
        </job-data-map>


    </job>

        <trigger>

            <simple>
                <name>MyTrigger</name>
                <group>MyTriggerJobGroup</group>
                <description>MyTriggerDescription</description>
                <job-name>MyJob</job-name>
                <job-group>MyJobGroup</job-group>


                <job-data-map>
                    <entry>
                        <key>TriggerParameter001Key</key>
                        <value>TriggerParameter001Value</value>
                    </entry>
                    <entry>
                        <key>TriggerParameter002Key</key>
                        <value>TriggerParameter002Value</value>
                    </entry>

                </job-data-map>



                <!--<start-time>1982-06-28T18:15:00.0Z</start-time>-->
                <!--<end-time>2020-05-04T18:13:51.0Z</end-time>-->
                <misfire-instruction>SmartPolicy</misfire-instruction>
                <!-- repeat indefinitely every 10 seconds -->
                <repeat-count>-1</repeat-count>
                <repeat-interval>5000</repeat-interval>



            </simple>

        </trigger>

Why?

The xsd uses an abstractType

  <xs:complexType name="abstractTriggerType" abstract="true">
    <xs:annotation>
      <xs:documentation>Common Trigger definitions</xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:element name="name" type="xs:string" />
      <xs:element name="group" type="xs:string" minOccurs="0" />
      <xs:element name="description" type="xs:string" minOccurs="0" />
      <xs:element name="job-name" type="xs:string" />
      <xs:element name="job-group" type="xs:string" minOccurs="0" />
      <xs:element name="priority" type="xs:nonNegativeInteger" minOccurs="0" />
      <xs:element name="calendar-name" type="xs:string" minOccurs="0" />
      <xs:element name="job-data-map" type="job-data-mapType" minOccurs="0" />



  <xs:complexType name="simpleTriggerType">
    <xs:annotation>
      <xs:documentation>Define a SimpleTrigger</xs:documentation>
    </xs:annotation>
    <xs:complexContent>
      <xs:extension base="abstractTriggerType">
        <xs:sequence>
          <xs:element name="misfire-instruction" type="simple-trigger-misfire-instructionType" minOccurs="0" />
          <xs:sequence minOccurs="0">
            <xs:element name="repeat-count" type="repeat-countType" />
            <xs:element name="repeat-interval" type="xs:nonNegativeInteger" />
          </xs:sequence>

So everything that is a part of the abstract has to be defined ~before~ any of the concrete properties.

That is sneaky!



来源:https://stackoverflow.com/questions/21486847/context-trigger-jobdatamap-values-via-xml-configuration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!