A quick way to integrate Seam 3 CDI and Quartz Jobs

I am currently working on porting a Seam 2/JBoss 4.x application to Seam 3/JBoss AS 7.x. The old codebase contains some “jobs” that use ServiceMBeanSupport, that are triggered at regular intervals using JBoss varia module. Since the module isn’t there anymore, I wanted port the “jobs” to Quartz. Adding quartz was easy enough, but the jobs needed other (injected) beans to do their work. I created a custom JobFactory that used BeanManager to give out the instances of Job classes.

And here’s a sample Job:

There are couple of ways to start the scheduler itself, one was to use a @Startup bean with @PostConstruct method which starts the scheduler and in @PreDestroy, the scheduler is stopped, as shown below:

The other approach is to start the scheduler when the Webapplication has started using the CDI Event listener mechanism.

8 thoughts on “A quick way to integrate Seam 3 CDI and Quartz Jobs”

  1. Hi vijay,

    Great article! I’m having a BeanManagerUnavailableException though. I’m not sure what to do in this case. Admittedly, I’m still doing some more googling as I type this

    1. Hi,

      Not sure what environment/server you are using:

      But you can lookup the Beanmanager using JNDI

      public static BeanManager getBeanManager() {
      try {
      InitialContext initialContext = new InitialContext();
      return (BeanManager) initialContext.lookup("java:comp/BeanManager");
      } catch (NamingException e) {
      e.printStackTrace();//BAD!
      return null;
      }
      }

      1. Thats quite simple but sometimes does not work, especially when the lookup is carried out from an “unmanaged thread” on IBM WebSphere. The problem is indeed to get hands on the BeanManager; the remaining is quite straight forward.

    2. Hey, it is a neat guide. I also encountered trouble finding the BeanManager.

      The simple solution is simply to
      @Inject
      BeanManager beanManager;

      It works!

  2. Hey, I need to use CDI to inject an EJB inside a Job to get my data updated. But I couldn’t seem to find a way out. And I can’t understand these Seam 3 things. I want the most simple basic CDI enabled in the Job, for that as I understand I’ll have to implement my own JobFactory, right? Can you please help a bit?

      1. But this BeanManagerAware is it a Weld component? I’m not using Seam. And I checked that integration, I probably don’t need that much, yours only looks good to me. But These same interfaces would work or I need to use some different Components to inject beans via Weld?

        1. BeanManagerAware is part of Solder – which can be used with “any CDI runtime” (from the Docs).

          If you don’t want all the she-bang, then the approach outlined here in the post should work.

Leave a Reply