Page 1 of 1

enthuware.oce-ejbd.v6.2.420

Posted: Tue Dec 13, 2011 12:06 am
by newEJB
@Resource(lookup="jms/myTopic")
private Destination myTopic;

Can you explain why the above doesn't work(not the right answer)? I think I am not quite clear about @Resource(lookup...). Thanks.

Re: enthuware.oce-ejbd.v6.2.420

Posted: Tue Dec 13, 2011 7:52 am
by admin
As per the API documentation for @Resource, the lookup parameter value must use global jndi name.

HTH,
Paul.

Re: enthuware.oce-ejbd.v6.2.420

Posted: Thu Aug 16, 2012 11:44 pm
by Misha
Isn't the third option also wrong because of the missing name attribute and not just the static property?

@Resource("jms/myTopic")
public static Topic myTopic;

Re: enthuware.oce-ejbd.v6.2.420

Posted: Sun Aug 19, 2012 11:19 am
by admin
Misha wrote:Isn't the third option also wrong because of the missing name attribute and not just the static property?

@Resource("jms/myTopic")
public static Topic myTopic;
No, name is the default attribute. So "jms/myTopic" is same as name="jms/myTopic"

HTH,
Paul.

Re: enthuware.oce-ejbd.v6.2.420

Posted: Sun Aug 19, 2012 8:21 pm
by Misha
admin wrote: No, name is the default attribute. So "jms/myTopic" is same as name="jms/myTopic"

HTH,
Paul.
I see, thanks.

Re: enthuware.oce-ejbd.v6.2.420

Posted: Mon Oct 22, 2012 3:22 pm
by fjwalraven
If I have a "jms Topic specified it in my Session bean's environment under the name "jms/myTopic" then for sure I won't inject the resource, but I would do a lookup of the resource.

This is what I have to do to specify a topic on my Session bean's environment:

Code: Select all

@Resource (lookup = "jms/theGlobalTopic", type=Destination.class, name="jms/myTopic")
@Stateless
@LocalBean
public class SomeEJB {
 @Resource SessionContext sct;
 // other code
}
then if I want to use it in my code I would do a lookup, like this:

Code: Select all

Destination myDest = (Destination) sct.lookup ("jms/myTopic");
and not inject it in a field again... although, I agree it works :D

Code: Select all

@Resource(name="jms/myTopic")
private Destination myTopic;
Or is this just a trick question to see whether people know the difference between "name" and "lookup"?

Regards,
Frits

Re: enthuware.oce-ejbd.v6.2.420

Posted: Mon Oct 22, 2012 5:10 pm
by admin
fjwalraven wrote:If I have a "jms Topic specified it in my Session bean's environment under the name "jms/myTopic" then for sure I won't inject the resource, but I would do a lookup of the resource.
Yes, you would indeed to a lookup, but, as you mentioned below, on the context. (i.e. sessionContext.lookup("jms/myTopic") ) and not in @Resource.
fjwalraven wrote: and not inject it in a field again... although, I agree it works :D

Code: Select all

@Resource(name="jms/myTopic")
private Destination myTopic;
Well, that's not necessarily true. One might want to do it to as it does make the code a little cleaner :)
Or is this just a trick question to see whether people know the difference between "name" and "lookup"?
Not sure if it is a trick question but one should know the difference between lookup and name. Ideally, lookup is used to lookup a resource that is available in the global jndi name space and make it available in the local name space under "name". If you already have a name in your local name space why would you use lookup?

HTH,
Paul.

Re: enthuware.oce-ejbd.v6.2.420

Posted: Mon Oct 22, 2012 11:11 pm
by fjwalraven
Thanks Paul, for the explanation!
If you already have a name in your local name space why would you use lookup?
On the other hand if I know the name in the Global JNDI, why would I use it under another name in my Session Bean I would just inject it like this:

Code: Select all

@Resource (lookup = "jms/theGlobalTopic")
Destination myDest 
I guess a Bean provider will only use the "local name" if the global name is not yet known as the Deployer or Administrator will create them. Right?

Regards,
Frits

Re: enthuware.oce-ejbd.v6.2.420

Posted: Sun Oct 28, 2012 1:52 pm
by admin
I tried to find some more information on this annotation and to be honest, I didn't find much for how the lookup attribute is supposed to be used. The most informative was the JavaDoc, which says, "The name of the resource that the reference points to. It can link to any compatible resource using the global JNDI names.".

So I think it is used to link a global jndi name to a local name. Though, as you said, that would be improbable because a global jndi name would be created by the deployer and so the developer cannot possibly know about it.

So, I am not really sure but the usage @Resource(lookup="jms/myTopic") doesn't seem to be the well accepted usage as opposed to @Resource(name="jms/myTopic")

If you have more information on this, please do share.

thank you.
Paul.

Re: enthuware.oce-ejbd.v6.2.420

Posted: Fri May 22, 2015 7:11 pm
by himaiMinh
I tried to use @Resource (name) to inject a topic, but the container cannot inject the topic.
I use Ivan Krizan's example in appendix C of the study guide:

Code: Select all

@MessageDriven (name ="ListenerFacadeMDB", mappedName="jms/NewMessage",
activationConfig = {
		@ActivationConfigProperty(propertyName="acknowledgeMode", propertyValue="Auto-acknowledge"),
		@ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue")})
public class ListenerFacadeEJB implements MessageListener{
	private final static String PUBLISH_TOPIC_JNDI_NAME = "jms/PublishingTopic";
    @Resource (mappedName = "jms/NewMessageFactory")
    private ConnectionFactory mJMSConnectionFactory;


   // attempt to inject a topic , but got an exception here
    @Resource(name="myTopic")
    private Topic mForwardingTopic;
   
    
    @Override
    public void onMessage(Message inReceivedMessage){
    	 
    	 Connection theForwardingConnection = null;
    	 try{
    		 theForwardingConnection = mJMSConnectionFactory.createConnection();
    		 Session theForwardingSession =
    			 theForwardingConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);
    		 theForwardingMsgProducer = theForwardingSession.createProducer(mForwardingTopic);
    		 .....
    	 }
    	 catch (final Exception e){
    		  e.printStackTrace();
    	 }
    	 finally {
    		    closeJmsResources(theForwardingConnection);
    	 }
   }
         ....
}
I got this injection exception during runtime:
MDB00050: Message-driven bean [MDBFacade:ListenerFacadeMDB]: Exception in creating message-driven ejb : [com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Unresolved Message-Destination-Ref myTopic@java.lang.String@null into class com.ivan.listeners.ListenerFacadeEJB]
Severe: com.sun.enterprise.container.common.spi.util.InjectionException