开发者

How to float exceptions from Sharepoint Timer jobs?

I have a class MyMainApplication which is a SPIisWebServiceApplication. So it is hosted and running under IIS.

I have a custom sharepoint timer job lets say named CustomTimerJob class which is derived from SPJobDefinition class. So, timer jobs are run under OWSTimer.exe

I have two Quest开发者_StackOverflow中文版ions : [Please see the code below, to relate the questions]

  1. The variables in my CutomTimerJob, are they accessible from the calle, in my example that would be the var job = new CustomTimerJob(); job.RunNow(); , will the varibale job have a reference to the customeTimerJob running, and be able to get the value of job.status ? I have seen, people have used [persisted] keyword marking the variables, to keep the state. I would appreciate if anyone can elaborate more on that. Why that is used and how it actually works, where does it persist to ?

  2. Can the exception float back to the callee ? As far I understand, it is logical that the exception will not float back to the callee, as the timer job is running on a seperate process. But, then by question is, when it does this var job = new CustomTimerJob();, what is the job variable pointing to ?

The code I wrote looks something like this :

>     Class MyMainApplication : SPIiWebServiceApplication
>     {
>     // something
>     .
>     .
>     .
>     void some_function()
>     {
>     // Create and run the timer job immediately
>     
>     var job = new CustomTimerJob()
>     job.RunNow();
>     
>     // Give it a bit of time before checking the status
>     Thread.Sleep(5000);
>     
>     // Want to print the status to see if it was changed when it ran 
>     Console.Writeln( job.Status );
>     
>     }
>     
>     }
>     
>     ----------------------
>     
>     class CustomTimerJob : SPJobDefinition
>     {
>     
>     public Boolean status;
>     // something
>     
>     public override void Execute(Guid contentDbId)
>     {
>     status = true;
>     try {
>     // do some processing
>     } catch (Exception) {
>     
>     // Can I throw the exception up from here ? And will the calle get the exception
>     // throw new CustomException(e);
>     
>     }
>     
>     }

I really appreciate the readers who had the heart to read all the way through till this line. Kudos from me !

Thanks in Advance.


The SPJobDefinition.RunNow() method only schedules the execution, which is then performed in the context of the OWSTimer.exe process. Hence the answers to your questions are:

  1. No, they are not accessible. More precisely, the instance you created is not the same instance as is going to be executed and it even lives in another process.
  2. No, for the same reason the exception won't be propagated to your that place where you call RunNow(). Generally it is a bad practice to allow an unhandled exception to be propagated outside a custom timer job, because the job is then considered failed. Unless that's what you need to achieve handle exceptions properly.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜