-
1. Re: EntityManager injection in a Message Driven Bean
lvdberg Aug 6, 2009 9:52 AM (in response to chrisouellette)Hi Chris,
I took me a while to get things working, but it works with me. Be aware that you must define the SEAM interceptor somewher. I do this in the ejb-jar.xml file (under META-INF).
It content should be:
<?xml version="1.0" encoding="UTF-8"?> <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" version="3.0"> <interceptors> <interceptor> <interceptor-class>org.jboss.seam.ejb.SeamInterceptor</interceptor-class> </interceptor> </interceptors> <assembly-descriptor> <interceptor-binding> <ejb-name>*</ejb-name> <interceptor-class>org.jboss.seam.ejb.SeamInterceptor</interceptor-class> </interceptor-binding> </assembly-descriptor> </ejb-jar>
Furthermore you must have your beans annotated with @Name as you already did and @JndiName (the latter can be done with a pattern in components.xml). I use it to send
Seam events
after receiving a Meeasge through JMS. Works great with Seam 2.1.2 and JBOSS 5.1Leo
-
2. Re: EntityManager injection in a Message Driven Bean
chrisouellette Aug 7, 2009 7:33 PM (in response to chrisouellette)Hi Leo,
Thanks for your reply. I do have the Seam Interceptors defined in my ejb-jar.xml and I am convinced that WebSphere is once again the cause of any problem. The good news is that I was able to get this working, albeit with a non-standard configuration. I had to make 2 concessions:
- I had to use a stateless session bean instead of a POJO
- I had to inject the bean using @EJB instead of @In
So now, my MDB code looks like this:
@Name("myMDB") @MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "destinationName", propertyValue = "jms/myQueue"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") }) public class MyMDB implements MessageListener { @Logger private Log log; @EJB private MyApplicationScopedSessionBean myAppComponent; public void onMessage(Message message) { myAppComponent.doStuff(); } }
I'm actually OK with this setup. I can't use the EntityManager inside my MDB but I have full access to all Application scoped components, including EntityManager, in my session bean.
-
3. Re: EntityManager injection in a Message Driven Bean
dgomesbr Aug 7, 2009 9:28 PM (in response to chrisouellette)Chris, like interceptors, u can't inject directly on mdb.
try
entityManager = (EntityManager) Component.getInstance("entityManager"); -
4. Re: EntityManager injection in a Message Driven Bean
chrisouellette Aug 7, 2009 9:38 PM (in response to chrisouellette)Hi Diego,
Thanks for the response. I have tried using:
Component.getInstance("entityManager");
from inside the MDB, but I get an exception:
java.lang.IllegalStateException: No application context active
-
5. Re: EntityManager injection in a Message Driven Bean
kapitanpetko Aug 8, 2009 10:34 AM (in response to chrisouellette)Why not just use the EJB PersitenceContext:
@PersistenceContext EntityManager em;
The SPMC is not really useful in and MDB (since there is no conversation).