Application Server 3  4  5 and 6
  1. Application Server 3 4 5 and 6
  2. JBAS-7236

@PostConstruct is never called for @Service beans

    Details

    • Type: Bug Bug
    • Status: Closed Closed (View Workflow)
    • Priority: Major Major
    • Resolution: Done
    • Affects Version/s: JBossAS-5.1.0.GA
    • Fix Version/s: 6.0.0.M1
    • Component/s: EJB
    • Security Level: Public (Everyone can see)
    • Environment:
      JBoss 5.1.0 GA running of Fedora Core 11
    • Similar Issues:
      Show 10 results 

      Description

      I have a @Service bean which defines initialization method and that method is marked with @PostConstruct.

      This method is never called by JBoss, not when the bean is first created and not when it is accessed.

      Same issue happens when testing with embedded jboss (beta3.sp9)

        Issue Links

          Activity

          Hide
          Gregory Mostizky
          added a comment -

          There is slightly similar bug here: https://jira.jboss.org/jira/browse/EJBTHREE-1068

          However it is for older JBoss line (4.2.X) and the scenario is different so I opened a new one instead of commenting previous one.

          Show
          Gregory Mostizky
          added a comment - There is slightly similar bug here: https://jira.jboss.org/jira/browse/EJBTHREE-1068 However it is for older JBoss line (4.2.X) and the scenario is different so I opened a new one instead of commenting previous one.
          Hide
          Gregory Mostizky
          added a comment -

          I've digged into the code (5.1.0 GA sources) and I think I found both the cause of this and also a workaround:

          In org.jboss.ejb3.service.ServiceContainer which is responsible for lifecycle of @Service beans there are following lines:

          <code>
          public void invokePostConstruct(BeanContext beanContext, Object[] params)

          { //Ignore }

          public void invokePreDestroy(BeanContext beanContext)
          { //Ignore }

          </code>

          Because of this the container will not invoke @PostConstruct and @PreDestroy even if it sees them.

          On the other hand there is a workaround.

          <code>
          public void create() throws Exception

          { super.create(); // EJBTHREE-655: fire up an instance for use as MBean delegate singleton = super.construct(); registerManagementInterface(); invokeOptionalMethod(METHOD_NAME_LIFECYCLE_CALLBACK_CREATE); }

          </code>

          What happens here is that after the bean is created there is hardcoded call to a method with "public void create()" signature if it's found.
          Similar is done for destroy.

          In other words if you define an initialization like this it will get called now and when the bug is fixed.

          <code>
          @PostConstruct
          public void create() {
          }
          </code>

          Show
          Gregory Mostizky
          added a comment - I've digged into the code (5.1.0 GA sources) and I think I found both the cause of this and also a workaround: In org.jboss.ejb3.service.ServiceContainer which is responsible for lifecycle of @Service beans there are following lines: <code> public void invokePostConstruct(BeanContext beanContext, Object[] params) { //Ignore } public void invokePreDestroy(BeanContext beanContext) { //Ignore } </code> Because of this the container will not invoke @PostConstruct and @PreDestroy even if it sees them. On the other hand there is a workaround. <code> public void create() throws Exception { super.create(); // EJBTHREE-655: fire up an instance for use as MBean delegate singleton = super.construct(); registerManagementInterface(); invokeOptionalMethod(METHOD_NAME_LIFECYCLE_CALLBACK_CREATE); } </code> What happens here is that after the bean is created there is hardcoded call to a method with "public void create()" signature if it's found. Similar is done for destroy. In other words if you define an initialization like this it will get called now and when the bug is fixed. <code> @PostConstruct public void create() { } </code>
          Hide
          Gregory Mostizky
          added a comment -

          Unfortunately the workaround I posted above doesn't work fully - the method is indeed called - however it is called before the @Service bean is injected with properties (such as @EJB reference) so it's not always helpful.

          Show
          Gregory Mostizky
          added a comment - Unfortunately the workaround I posted above doesn't work fully - the method is indeed called - however it is called before the @Service bean is injected with properties (such as @EJB reference) so it's not always helpful.

            People

            • Assignee:
              Carlo de Wolf
              Reporter:
              Gregory Mostizky
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: