context.Trigger.JobDataMap值通过xml配置

时间:2014-01-31 17:56:32

标签: quartz.net

在quartz_jobs.xml中,我可以为作业设置一些参数......

   <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>

这是代码:

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");

    }
}

现在,我可以“编写一个工作并触发”(不使用.xml设置)(代码未见)....我可以得到以下工作。 (并且已为triggerParameter001Value和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");

    }
}

但是我没有找到一种方法来传递触发器的参数...... 在xml 中定义。

我搜索了

"trigger-data-map"

"jobtrigger-data-map"

无济于事。

我也在“http://quartznet.sourceforge.net/JobSchedulingData”xsd附近钓鱼。 这只是xml中缺少的吗? 我错过了什么吗?

1 个答案:

答案 0 :(得分:3)

确定。这个是SNEAKY!

以下内容不起作用:(注意“简单”元素下“job-data-map”元素的位置)

   <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>

上面的xml给出了这样的错误:

Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin.ProcessFile错误调度作业时出错:名称空间“http://quartznet.sourceforge.net/JobSchedulingData”中的元素“simple”在名称空间中具有无效的子元素“job-data-map” 'http://quartznet.sourceforge.net/JobSchedulingData'。

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

以下将工作(注意“工作数据图”的位置变化(仍在“简单”元素下,但“向上”移动一些)

   <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>

为什么?

xsd使用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>

因此,必须在〜任何具体属性之前定义作为摘要一部分的所有内容。

那是偷偷摸摸的!