Results 1 to 7 of 7

Thread: SpringBeanAutowiringInterceptor on SLSB Prevents Lifecycle Methods to be Called

  1. #1

    Default 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. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    What app server are you using?
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3

    Default

    Quote 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.

  4. #4
    Join Date
    Aug 2008
    Posts
    1

    Default

    Hi,

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

    Stephen

  5. #5

    Default

    Quote 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.

  6. #6

    Default

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

    Any hints?

  7. #7

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •