如何在 Quartz.Net 中让 Job 引发 EventHandler?

发布于 2024-12-26 14:29:43 字数 394 浏览 1 评论 0原文

我有一个实例化 Quartz Scheduler 的控制台应用程序。
我希望作业能够引发事件处理程序,以便“母应用程序”调用特定的方法。
问题是 Job 类似乎与外部类明显是相当隔离的。
我确信有一个好方法可以做到这一点,但我还没有偶然发现它。

public class RestartJob : IJob
{        
    public RestartJob()
    {
    }

    public virtual void Execute(IJobExecutionContext context)
    {
        //Send Restart EventHandler Subscription to Console.            
    }
}

I have a Console Application that instantiates a Quartz Scheduler.
I would like a Job to raise an event Handler so that the "Mother App" calls a particular Method.
The problem is that the Job Class seems to be pretty isolated to the external classes apparently.
I am sure there is a good way to do this but I did not stumble upon it yet.

public class RestartJob : IJob
{        
    public RestartJob()
    {
    }

    public virtual void Execute(IJobExecutionContext context)
    {
        //Send Restart EventHandler Subscription to Console.            
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〃安静 2025-01-02 14:29:43

我通过在包含调度程序逻辑的类上实现单例来解决我的问题。
很有魅力。希望这可以帮助其他人解决在我看来“必须具备”的功能。

public class Skeduler
{
    private static Skeduler instance;

    public static Skeduler Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new Skeduler();
            }
            return instance;
        }
    }

    public delegate void SendRestartX();
    public event SendRestartX SendRestart;

    public void doSendRestart()
    {
        if (SendRestart!=null)
            SendRestart();
    }

    //(Job Methods & Logics Goes Here)

 }

public class RestartJob : IJob
{
    //Required
    public RestartJob()
    {
    }

    public virtual void Execute(IJobExecutionContext context)
    {
        Skeduler.Instance.doSendRestart();
    }

}

用法 :

    public MainClass
    {   
        public void Run()
        {
          skeduler = Skeduler.Instance;
          skeduler.SendRestart += new Skeduler.SendRestartX(MethodToCall);
        }
    }

I solved my problem by implementing a singleton on the class containing the Scheduler Logic.
Works a charm. Hope this could help other to solve what seems to me as a "Must-Have" feature.

public class Skeduler
{
    private static Skeduler instance;

    public static Skeduler Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new Skeduler();
            }
            return instance;
        }
    }

    public delegate void SendRestartX();
    public event SendRestartX SendRestart;

    public void doSendRestart()
    {
        if (SendRestart!=null)
            SendRestart();
    }

    //(Job Methods & Logics Goes Here)

 }

public class RestartJob : IJob
{
    //Required
    public RestartJob()
    {
    }

    public virtual void Execute(IJobExecutionContext context)
    {
        Skeduler.Instance.doSendRestart();
    }

}

Usage :

    public MainClass
    {   
        public void Run()
        {
          skeduler = Skeduler.Instance;
          skeduler.SendRestart += new Skeduler.SendRestartX(MethodToCall);
        }
    }
倒带 2025-01-02 14:29:43

您可以在 JobDataMap 中传递事件处理程序,然后使用作业中的事件处理程序。

创建工作时

IJobDetail job = JobBuilder.Create<RestartJob>().WithIdentity("job"), "group")
        .SetJobData(new JobDataMap{{"event-handler", YourEventHandler}});

在工作类别上:

public class RestartJob : IJob
{        
    public RestartJob()
    {
    }

    public virtual void Execute(IJobExecutionContext context)
    {
        var @event = context.JobDetail.JobData["event-handler"];
        @event?.Invoke(YourEventParameters); 
    }
}

You can pass the event handler within a JobDataMap and then use the event handler from your job.

When Creating your job

IJobDetail job = JobBuilder.Create<RestartJob>().WithIdentity("job"), "group")
        .SetJobData(new JobDataMap{{"event-handler", YourEventHandler}});

On your job class:

public class RestartJob : IJob
{        
    public RestartJob()
    {
    }

    public virtual void Execute(IJobExecutionContext context)
    {
        var @event = context.JobDetail.JobData["event-handler"];
        @event?.Invoke(YourEventParameters); 
    }
}
喜爱皱眉﹌ 2025-01-02 14:29:43

你可以试试这个:
创建 IJobDetail :

IJobDetail job = JobBuilder.Create<LogJob>().WithIdentity("job"), "group")
        .UsingJobData("id", 123).Build();
job.JobDataMap["SOMENAME"] = this;

用法:

public void Execute(IJobExecutionContext context) {
    JobDataMap dataMap = context.JobDetail.JobDataMap;
    CLASSNAME SOMENAME = dataMap["SOMENAME"] as MediaPlaylistsAds;
    int id = (int)dataMap["id"];
    SOMENAME.SOMEFUNC(id);
}

You can try this :
Create IJobDetail :

IJobDetail job = JobBuilder.Create<LogJob>().WithIdentity("job"), "group")
        .UsingJobData("id", 123).Build();
job.JobDataMap["SOMENAME"] = this;

Usage :

public void Execute(IJobExecutionContext context) {
    JobDataMap dataMap = context.JobDetail.JobDataMap;
    CLASSNAME SOMENAME = dataMap["SOMENAME"] as MediaPlaylistsAds;
    int id = (int)dataMap["id"];
    SOMENAME.SOMEFUNC(id);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文