enthuware.oce-ejbd.v6.2.420
Moderator: admin
enthuware.oce-ejbd.v6.2.420
@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.
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.
-
- Site Admin
- Posts: 10070
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.oce-ejbd.v6.2.420
As per the API documentation for @Resource, the lookup parameter value must use global jndi name.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Thu Aug 09, 2012 1:32 am
- Contact:
Re: enthuware.oce-ejbd.v6.2.420
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;
@Resource("jms/myTopic")
public static Topic myTopic;
-
- Site Admin
- Posts: 10070
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.oce-ejbd.v6.2.420
No, name is the default attribute. So "jms/myTopic" is same as name="jms/myTopic"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;
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Thu Aug 09, 2012 1:32 am
- Contact:
Re: enthuware.oce-ejbd.v6.2.420
I see, thanks.admin wrote: No, name is the default attribute. So "jms/myTopic" is same as name="jms/myTopic"
HTH,
Paul.
-
- Posts: 429
- Joined: Tue Jul 24, 2012 2:43 am
- Contact:
Re: enthuware.oce-ejbd.v6.2.420
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:then if I want to use it in my code I would do a lookup, like this:
and not inject it in a field again... although, I agree it works
Or is this just a trick question to see whether people know the difference between "name" and "lookup"?
Regards,
Frits
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
}
Code: Select all
Destination myDest = (Destination) sct.lookup ("jms/myTopic");
Code: Select all
@Resource(name="jms/myTopic")
private Destination myTopic;
Regards,
Frits
-
- Site Admin
- Posts: 10070
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.oce-ejbd.v6.2.420
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: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.
Well, that's not necessarily true. One might want to do it to as it does make the code a little cleanerfjwalraven wrote: and not inject it in a field again... although, I agree it worksCode: Select all
@Resource(name="jms/myTopic") private Destination myTopic;
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?Or is this just a trick question to see whether people know the difference between "name" and "lookup"?
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 429
- Joined: Tue Jul 24, 2012 2:43 am
- Contact:
Re: enthuware.oce-ejbd.v6.2.420
Thanks Paul, for the explanation!
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
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:If you already have a name in your local name space why would you use lookup?
Code: Select all
@Resource (lookup = "jms/theGlobalTopic")
Destination myDest
Regards,
Frits
-
- Site Admin
- Posts: 10070
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.oce-ejbd.v6.2.420
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.
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.
-
- Posts: 358
- Joined: Fri Nov 29, 2013 8:26 pm
- Contact:
Re: enthuware.oce-ejbd.v6.2.420
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:
I got this injection exception during runtime:
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);
}
}
....
}
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
Who is online
Users browsing this forum: No registered users and 89 guests