• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to lookup local interfaces?  RSS feed

 
Mohammad Norouzi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

In my application I have no problem with session bean injection using @EJB but in a non-ejb object when I want to look up a SLSB it fails. if I change my session bean to a remote object then everything is ok but because of security issues and also performace I want to use local interfaces

I am using glassfish as AS.

what is the problem. did I missed something?
[ December 06, 2008: Message edited by: Mohammad Norouzi ]
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post your lookup code and the exception stacktrace. Are you sure you are using the correct jndi-name for the lookup?
 
Mohammad Norouzi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the full interface name as jndi name
consider ReportingService as a local interface I put this snippet:

InitialContext c = new InitialCondext(myServerProperties);
ReportingService service = (ReportingService)c.lookup(ReportingService.class.getName());


if this is a local interface I get NamingException but if I change it to a remote interface everything's ok/
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you doing that in a module that is in the same EAR? Please read here:
https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#ClientLocalEJBAccess
and also a question below this one.
Give us feedback if it helps.
 
Mohammad Norouzi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raf
I chekc the FAQ you send but my problem still exists.

I have a local interface namely IEndOfDayService and a POJO class, actually this is a Quartz task to be executed every day

I have tested all following names:

service = context.lookup("java:comp/env/IEndOfDayService");
System.out.println("1"+service);

service = context.lookup("java:comp/env/"+IEndOfDayService.class);
System.out.println("2"+service);

service = context.lookup("java:comp/env/"+EndOfDayServiceImpl.class.getSimpleName()+"/local");
System.out.println("3"+service);

and also I put
@EJB(name="IEndOfDayServiceRef",beanInterface=IEndOfDayService.class)

on my task class and then look it up using:
service = context.lookup("java:comp/env/IEndOfDayServiceRef");
System.out.println("4"+service);

but still it returns null

I am really stuck !!! please help me
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since it's Quartz task (which runs asynchronously in its own thread context), i don't think you can do the injection in that class. Try the injection in a servlet and see if it works. If it works, then it will narrow down the possible reasons why it does not work in the quartz task.
 
Mohammad Norouzi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jaikiran Pai:
Since it's Quartz task (which runs asynchronously in its own thread context), i don't think you can do the injection in that class. Try the injection in a servlet and see if it works. If it works, then it will narrow down the possible reasons why it does not work in the quartz task.


I don't want inject it into my task, I want to look it up. I want to look up a local interface and this task is in the same application so I don't want to convert my SB to a remote interface!
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#SessionBeanGlobalJNDINameAssignment
The first sentences state that only remote interfaces get a global JNDI name that you can look up. A global JNDI name corresponds to the mappedName elements of @EJB / @Resource, and you can only inject local interfaces using the name attribute, and only within the same EAR.
To make this work, you need to use ejb-ref or @EJB on the class level of whatever managed resource is using the class you have the lookup code in. This link:
https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#POJOLocalEJB
has all the necessary steps you need to follow for this to work.
Hope this helps.
 
Mohammad Norouzi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raf Szczypiorski:
https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#SessionBeanGlobalJNDINameAssignment
The first sentences state that only remote interfaces get a global JNDI name that you can look up. A global JNDI name corresponds to the mappedName elements of @EJB / @Resource, and you can only inject local interfaces using the name attribute, and only within the same EAR.
To make this work, you need to use ejb-ref or @EJB on the class level of whatever managed resource is using the class you have the lookup code in. This link:
https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#POJOLocalEJB
has all the necessary steps you need to follow for this to work.
Hope this helps.



Yes Raf, it is said the local interfaces are in a private scope "java:comp/env" and as example it is declared following snippet:

InitialContext ic = new InitialContext();
FooLocal foo = (FooLocal) ic.lookup("java:comp/env/fooejbref");

which "fooejbref" is the local name specified in @EJB

but as I said in my previous posts neither of them work!

neither @EJB works nor looking up this name: "java:comp/env/IEndOfDayService" in which IEndOfDayService is the name of my local interface. I even use the fully qualified name but problem still exists

I don't think I can put @EJB on my POJO classes except servlets. Am I right?

and also I think the private jndi name I build is incorrect

Have you got any suggestion? Is there any way to print out all the JNDI names available in AS?
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the link I posted previously until the end of discussion of local beans lookups? I would say this is explaining exactly what you should do.
Yes, @EJB can be put at a class level of managed resources (servlets, tags, listeners, JSF backing beans, EJB...). You want to use it in a POJO, but this POJO is used within the context of one of the mentioned resources - and the POJO "inherits" the JNDI context from its enclosing resource. For example, if you have a servlet, and in the servlet you are instantiating your POJO thats does the lookup, you put the @EJB on the servlet level. (This is shown in step 1 in one of the links from my previous post.)

You said you don't want to use injection, maybe because you don't like annotations. So, maybe you don't want to use @JEB annotation at class level either, so the only option left is to use ejb-local-ref in web.xml or ejb-jar.xml. Everything is explained and there are examples if you carefully follow the links I posted. (Also in step 1.)

Then, in your POJO that is invoked within a context of a managed resource, you do the lookup using the name you gave to the ejb-local-ref (or in the @EJB annotation). You append the name to the "java:comp/env" environment context. (This is explained well in step 2.)

As for listing the JNDI context, there are "asadmin list-jndi-entries" and "asadmin list-jndi-resources" commands, as well as you can go to the web admin console and in the "Application Server" tab, there are a few buttons, and one of them is "JNDI browsing". Note however, that - as one of the links I gave you - no local EJB will be listed there!

So. to summarise:
1a. put the @EJB annotation at a class level for the managed resource which uses you POJO that in turns does the lookup (for a war, you can put it at a class level of whatever managed resource you want, JNDI context is shared anyways)
OR
1b. put a ejb-local-ref in web.xml (the whole war has the same JNDI environment) or in ejb-jar.xml for the managed resource that is using your POJO
2. in the POJO, use lookup("java:comp/env/" + the_name_you_used)

It works for me, and I am no magician ;-) So, please read the links carefully.
 
Mohammad Norouzi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Raf for your complete answer. but I said that my POJO is not a managed resource. it's not in .war file. it is a simple class, managed by Quartz within the same .jar file that my local interfaces are placed. That is why @EJB doesn't work

And I said I don't want to inject, it was due to reply to Jaikiran Pai that told me you can't inject in an unamanaged resource and I said yes I know that is why I want to look it up!.

Anyway, thank you again.
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not managed itself, but it is invoked within a context of a managed resource. Something invokes the code in your POJO, doesn't it? It is either an EJB, a servlet, or other managed resource. If it is a Quartz task, doesn't something start up the Quartz service? It is a bean, a ServletContextListener.contextInitialized, whatever. Have you actually tried putting a ejb-local-ref / annotation on the managed resource that is using your POJO, or you just say it won't work?
 
Mohammad Norouzi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raf Szczypiorski:
It's not managed itself, but it is invoked within a context of a managed resource. Something invokes the code in your POJO, doesn't it? It is either an EJB, a servlet, or other managed resource. If it is a Quartz task, doesn't something start up the Quartz service? It is a bean, a ServletContextListener.contextInitialized, whatever. Have you actually tried putting a ejb-local-ref / annotation on the managed resource that is using your POJO, or you just say it won't work?


Yes Raf, my Quartz task is adding to Quartz initializer from a ServletContextListener and then it will run every often as it has scheduled and what I didn't know was the source of invoking POJO you said now. I've read the documents you said but it was not clear or I didn't understand it.

I thought that @EJB should be placed on the task!

eventually I could look it up using ejb-local-ref in web.xml but didn't tried to put @EJB on my context listener. right now I'm at home I'll check it tomorrow.

Anyway thank you very much Raf.
 
Mohammad Norouzi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends
Sorry for activating this thread again. I have encountered the same problem looking up local interfaces.
The problem is that an instance of a class say a job which is managed with our API is instantiated using reflection and its methods will be invoked using reflection and unfortunately in the method body neither of names is understanding by the container


I think this is because I am using reflection to instantiating the class, do you have any idea?

thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!