3 Replies Latest reply on Aug 15, 2010 9:50 PM by kapitanpetko

    Thread of Control IDs

    j1.jonathan.m.clarke.dsl.pipex.com

      Dear All,


      I have a mostly server-side implementation, which is multi-threaded using a Quartz scheduler. For each on of those Quartz jobs that are running, I would like to produce a single diagnostics file which, as part of the output, records debug messages along side the thread of control that debugging object exists in. So, for example, I might expect to see:


      DateTime          ClassName     TheadId          DebugMessage
      12/08/2010 21:00:01     ClassA          123          FirstOutputMessage
      12/08/2010 21:00:50     ClassB          123          SecondOutputMessage
      12/08/2010 21:00:55     ClassC          456          ThirdOutputMessage
      12/08/2010 21:00:57     ClassA          123          FourthOutputMessage


      The objects that are debugging have no access to their ultimate calling Quartz job, but I need to at the id for the thread of control. For example, in Delphi, I might call a global method called GetThreadId, which is able to determine the thread of control for any calling object. I can't find an equivalent for Seam/Quartz or J2EE, for that matter.


      Please help. Many thanks,
      Jonathan.

        • 1. Re: Thread of Control IDs
          kapitanpetko

          Nothing to do with Seam, but:



          • if you are using log4j, use the %t conversion pattern to output the executing thread's name in the logs

          • use Thread.currentThread().getId/Name()



          As usual, browsing the API reference helps.



          • 2. Re: Thread of Control IDs
            j1.jonathan.m.clarke.dsl.pipex.com

            Hi Nikolay,


            Thanks for the help. I refrained from even going near the native Java Thread code, because I understood that J2EE (I'm still learning about J2EE and Seam!) and Java Threads are not necessarily happy (or stable) bed-fellows. So, I put your line of code in, yes, I did get some good results, but there is still one outstanding issue. I have one main Quartz job (let's call is JobA) which drives the entire server-side operations of my .ear. The code called in JobA can then spawn other jobs to do certain tasks. However, JobA is only ever spawned once, and runs indefinitely. However, it's ThreadId keeps changing. Is this because Quartz spawns a new thread internally, for each execution cycle?


            Jonathan.

            • 3. Re: Thread of Control IDs
              kapitanpetko

              Jonathan Clarke wrote on Aug 13, 2010 05:33:


              Thanks for the help. I refrained from even going near the native Java Thread code, because I understood that J2EE (I'm still learning about J2EE and Seam!) and Java Threads are not necessarily happy (or stable) bed-fellows.


              Well, Quartz spawns a bunch of threads right in your JEE app, so you could argue that it's not-compliant :) In any case, IIRC, the spec only says something to the tune of 'don't start your own threads', so as long as you are only checking thread IDs, etc., you should be OK.



              So, I put your line of code in, yes, I did get some good results, but there is still one outstanding issue. I have one main Quartz job (let's call is JobA) which drives the entire server-side operations of my .ear. The code called in JobA can then spawn other jobs to do certain tasks. However, JobA is only ever spawned once, and runs indefinitely. However, it's ThreadId keeps changing. Is this because Quartz spawns a new thread internally, for each execution cycle?


              What do you mean by 'runs indefinitely'? Runs every n minutes? If so, yes the actual thread that executes the job changes, so the thread ID/name will change for every execution. Quartz keeps a thread pool, so if you monitor the log long enough, you should notice that the thread name repeats. Should be something like 'YourSchedulerName-Worker-1/2/3...'.


              HTH