9 Replies Latest reply on Jul 30, 2008 3:54 AM by Carlo de Wolf

    Using quartz from EJB 3.0

    henk de boer Master

      Hi,

      I would like to use Quartz from within EJB 3.0 to programmatically create and schedule new jobs. I've already asked a similar question in the JCA forum, since there exists a Quartz RA. However this seems to be only suitable as a replacement for quartz' jobs.xml file.

      What I actually need is a handle to the quartz scheduler, so my session beans are able to create jobs depending on the outcome of some business method. Basically, what I hope to find is something like the method used for obtaining an EJB3 TimerService, but instead of the EJB3 TimerService I would like to get the Quartz scheduler.

      The EJB3 TimerService can be obtained via a Resource annotation, e.g.:

      public @Stateless class SomeBean {
      
       @Resource javax.ejb.TimerService timerService;
      }
      


      Does something like this exist for quartz? If not, what would be the recommended way for scheduling quartz jobs from within EJB3 beans?



        • 2. Re: Using quartz from EJB 3.0
          jaikiran pai Master

           

          I would like to get the Quartz scheduler.


          How do you deploy the quartz scheduler? Does it get bound in the JNDI? If yes, then you can inject it using the @Resource annotation and the mappedName attribute:

          @Resource (mappedName="jndiNameToWhichTheSchedulerIsBound")





          • 3. Re: Using quartz from EJB 3.0
            henk de boer Master

             

            "jaikiran" wrote:
            I would like to get the Quartz scheduler.


            How do you deploy the quartz scheduler? Does it get bound in the JNDI? If yes, then you can inject it using the @Resource annotation and the mappedName attribute:


            Thanks for your suggestion. Currently I haven't set up anything yet. I've used Quartz a lot in plain web applications (Tomcat based) and I also made some simple EJB3 applications.

            Recently the requirement came up to schedule jobs programmatically from within EJB3 beans. Since I've never combined the two before, I wondered what the best approach was.

            What you basically mean is that I deploy Quartz within a separate web application, bind it to JNDI from there and pick it up through JNDI again from the EJB3 side?

            If I did that, I probably can't give the scheduler a job class name of code that lives within the EJB3 container, since the scheduler then lives within another container (a web container). I could circumvent this perhaps by having a job class at the web container side that calls the EJB3 container again.

            Is such a scheme what you're aiming at, or did you mean something else?


            • 4. Re: Using quartz from EJB 3.0
              jaikiran pai Master

              Well, i actually thought that you had managed to bind Quartz scheduler to JNDI and that's why i suggested the @Resource approach. But based on your recent reply, it looks like that you are trying to find a way to use the quartz scheduler in your EJB3.

              So, is there any specific reason for injecting (or even try to lookup) the scheduler from JNDI? Why not just use the Quartz scheduler APIs to trigger the jobs?

              • 5. Re: Using quartz from EJB 3.0
                Carlo de Wolf Master

                There are two ways to use Quartz from EJB3:
                1. the unsupported EJB3 Quartz timer service: https://jira.jboss.org/jira/browse/EJBTHREE-619
                2. or the Quartz resource adapter: http://wiki.jboss.org/auth/wiki/QuartzSchedulerIntegration

                • 6. Re: Using quartz from EJB 3.0
                  henk de boer Master

                   

                  "jaikiran" wrote:
                  So, is there any specific reason for injecting (or even try to lookup) the scheduler from JNDI? Why not just use the Quartz scheduler APIs to trigger the jobs?


                  Well, using Quartz directly in the EJB3 container is illegal. The EJB3 container prohibits code to spawn its own threads. Spawning threads is exactly what Quartz does.

                  Normally, in Java SE code or within a web application, you would simply use a code fragment like below to obtain the scheduler:

                  SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
                  Scheduler scheduler = schedFact.getScheduler();
                  


                  However, doing something like this in e.g. a stateless session bean is illegal and would mess up the container.

                  "wolfc" wrote:
                  There are two ways to use Quartz from EJB3:
                  1. the unsupported EJB3 Quartz timer service: https://jira.jboss.org/jira/browse/EJBTHREE-619
                  2. or the Quartz resource adapter: http://wiki.jboss.org/auth/wiki/QuartzSchedulerIntegration


                  I already tried the second option. The Quartz resource adapter appears to be strictly an inflow adapter. It seems to be able to replace something like jobs.xml in Quartz. I.e. it reads the annotations from classes (and probably also from the EJB3 deployment descriptor) and statically schedules jobs based on that.

                  Based on what I tried and read so far, the resource adapter does not seem to work the other way around: my code can't control quartz. Instead, using the RA quartz can only control my code. I may be wrong though. I already asked for this specifically in the JCA forum but haven't received any replies yet.

                  I'm definitively going to check out the "unsupported EJB3 Quartz timer service", although 'unsupported' doesn't sound too good. Thanks for the suggestion! :)

                  • 7. Re: Using quartz from EJB 3.0
                    henk de boer Master

                    P.s.

                    I took a look at:

                    https://jira.jboss.org/jira/browse/EJBTHREE-619

                    ,but this seems to be strictly an internal implementation detail, and not something that is exposed to the programmer. Perhaps with casting or reflection this would allow me access to the Quartz scheduler, but for a production system I think I'd better not try that ;) The commit comment "This is a prototype implementation and will probably not be supported in the future. " also doesn't sound that encouraging.

                    I do want to try this though (if only just for fun), so I wonder if you know about any documentation about how to use this in actual user code?

                    • 8. Re: Using quartz from EJB 3.0
                      henk de boer Master

                      Anyone else has any idea? I tried for over a full week to find a solution, but still did not find anything.

                      • 9. Re: Using quartz from EJB 3.0
                        Carlo de Wolf Master

                        You can use the code that stems from EJBTHREE-619 as a base for your solution.

                        Change the MBean to suit your own needs and then have it bind a proxy in JNDI that you can lookup in the bean.

                        If you want it injectable, then you need to change the ejb3 code itself.