1

我写了下面的自定义作业监听器。(不是一个非常健壮的)。

有没有办法通过 .config 连接这个监听器(在更具体的元素中)

“GranadaCoder.Apps.QuartzPOC.BAL.Listeners.MyFirstJobListener,GranadaCoder.Apps.QuartzPOC”

后续问题是..您可以添加多个自定义 JobListener 吗?

我看到了对象模型 (C#) 代码。我只是没有通过.config 看到它。

IJobListener jobListener001 = new MyFirstJobListener ();
sched.ListenerManager.AddJobListener(jobListener001, GroupMatcher<JobKey>.AnyGroup());

谢谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Common.Logging;
using Quartz;

/* Assembly Name = "GranadaCoder.Apps.QuartzPOC" */
namespace GranadaCoder.Apps.QuartzPOC.BAL.Listeners
{
    public class MyFirstJobListener : IJobListener
    {
        public void JobExecutionVetoed(IJobExecutionContext context)
        {

            string msg = string.Format("    MyFirstJobListener : JobExecutionVetoed fired.  Key = '{0}'.", context.JobDetail.Key);

            Console.WriteLine(msg);
            ILog log = LogManager.GetLogger(typeof(MyFirstJobListener));
            log.Info(msg);

        }

        public void JobToBeExecuted(IJobExecutionContext context)
        {
            string msg = string.Format("    MyFirstJobListener : JobToBeExecuted fired.  Key = '{0}'.", context.JobDetail.Key);

            Console.WriteLine(msg);
            ILog log = LogManager.GetLogger(typeof(MyFirstJobListener));
            log.Info(msg);
        }

        public void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException)
        {
            string msg = string.Format("    MyFirstJobListener : JobWasExecuted fired.  Key = '{0}'.", context.JobDetail.Key);

            Console.WriteLine(msg);
            ILog log = LogManager.GetLogger(typeof(MyFirstJobListener));
            log.Info(msg);

            if (null != jobException)
            {

                StringBuilder sb = new StringBuilder();
                Exception exc = jobException;
                while (null != exc)
                {
                    sb.Append(exc.Message + " --- ");
                    exc = exc.InnerException;
                }

                string sbMsg = sb.ToString();
                msg = string.Format("    MyFirstJobListener : JobWasExecuted fired.  jobException.Message = '{0}'.", sbMsg);
                Console.WriteLine(msg);
                log.Info(msg);

            }
        }

        public string Name
        {
            get 
            {
                return "    MyFirstJobListener : MyFirstJobListener.Name Property";
            }
        }
    }
}
4

1 回答 1

2

所以我想出了一些事情。

有 2 个“内置”监听器。

你可以把它们连接起来。并获得基本的“ILog”事件。

让我失望的是“triggHistory”和“jobHistory”这两个名字。

这些是任意的。源代码中不存在“triggHistory”。

所以你必须做两件事。实现您感兴趣的侦听器接口并实现“ISchedulerPlugin”。(“ISchedulerPlugin”是什么在欺骗我)

例子:

namespace MyNamespace
{
  public class MySuperCoolJobListener : IJobListener, ISchedulerPlugin
  { /* bunch of stuff */ }


  public class MySuperCoolTriggerListener : ITriggerListener, ISchedulerPlugin
  { /* bunch of stuff */ }

  public class MySuperCoolSchedulerListener : ISchedulerListener , ISchedulerPlugin
  { /* bunch of stuff */ }

}

然后你可以将它们添加到 .config(uration) 中。

<add key="quartz.plugin.WhateverNameIWantHereJobHistory.type" value="MyNamespace.MySuperCoolJobListener, MyAssembly" />
<add key="quartz.plugin.WhateverNameIWantHereTriggerHistory.type" value="MyNamespace.MySuperCoolTriggerListener, MyAssembly" />
<add key="quartz.plugin.WhateverNameIWantHereSchedulerHistory.type" value="MyNamespace.MySuperCoolSchedulerListener, MyAssembly" />

您可以添加多个“集合”。

<add key="quartz.plugin.PooptyDoopJobHistory.type" value="MyNamespace.MyTotallyRadJobListener, MyOtherAssembly" />
<add key="quartz.plugin.PooptyDoopTriggerHistory.type" value="MyNamespace.MyTotallyRadTriggerListener, MyOtherAssembly" />
<add key="quartz.plugin.PooptyDoopSchedulerHistory.type" value="MyNamespace.MyTotallyRadSchedulerListener, MyOtherAssembly" />

DotNetAssembly 的价值当然很重要。

“quartz.plugin”的前缀。是重要的事情。

而是“quartz.plugin”之间的值。“.type”有点随意。那是活动扳手。

回到它的工作原理:框架将尝试实例化它们......

quartz.net 源代码面包屑是:

    public const string PropertyPluginPrefix = "quartz.plugin";

public class StdSchedulerFactory : ISchedulerFactory
{}

        // Set up any SchedulerPlugins
        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        IList<string> pluginNames = cfg.GetPropertyGroups(PropertyPluginPrefix);
        ISchedulerPlugin[] plugins = new ISchedulerPlugin[pluginNames.Count];
        for (int i = 0; i < pluginNames.Count; i++)
        {
            NameValueCollection pp = cfg.GetPropertyGroup("{0}.{1}".FormatInvariant(PropertyPluginPrefix, pluginNames[i]), true);

            string plugInType = pp[PropertyPluginType];

            if (plugInType == null)
            {
                initException = new SchedulerException("SchedulerPlugin type not specified for plugin '{0}'".FormatInvariant(pluginNames[i]));
                throw initException;
            }
            ISchedulerPlugin plugin;
            try
            {
                plugin = ObjectUtils.InstantiateType<ISchedulerPlugin>(LoadType(plugInType));
            }
            catch (Exception e)
            {
                initException = new SchedulerException("SchedulerPlugin of type '{0}' could not be instantiated.".FormatInvariant(plugInType), e);
                throw initException;
            }
            try
            {
                ObjectUtils.SetObjectProperties(plugin, pp);
            }
            catch (Exception e)
            {
                initException = new SchedulerException("JobStore SchedulerPlugin '{0}' props could not be configured.".FormatInvariant(plugInType), e);
                throw initException;
            }
            plugins[i] = plugin;
        }

如果这一切都没有清除......然后找到这个quartz.net代码。

LoggingJobHistoryPlugin.cs

public class LoggingJobHistoryPlugin : ISchedulerPlugin, IJobListener
{

LoggingTriggerHistoryPlugin.cs

public class LoggingTriggerHistoryPlugin : ISchedulerPlugin, ITriggerListener
{

(Quartz.Net 似乎没有编写默认/包含的 ISchedulerListener ,仅供参考)

清如泥?

于 2014-02-12T23:49:40.543 回答