title: Port MBean from JBoss 4.x to JBoss 7
body: <p>we're currently in the process of porting some of our projects from JBoss 4.x to JBoss 7.
  So far everything seems to work fine, except for our MBeans, which we're commonly using to provide simple management operations.</p>
  
  <p>I've been searching for quite a while now, but either I'm incapable of comming up with the correct search term or I'm missing some piece of knowledge to bridge the gap between MBean definition in JBoss 4.x and JBoss 7.</p>
  
  <p>Thus, hopefully someone can provide a hint on what I might be missing or where I'd have to read on (maybe some documentation, examples etc.)</p>
  
  <p>In Jboss 4.x our MBeans often look like this:</p>
  
  <pre><code>@Service( objectName = "Domain:Name=SomeMBean",
    xmbean="resource:&lt;path-to-xmbean.xml&gt;")
  class SomeMBean 
  {
    @EJB
    private SomeService someService;    
  
    public String someOperation()
    {
       someService.doSomething();
       return "success";
    }
  }
  </code></pre>
  
  <p>We used the <code>@Service</code> annotation to define the object name and xmbean descriptor and JBoss would automatically register those mbeans.</p>
  
  <p>Apparently, in JBoss 7 the <code>@Service</code> annotation doesn't exist anymore and thus another approach is needed.</p>
  
  <p>So far, I managed to register the MBean manually with the platform mbean server, but I'd rather like JBoss to do that automatically. Additionally, I didn't manage to provide descriptions for the methods/parameters so far (although those are more of a nice to have feature).</p>
  
  <p>I'll repeat the question for clarity: </p>
  
  <p>How would I define an MBean in JBoss 7 (Java EE 6) that provides the following features?</p>
  
  <ul>
  <li>automatic deployment</li>
  <li>access to EJBs</li>
  <li>accessible through JConsole or JMX-Console (I'm currently using Dimitris Andreadis' port)</li>
  <li>provide descriptions for methods/parameters</li>
  </ul>
  
  <p><strong>Update</strong></p>
  
  <p>Here's what I got so far:</p>
  
  <p>First, I found this projection, which uses CDI to wrap the injection target of any bean that is annotated accordingly and does the JMX registration in the <code>postConstruct()</code> method: <a href="http://code.google.com/p/jmx-annotations/" rel="nofollow">http://code.google.com/p/jmx-annotations/</a>. Additionally, the found MBeans are scanned for class/attribute/method/parameter annotations that provide a description for the annotated property.</p>
  
  <p>However, the <code>postConstruct()</code> method seems not to be called for EJBs (I assume that is in order not to clash with the EJB container). Thus MBeans now should not be EJBs but plain CDI beans.</p>
  
  <p>Thus, however, has the drawback that MBeans are not automatically instantiated. To overcome this, there is a singleton bean that at startup loops through all beans in the <code>BeanManager</code> and creates an instance of every MBean found. Because the MBeans still have their injection target, its <code>postConstruct()</code> method will no be called and the bean will be registered in the MBean server.</p>
  
  <p>Here's a rough overview of the startup procedure:</p>
  
  <ul>
  <li>a custom CDI extension scans each CDI bean for the custom @MBean annotation</li>
  <li>for each elligible MBean the injection target is wrapped</li>
  <li>a singleton bean will be started which in its @PostConstruct method will create instances of the MBeans</li>
  <li>the <code>postConstruct()</code> method of the MBean's injection target will be called and thus the MBean is registered in the MBean server</li>
  </ul>
  
  <p>One drawback of this method would be the missing transaction context when executing MBean methods (any EJB calls will run in a transaction context). Howver, this could be fixed using a CDI interceptor that will provide for the transaction context, if needed. The Seam project seems to have appropriate interceptors for that.</p>
  
  <p>I'm still not sure if this is a sane and stable approach, so any constructive comments, hints etc. are more than welcome.</p>