我正在编写一个Scheduling Client Server应用程序。服务器应用程序使用Quartz和客户端应用程序使用Window Service(使用WEB和控制台应用程序进行测试)。客户端/服务器项目都成功构建,但是当我启动服务并且我的控制台应用程序与此服务通信时。客户端对象调用时出现异常:sched.ScheduleJob(作业,触发器)
通过此行的异常。
以下是我的代码段
namespace QuartzService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
private IScheduler sched;
public void Show()
{
try {
NameValueCollection properties = new NameValueCollection();
properties["quartz.scheduler.instanceName"] = "RemoteServer";
// set thread pool info
properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
properties["quartz.threadPool.threadCount"] = "5";
properties["quartz.threadPool.threadPriority"] = "Normal";
// set remoting exporter
properties["quartz.scheduler.exporter.type"] = "Quartz.Simpl.RemotingSchedulerExporter, Quartz";
properties["quartz.scheduler.exporter.port"] = "555";
properties["quartz.scheduler.exporter.bindName"] = "QuartzScheduler";
properties["quartz.scheduler.exporter.channelType"] = "tcp";
properties["quartz.scheduler.exporter.channelName"] = "httpQuartz";
/// properties = (NameValueCollection)ConfigurationManager.GetSection("quartz");
ISchedulerFactory sf = new StdSchedulerFactory(properties);
sched = sf.GetScheduler();
sched.Start();
string lines = "==================Start===========================\n"+DateTime.Now.TimeOfDay.ToString()+"\n====================END=========================\n";
// Write the string to a file.
string filepath = @"D:\Murtaza\Service\test.txt";
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt",true);
file.WriteLine(lines);
file.Close();
// Console.WriteLine("Hello");
// Thread.Sleep(TimeSpan.FromMinutes(1));
// sched.Shutdown(true);
}
catch { }
}
}
}
=============================== 这是客户端控制台应用程序代码:
namespace Client
{
class Program
{
static void Main(string[] args)
{
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";// "tcp://10.0.0.85:555/QuartzScheduler"; //"tcp://10.0.0.46:555/QuartzScheduler";
// First we must get a reference to a scheduler
ISchedulerFactory sf = new StdSchedulerFactory(properties);
IScheduler sched = sf.GetScheduler();
IJobDetail job = JobBuilder.Create<TestJob>()
.WithIdentity("remotelyAddedJob", "default")
.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();
// schedule the job
sched.ScheduleJob(job, trigger);
}
catch (Exception ex)
{ }
}
}
}