客户监听器通过配置添加

时间:2014-02-11 23:45:41

标签: quartz.net quartz.net-2.0

我写了下面的自定义工作监听器。 (不是非常强大的)。

有没有办法通过.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";
            }
        }
    }
}

1 个答案:

答案 0 :(得分: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源代码bread-crumbs是:

    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,fyi)

清除泥土?