Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify name for resource annotations in compile time?

Tags:

java

ejb-3.0

Our code has something like this:

@Resource(name = "java:comp/resource/foo/bar/ONE_QUEUE")
private Queue queue;

However, in one deployment scenario the queue annotation should look like this:

@Resource(name = "java:comp/resource/foo/bar/SECOND_QUEUE")
private Queue queue;

I would like to choose the name to use with Maven build profiles.

What options do I have?

like image 962
tputkonen Avatar asked Jan 18 '10 07:01

tputkonen


People also ask

How to include resources at compile time (C++)?

How to: Include Resources at Compile Time (C++) By default all resources are located in one resource script (.rc) file, however there are many reasons to place resources in a file other than the main .rc file: To add comments to resource statements that won't get deleted when you save the .rc file.

What is the @resource annotation in spring for?

The @Resource annotation in spring performs the autowiring functionality. This annotation follows the autowire=byName semantics in the XML based configuration i.e. it takes the name attribute for the injection.

What does @resource mean in an annotated field?

@Resource means get me a known resource by name. The name is extracted from the name of the annotated field or it's taken from the name parameter. Refer this link : stackoverflow.com/questions/4093504/resource-vs-autowired First of all, to understand the point of @Resource you need to understand the Inversion of Control (IoC).

How do I include a compiler in a resource file?

Right-click the .rc file in Resource View and select Resource Includes. In the Compile-time directives box, add the #include compiler directive to include the new resource file in the main resource file in the development environment.


2 Answers

This is not the right way to do things. Resources should be added to the local jndi name of individual EJBs. This is to separate the jndi name used in the bean code from the global jndi bindings set by the bean deployer. The mapping of the bean local jndi binding and the global binding may be handled via the ejb-jar.xml and appserver-specific deployment descriptors.

So, instead, you should declare your @Resource (which is equivalent to a <resource-ref> element indicating resource reference name and type) like this:

@Resource(name = "jms/queue/aQueue")
private Queue queue;

Then, in a appserver-specific deployment descriptor (for GlassFish it's sun-ejb-jar.xml, for JBoss it's jboss.xml, for WebLogic it's weblogic-ejb-jar.xml, etc), declare a <resource-ref> element indicating the resource reference name and the global jndi binding via the <jndi-name> element.

<resource-ref>
  <res-ref-name>jms/queue/aQueue</res-ref-name>
  <jndi-name>resource/foo/bar/ONE_QUEUE</jndi-name>
</resource-ref>

Once you'll get the whole thing working, it will be easy to variabalize this appserver-specific deployment descriptor using Maven for different environments with profiles and filtering. Just use a property, activate filtering of resources, and set different value in profiles. Something like that:

<resource-ref>
  <res-ref-name>jms/queue/aQueue</res-ref-name>
  <jndi-name>${my.jndi.name}</jndi-name>
</resource-ref>
like image 160
Pascal Thivent Avatar answered Nov 14 '22 23:11

Pascal Thivent


I think you can use maven filtering, although it would feel strange.

Here is an article about this approach.

like image 32
Bozho Avatar answered Nov 15 '22 00:11

Bozho