【问题标题】:Customer Listener added through config通过配置添加的客户侦听器
【发布时间】:2014-02-11 23:45:40
【问题描述】:

我编写了以下自定义作业侦听器。 (不是很健壮)。

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

【问题讨论】:

    标签: quartz.net quartz.net-2.0


    【解决方案1】:

    所以我想出了一些事情。

    有 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 ,仅供参考)

    清如泥?

    【讨论】:

      猜你喜欢
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      相关资源
      最近更新 更多