enthuware.oce-ejbd.v6.2.420

Moderator: admin

Post Reply
newEJB

enthuware.oce-ejbd.v6.2.420

Post 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.

admin
Site Admin
Posts: 8906
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.oce-ejbd.v6.2.420

Post by admin »

As per the API documentation for @Resource, the lookup parameter value must use global jndi name.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Misha
Posts: 10
Joined: Thu Aug 09, 2012 1:32 am
Contact:

Re: enthuware.oce-ejbd.v6.2.420

Post 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;

admin
Site Admin
Posts: 8906
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.oce-ejbd.v6.2.420

Post 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.
If you like our products and services, please help us by posting your review here.

Misha
Posts: 10
Joined: Thu Aug 09, 2012 1:32 am
Contact:

Re: enthuware.oce-ejbd.v6.2.420

Post by Misha »

admin wrote: No, name is the default attribute. So "jms/myTopic" is same as name="jms/myTopic"

HTH,
Paul.
I see, thanks.

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: enthuware.oce-ejbd.v6.2.420

Post 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

admin
Site Admin
Posts: 8906
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.oce-ejbd.v6.2.420

Post 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.
If you like our products and services, please help us by posting your review here.

fjwalraven
Posts: 429
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: enthuware.oce-ejbd.v6.2.420

Post 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

admin
Site Admin
Posts: 8906
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: enthuware.oce-ejbd.v6.2.420

Post 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.
If you like our products and services, please help us by posting your review here.

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: enthuware.oce-ejbd.v6.2.420

Post 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

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests