Announcement Announcement Module
Collapse
No announcement yet.
SpringBeanAutowiringInterceptor on SLSB Prevents Lifecycle Methods to be Called Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • SpringBeanAutowiringInterceptor on SLSB Prevents Lifecycle Methods to be Called

    It seems as if the SpringBeanAutowiringInterceptor prevents lifecycle methods (postCreate and preDestroy) from being called

    My original SLSB looked like this:

    Code:
    @Stateless
    @Local(IMessageHandler.class)
    @Interceptors(SpringBeanAutowiringInterceptor.class)
    public class MyMessageHandler implements IMessageHandler {
    
    	@Resource(mappedName = "<queueName>")
    	private Destination targetDestination;
    	@Resource(mappedName = "<connFactoryName>")
    	private ConnectionFactory factory;
    
    	private Connection conn;
    
    	public void dispatch(Object toDispatch) throws Exception {
    		Session session = null;
    		MessageProducer producer = null;
    		try {
    			session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
    			producer = session.createProducer(targetDestination);
    			Message message = createMessage(toDispatch, session);
    			producer.send(message);
    		} catch (JMSException e) {
    			 // handle JMS exception
    		} finally {
    			 // close session and producer
    		}
    	}
    
    	
    
    	@PostConstruct
    	void initConnection() {
    		try {
    			conn = factory.createConnection();
    		} catch (JMSException e) {
    			... // handle JMS exception
    		}
    	}
    
    	@PreDestroy
    	void cleanUp() {
    		try {
    			conn.close();
    		} catch (JMSException e) {
    			... // handle JMS exception
    		}
    	}
    
    }
    The problem was that I always received a NullPointerException when I created the session (highlighted in blue). Apparently the connection was not initialized. I found out that the method initConnection(), annotated with @PostConstruct was not called.

    As soon as I removed the interceptor (highlighted in line 3), everything worked just fine.

    My setup
    applicationContext-messaging.xml contains a default implementation for the bean messageHandler (this session bean):
    Code:
    <beans >
    	<bean id="messageHandler" class=".MyMessageHandler" />
    	
    </beans>
    This context is imported in serviceContext.xml and the bean reference for messageHandler is overwritten.
    Code:
    <beans >
    	<import resource="classpath:applicationContext.xml"/>
    	
    	<import resource="classpath:applicationContext-messaging.xml"/>
    	
    	
    	<!-- Override from JNDI -->
    	
    	<jee:jndi-lookup id="batchProcessor" jndi-name="<jndiName>" />
    	
    </beans>
    And finally this is the application context loaded from within beanRefContext.xml:
    Code:
    <beans >
    	<bean id="serviceContext" class="org.springframework.context.support.ClassPathXmlApplicationContext">
    		<constructor-arg value="serviceContext.xml"/>
    	</bean>
    </beans>
    With this I can do nearly all integration testing without the need for a JEE container or embedded microcontainer. The beans are only real EJBs when being loaded inside the EJB container.

    Is this an expected behavior? Did I misunderstand something with this setup?


    Thank you,
    Kariem

  • #2
    What app server are you using?

    Comment


    • #3
      Originally posted by Rod Johnson View Post
      What app server are you using?
      JBoss 4.2.2.

      The server is launched from within WTP in Eclipse 3.4. I can try the same in a setup without WTP and post back as soon as I have more information. It happened to me sometimes already that applications show a different behavior when being launched from inside Eclipse, so I'll try to rule that out.

      Comment


      • #4
        Hi,

        FYI I'm seeing the same behaviour except with MDBs in SAP NetWeaver 7.1 CE.

        Stephen

        Comment


        • #5
          Originally posted by kariem View Post
          I can try the same in a setup without WTP and post back as soon as I have more information.
          Sorry for not replying that long. Just tried it with JBoss 4.2.3 outside of Eclipse. I can definitely reproduce the problem. If I remove the listener, the lifecycle methods are called correctly.

          Comment


          • #6
            Same here. JBoss 5.1.0, Stateless SB. With SpringBeanAutowiringInterceptor no @PostConstruct is invoked. Without, works like a charm.

            Any hints?

            Comment


            • #7
              Just updated my spring 2.5.5 to latest 2.5.6.SEC01. Now it works as expected. No idea what changed between these versions but that fixed it.

              Comment

              Working...
              X