0

I'm not sure if this is supposed to work, but i'm trying to write a JMS producer with cdi with wildfly and stuck at injecting resources into a cdi managed bean:

public class CdiProducer {
    @Resource(name = "java:jboss/DefaultJMSConnectionFactory")
    @Produces
    QueueConnectionFactory qcf;

    @Resource(name = "java:/queue/HELLOWORLDMDBQueue")
    @Produces
    @Hello
    Queue helloWordQueue;

Running this I'm getting the following error:

JBAS016076: Error injecting resource into CDI managed bean. Can't find a resource named ...

What is very strange however is, when i copy&paste the resources into a enterprise bean, everything works!

@Stateless
public class QueueSender {
    @Resource(name="java:jboss/DefaultJMSConnectionFactory")
    QueueConnectionFactory qcf;

    @Resource(name="java:/queue/HELLOWORLDMDBQueue")
    Queue helloWordQueue;

Queue is defined as:

  <jms-destinations>
     <jms-queue name="HelloWorldQueue">
        <entry name="/queue/HELLOWORLDMDBQueue"/>
        <entry name="java:jboss/exported/queue/HELLOWORLDMDBQueue"/>
     </jms-queue>
  </jms-destinations>

Is this supposed to work this way? Or is a bug in wildfly?

mglauche
  • 3,344
  • 4
  • 28
  • 31
  • Shouldn't you be using @Resource(mappedName="...")? – Rudi Angela Mar 17 '14 at 20:18
  • @Rudi Why? Resource should be able to work with the local jndi name? – mglauche Mar 17 '14 at 21:24
  • In your question, you write "JBAS016076: Error injecting resource into CDI managed bean. Can't find a resource named ..." however you seem to have cut off the critical part. Can you include this critical part? Is it failing on the `Queue` or the `ConnectionFactory`? Also, if you're using WildFly 8, you can simply inject a `JMSContext` and avoid a lot of these defaults. – John Ament Mar 18 '14 at 23:44
  • @John: Both are failing, with the same error message, seems consistent with all kinds of resources. As for JMSContext, i would love to use, but we are using wildfly for local developing, the final product needs to be deployed on a websphere 8.5, which is JEE6/JMS 1.1 only, so no JMSContext :( – mglauche Mar 19 '14 at 10:31

2 Answers2

2

I don't have direct experience with Wildfly/JBoss, but I had the same experiences as you with Glassfish. In our case we were using @PersistenceContext to inject an EntityManager, but I believe the same rules apply.

The Weld documentation has a section about unifying Java EE resources and CDI. It shows how you can define a producer field (described in more detail here) to connect such a resource to CDI in a way that means you can use @Inject elsewhere.

Fields have a duality in that they can both be the target of Java EE component environment injection and be declared as a CDI producer field. Therefore, they can define a mapping from a string-based name in the component environment, to a combination of type and qualifiers used in the world of typesafe injection. We call a producer field that represents a reference to an object in the Java EE component environment a resource.

...

A resource declaration really contains two pieces of information: the JNDI name, EJB link, persistence unit name, or other metadata needed to obtain a reference to the resource from the component environment, and the type and qualifiers that we will use to inject the reference into our beans.

Example:

@Produces @Resource(lookup="java:global/env/jdbc/CustomerDatasource") 
@CustomerDatabase Datasource customerDatabase;

Elsewhere:

@Inject @CustomerDatabase Datasource customerDatabase;

While not explicitly stated on that page, I believe the class containing this field has to be a Java EE bean, i.e. annotated with one of the EJB annotations like @javax.ejb.Stateless or @javax.ejb.Singleton.

andersschuller
  • 13,509
  • 2
  • 42
  • 33
2

Late to the party, but I ran into this same issue. When using @resource on a CDI managed bean the JNDI was always getting prepended with 'java:comp/env/'. To fix this I changed name to lookup when using @resource outside an enterprise bean.

@Resource(lookup = "java:/ConnectionFactory")
private ConnectionFactory connectionFactory;
Jimeh
  • 367
  • 1
  • 6
  • 16