Quartz Scheduling Client Server

时间:2014-09-29 13:52:43

标签: c# quartz-scheduler

例外:::"计划程序名称为' RemoteServer'已经存在。"

我正在使用单身模式。并尝试不同的方式,但当我一次调用它执行正常,我的工作执行得很好。但是,当我安排第二份工作时,我遇到了这个问题。 "名称为' RemoteServer'已经存在。"     客户代码::

public  void SchedulenewAlert()
    {
        try
        {
            NameValueCollection properties = new NameValueCollection();
            properties["quartz.scheduler.instanceName"] = "RemoteClient";
            properties["quartz.scheduler.instanceId"] = "AUTO";
            // set thread pool info
         properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool,Quartz";
            properties["quartz.threadPool.threadCount"] = "5";
            properties["quartz.threadPool.threadPriority"] = "Normal";

            // set remoting expoter
          properties["quartz.scheduler.proxy"] = "true";
properties["quartz.scheduler.proxy.address"] = "tcp://10.0.0.46:555/QuartzScheduler";/QuartzScheduler";
            // First we must get a reference to a scheduler
            ISchedulerFactory sf = new StdSchedulerFactory(properties);
            IScheduler sched = sf.GetScheduler();

            String jobname = "remotelyAddedJob2" + DateTime.Now.Ticks.ToString();
            IJobDetail job = JobBuilder.Create<MyTest>()
                .WithIdentity(jobname, "default1")
                .Build();

            JobDataMap map = job.JobDataMap;
            map.Put("msg", "Your remotely added job has executed!");

            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("remotelyAddedTrigger", "default")
                .ForJob(job.Key)
                .WithCronSchedule("/5 * * ? * *")
                .Build();

var TestTrigger =  TriggerBuilder.Create().ForJob(job).WithIdentity("remotelyAddedTrigger2", "default1")
                .StartNow().Build();


DateTimeOffset pullReportsToQueuejobCalendar = DateBuilder.DateOf(9, 30, 17, 1, 6, 2005);
string trgName = "pullReportsFromQueuejobTrigger" + DateTime.Now.Ticks.ToString();
            var pullReportsToQueuejobTrigger = new CalendarIntervalTriggerImpl
            {
                StartTimeUtc = pullReportsToQueuejobCalendar,
                Name = trgName,
                RepeatIntervalUnit = IntervalUnit.Second,
                RepeatInterval = 40000    // every --- seconds
            };
            /// while (true)
            // {


            // schedule the job
            sched.ScheduleJob(job, pullReportsToQueuejobTrigger);

        }
        catch (Exception ex)
        {
            string lines = "==================Exception  Client===========================\n" + DateTime.Now.TimeOfDay.ToString() + "\n====================END=========================\n";
System.IO.StreamWriter file = new  System.IO.StreamWriter("c:\\ExceptionClient.txt", true);
            file.WriteLine(lines);
            file.Close();
        }
       ///}
    }

============================ 服务器职务代码....

public virtual void Execute(IJobExecutionContext context)
    {
        // This job simply prints out its job name and the
        // date and time that it is running
        try
        {
            string lines = "==================StartClient===========================\n"
            + DateTime.Now.TimeOfDay.ToString() + "\n====================END=========================\n";
            System.IO.StreamWriter file = new System.IO.StreamWriter("d:\\testClient.txt", true);
            file.WriteLine(lines);
            file.Close();
        }
        catch(Exception ex)
        {

            string lines = "==================Exception at Client  Calling===========================\n"
            + DateTime.Now.TimeOfDay.ToString() + "\n====================END=========================\n";
            System.IO.StreamWriter file = new System.IO.StreamWriter("d:\\testExceptionClient.txt", true);
            file.WriteLine(lines);
            file.WriteLine("EXception........... <<<< Message  "+ex.Message);
            file.Close();
}
}

2 个答案:

答案 0 :(得分:1)

这对我有用:

 IScheduler sched = sf.GetScheduler("scheduler name");
        if (sched == null)
        {
            sched = sf.GetScheduler();
        }

答案 1 :(得分:0)

你混淆逻辑来处理事情 这是管理Quartz的好方法

     public class SchedularHandler
        {

            private static IScheduler _scheduler;

            public IScheduler Scheduler
            {
                get
                {
                    if (_scheduler == null)
                        _scheduler = GetScheduler();
                    return _scheduler;
                }

            }
            public SchedularHandler()
            {

                if (_scheduler == null)
                    _scheduler = GetScheduler();

                if (!_scheduler.IsStarted)
                    _scheduler.Start();
            }
            private NameValueCollection GetProperties()
            {
                NameValueCollection properties = new NameValueCollection();
               properties["quartz.scheduler.instanceName"] = "RemoteClient";
               properties["quartz.scheduler.instanceId"] = "AUTO";
               // set thread pool info
                properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool,Quartz";
               properties["quartz.threadPool.threadCount"] = "5";
              properties["quartz.threadPool.threadPriority"] = "Normal";

              // set remoting expoter
              properties["quartz.scheduler.proxy"] = "true";
              properties["quartz.scheduler.proxy.address"] = "tcp://10.0.0.46:555/QuartzScheduler";
                // First we must get a reference to a scheduler
              return properties;

            }
            private IScheduler GetScheduler()
            {

                var properties = GetProperties();
                var sf = new StdSchedulerFactory(properties);
                return sf.GetScheduler();

            }
      public DateTimeOffset ScheduleJob(IJobDetail jobDetail, ITrigger trigger)
        {

            return _scheduler.ScheduleJob(jobDetail, trigger);
        }

        }

/////////////////////////////////////////////// ////////
您的ScheduleAndAlert()方法将是那样的

public  void SchedulenewAlert()
    {
        try
        {
           SchedularHanlder schedHandler = new SchedularHanlder()

            String jobname = "remotelyAddedJob2" + DateTime.Now.Ticks.ToString();
            IJobDetail job = JobBuilder.Create<MyTest>()
                .WithIdentity(jobname, "default1")
                .Build();

            JobDataMap map = job.JobDataMap;
            map.Put("msg", "Your remotely added job has executed!");

            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("remotelyAddedTrigger", "default")
                .ForJob(job.Key)
                .WithCronSchedule("/5 * * ? * *")
                .Build();

      var TestTrigger =      TriggerBuilder.Create().ForJob(job).WithIdentity("remotelyAddedTrigger2", "default1")
                .StartNow().Build();


DateTimeOffset pullReportsToQueuejobCalendar = DateBuilder.DateOf(9, 30, 17, 1, 6, 2005);
string trgName = "pullReportsFromQueuejobTrigger" + DateTime.Now.Ticks.ToString();
            var pullReportsToQueuejobTrigger = new CalendarIntervalTriggerImpl
            {
                StartTimeUtc = pullReportsToQueuejobCalendar,
                Name = trgName,
                RepeatIntervalUnit = IntervalUnit.Second,
                RepeatInterval = 40000    // every --- seconds
            };
            /// while (true)
            // {


            // schedule the job
            schedHandler.ScheduleJob(job, pullReportsToQueuejobTrigger);

        }
        catch (Exception ex)
        {
            string lines = "==================Exception  Client===========================\n" + DateTime.Now.TimeOfDay.ToString() + "\n====================END=========================\n";
System.IO.StreamWriter file = new  System.IO.StreamWriter("c:\\ExceptionClient.txt", true);
            file.WriteLine(lines);
            file.Close();
        }
       ///}
    }