2 Replies Latest reply on Jul 19, 2011 12:32 PM by benkirby

    How to get a 'JBoss service' with CDI in JMX with AS6?

    benkirby

      Hi, I'm currently migrating a JBoss service class from AS5.1 to AS6 (not going to AS7 for a variety of reasons).

       

      For AS5.1, the service implements a {serviceName}MBean and has a jboss-service.xml with attribute values. It's packaged in a jboss-sar, which is packaged in an EAR to be deployed. When deployed, the service fields are populated with the values from jboss-service.xml, and the service is automatically registered into JMX.

       

      I would like to achieve the same thing using AS6, but would like the service to support CDI - so I'd like its new @Inject injection points to be satisfied. I need these to be satisfied in the object registered with JMX, so that methods called via JMX can reference injected fields, but I'm struggling to achieve this.

       

      I've had to package the service in a jar, instead of a jboss-sar, for classloader reasons, but let's say it's otherwise unchanged. When deployed to AS6, all works as before - service goes into JMX, values from XML propagate to the object. However, the instance created does not have its CDI injection points satisfied, and neither does the object registered in JMX.

       

      If I annotate the service class with @Startup and @javax.ejb.Singleton, but keep its interface and the jboss-service.xml, the object registered into JMX still does not have its CDI injection points satisfied. However if I programmattically deregister that bean, and re-register the instance in a @PostConstruct method, then the bean in JMX DOES have its injection points satisfied. However that bean no longer has the values specified in the jboss-service.xml.

       

      So how can I get the best of both worlds? CDI and the usual JBoss service behaviour? What is the correct way to implement a JBoss service with CDI? I've been unable to find documentation on this. Hope someone can help.

       

      Thanks,

      Ben

        • 1. Re: How to get a 'JBoss service' with CDI in JMX with AS6?
          alesj

          (a) So, you want to get your MBeans populated with your CDI beans?

          (b) Or vice-versa?

           

          For (b) CDI Extensions would be the way to go.

           

          Where (a) is a bit trickier.

           

          We arelady failrly experimented with MC beans and CDI.

          The code can be found here:

          * http://anonsvn.jboss.org/repos/jbossas/trunk/weld-int/deployer-mc-int/

           

          In JMX case, you would somehow need to intercept the MBean usage,

          then get CDI' BeanManager, and pass-through this MBean, to get proper @Inject handling.

           

          CDI BeanManager can be found in JNDI, but it needs to be at a proper "time"; e.g. when JNDI context is setup.

          Intercepting MBeans would require a bit more work, but can be done.

          e.g. use plain MC bean + MC <install>, then register this MC bean as @JMX as well

          • 2. Re: How to get a 'JBoss service' with CDI in JMX with AS6?
            benkirby

            Thanks for the good suggestions, Ales. From this and suggestions elsewhere it seems that, at present, there isn't a nice, catch-all 'CDI JBoss Service' pattern we should be using.

             

            In the end we went with CDI bean -> JMX - seemed a bit easier, like you say! We created the services as Singleton EJBs, so their injection points are satisfied, which then get registered/unregistered to JMX in their PostConstruct/PreDestroy methods, using German Escobar's excellent CDI portable extension (http://www.germanescobar.net/2010/01/cdi-portable-extension-jmx.html, http://community.jboss.org/thread/148750 is also helpful).

             

            May try to use ApplicationScoped beans and get them to start by observing a ContainerInitialized (or whatever it's called) event, instead, however, as we don't need all the features of a proper EJB. Haven't tried that yet, mind.

             

            Cheers for the help!