When using a stateful session bean with an idle timeout set, how can the bean receive notification from the container that it is being removed due to timeout?
According to the spec, ejbRemove need not (or must not) be called in this case. ejbPassivate is simply the Wrong Thing to be called (the bean is transitioning to the 'does not exist' state, not the 'passive' state).
The EJB 1.1. spec says in section 6.6.3 Missed ejbRemove Calls:
The application using the session bean should provide some clean up mechanism to periodically clean up the unreleased resources.Probably not the answer you're looking for, especially if you allocate some other resource (a Message Queue, for example) that you need to release. Although, if you're using a resource, you really should be getting it when you need it (via JNDI) and returning it back to the pool right away.
For example, if a shopping cart component is implemented as a session bean, and the session bean stores the shopping cart content in a database, the application should provide a program that runs periodically and removes abandoned shopping carts from the database.