• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot lookup from a remote bean example

 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
On Ivan's notes, session 5.2, p.80, there is a EJB project called RemoteSessionBean with a @Remote bean StatefulSession1Bean.
There is a dynamic web project called RemoteJavaEESessionBeanClient with a servlet that looks up the remote bean by doing



But when I tried this example based on the notes' instruction, the servlet cannot lookup the remote bean.

My solution is to create a .ear file with a jar file containing the @Remote bean StatefulSession1Bean and the war file containing the servlet.
After I deployed the .ear file, the servlet can lookup the remote bean in the jar.

If you have any other suggestion, please let me know.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when I tried this example based on the notes' instruction, the servlet cannot lookup the remote bean.

Did you deploy the EJB-jar and the WAR-project to the server?
Did you see the EJB logging in the console when deploying the EJB-project?
 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I see the EJB and the servlet being deployed to Glassfish. Glassfish even prints out the JNDI lookup names of the EJB.
But the servlet cannot read it.

When I packaged the EJB bean in .jar and the servlet in .war inside an .ear file, everything works.
But when I deploy the EJB project and the dynamic web project separately in Glassfish, the servlet cannot read the JNDI name of the EJB.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The cross-project injection probably doesn't work.

Have you tried to do a manual lookup of the bean (from the InitialContext)?
 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. I tried to use both @EJB (lookup= ...) and InitialContext to lookup with the cross project injection. Both of them don't work.
My third trial is to put the servlet in one .ear and the EJB in another .ear. I deployed the two .ear files. But it does not work either.

I found out from an Oracle documentation saying that usually an EJB in one .ear does not communicate with another EJB in another .ear. That says two application won't talk to each other.

 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried it out and I got the example working. However it sometimes throws a javax.ejb.NoSuchObjectLocalException. I tried it from different browsers and FireFox seams to work most of the times.

What kind of error did your get?
 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Fritz. Thanks so much for your help.
This is my experiment 1 using InitialContext lookup for everyone's reference :


I deployed the EJB and servlet. But I got this from the console:

Info: Portable JNDI names for EJB StatefulSession1Bean : [java:global/RemoteSessionBean0309/StatefulSession1Bean, java:global/RemoteSessionBean0309/StatefulSession1Bean!com.ivan.scbcd6.StatefulSession1Remote]
Info: RemoteSessionBean0309 was successfully deployed in 390 milliseconds....

Info: WEB0671: Loading application [RemoteJavaEEClient0309] at [/RemoteJavaEEClient0309]
Info: RemoteJavaEEClient0309 was successfully deployed in 952 milliseconds.

Info: *** StatefulSession1Bean1.created.
Info: PWC1412: WebModule[null] ServletContext.log():PWC1409: Marking servlet RemoteEJBClientServlet1 as unavailable
Warning: StandardWrapperValve[RemoteEJBClientServlet1]: PWC1382: Allocate exception for servlet RemoteEJBClientServlet1
com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class com.ivan.scbcd6.RemoteEJBClientServlet1
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:317)
at com.sun.enterprise.web.WebContainer.createServletInstance(WebContainer.java:702)
at com.sun.enterprise.web.WebModule.createServletInstance(WebModule.java:1958)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1263)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:1070)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:189)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:326)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:170)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)
Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting invoke lifecycle method public void com.ivan.scbcd6.RemoteEJBClientServlet1.initialize()
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.invokeLifecycleMethod(InjectionManagerImpl.java:737)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:492)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectInstance(InjectionManagerImpl.java:146)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectInstance(InjectionManagerImpl.java:132)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:311)
... 28 more
Caused by: java.security.PrivilegedActionException: java.lang.reflect.InvocationTargetException
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.invokeLifecycleMethod(InjectionManagerImpl.java:722)
... 32 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl$3.run(InjectionManagerImpl.java:728)
... 34 more
Caused by: java.lang.ClassCastException: com.sun.proxy.$Proxy125 cannot be cast to com.ivan.scbcd6.StatefulSession1Remote
at com.ivan.scbcd6.RemoteEJBClientServlet1.initialize(RemoteEJBClientServlet1.java:32)
... 39 more
FileStructures.png
[Thumbnail for FileStructures.png]
File structure of the EJB bean and servlet
 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I clean up the published resources from experiment 1 in Glassfish before I did experiment 2.
Experiment 2 using @EJB for dependency injection:



Info: Portable JNDI names for EJB StatefulSession1Bean : [java:global/RemoteSessionBean0309/StatefulSession1Bean, java:global/RemoteSessionBean0309/StatefulSession1Bean!com.ivan.scbcd6.StatefulSession1Remote]
Info: RemoteSessionBean0309 was successfully deployed in 250 milliseconds.
Info: WEB0671: Loading application [RemoteJavaEEClient0309] at [/RemoteJavaEEClient0309]
Info: RemoteJavaEEClient0309 was successfully deployed in 281 milliseconds.

Info: *** StatefulSession1Bean1.created.
Info: PWC1412: WebModule[null] ServletContext.log():PWC1409: Marking servlet RemoteEJBClientServlet1 as unavailable
Warning: StandardWrapperValve[RemoteEJBClientServlet1]: PWC1382: Allocate exception for servlet RemoteEJBClientServlet1
com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class com.ivan.scbcd6.RemoteEJBClientServlet1
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:317)
at com.sun.enterprise.web.WebContainer.createServletInstance(WebContainer.java:702)
....
Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=com.ivan.scbcd6.RemoteEJBClientServlet1/mRemoteSessionBean,Remote 3.x interface =com.ivan.scbcd6.StatefulSession1Remote,ejb-link=null,lookup=java:global/RemoteSessionBean0309/StatefulSession1Bean,mappedName=,jndi-name=,refType=Session into class com.ivan.scbcd6.RemoteEJBClientServlet1
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:698)

...
Caused by: java.lang.IllegalArgumentException: Can not set com.ivan.scbcd6.StatefulSession1Remote field com.ivan.scbcd6.RemoteEJBClientServlet1.mRemoteSessionBean to com.sun.proxy.$Proxy139
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:63)
at java.lang.reflect.Field.set(Field.java:657)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:649)
... 32 more

 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Experiment 3, archive the EJB in a .jar file, put it in the WEB-INF/lib folder of the dynamic web project , in which the servlet client is.

But the error message is the same.

Info: Portable JNDI names for EJB StatefulSession1Bean : [java:global/RemoteSessionBean0309/StatefulSession1Bean, java:global/RemoteSessionBean0309/StatefulSession1Bean!com.ivan.scbcd6.StatefulSession1Remote]
Info: RemoteSessionBean0309 was successfully deployed in 312 milliseconds.


Info: Portable JNDI names for EJB StatefulSession1Bean : [java:global/RemoteJavaEEClient0309/StatefulSession1Bean, java:global/RemoteJavaEEClient0309/StatefulSession1Bean!com.ivan.scbcd6.StatefulSession1Remote]
Info: WEB0671: Loading application [RemoteJavaEEClient0309] at [/RemoteJavaEEClient0309]
Info: RemoteJavaEEClient0309 was successfully deployed in 312 milliseconds.

Info: *** StatefulSession1Bean1.created.
Info: PWC1412: WebModule[null] ServletContext.log():PWC1409: Marking servlet RemoteEJBClientServlet1 as unavailable
Warning: StandardWrapperValve[RemoteEJBClientServlet1]: PWC1382: Allocate exception for servlet RemoteEJBClientServlet1
com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class com.ivan.scbcd6.RemoteEJBClientServlet1
...
Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=com.ivan.scbcd6.RemoteEJBClientServlet1/mRemoteSessionBean,Remote 3.x interface =com.ivan.scbcd6.StatefulSession1Remote,ejb-link=null,lookup=java:global/RemoteSessionBean0309/StatefulSession1Bean,mappedName=,jndi-name=,refType=Session into class com.ivan.scbcd6.RemoteEJBClientServlet1
...
Caused by: java.lang.IllegalArgumentException: Can not set com.ivan.scbcd6.StatefulSession1Remote field com.ivan.scbcd6.RemoteEJBClientServlet1.mRemoteSessionBean to com.sun.proxy.$Proxy139
...

FileStructure2.png
[Thumbnail for FileStructure2.png]
 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only experiment works is the package the EJB in .jar and the remote interface in ejb-client.jar file. The servlet is in a .war file.
With dependency injection, it works like this:

Info: Portable JNDI names for EJB StatefulSession1Bean : [java:global/RemoteEJB0309EAR/RemoteEJB0309/StatefulSession1Bean!com.ivan.scbcd6.StatefulSession1Remote, java:global/RemoteEJB0309EAR/RemoteEJB0309/StatefulSession1Bean]
Info: WEB0671: Loading application [RemoteEJB0309EAR#RemoteClient0309.war] at [RemoteClient0309]
Info: RemoteEJB0309EAR was successfully deployed in 624 milliseconds.

Info: *** StatefulSession1Bean1.created.
Info: StatefulSession1Bean.processList: This list contains :[ string 1, string 2, last string, ]
Info: List after having invoked EJB processList:[ string 1, string 2, last string, String added in EJB,
 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Experiment 5 is to package the servlet in .war file. In WEB-INF/lib, put the .jar where the EJB is.
Only deploy the .war file to Glassfish. Lookup the EJB from the .war file. In this case the .war file is called RemoteJavaEEClient0309.


Portable JNDI names for EJB StatefulSession1Bean : [java:global/RemoteJavaEEClient0309/StatefulSession1Bean, java:global/RemoteJavaEEClient0309/StatefulSession1Bean!com.ivan.scbcd6.StatefulSession1Remote]
Info: WEB0671: Loading application [RemoteJavaEEClient0309] at [/RemoteJavaEEClient0309]
Info: RemoteJavaEEClient0309 was successfully deployed in 765 milliseconds.

Info: *** StatefulSession1Bean1.created.
Info: StatefulSession1Bean.processList: This list contains :[ string 1, string 2, last string, ]
Info: List after having invoked EJB processList:[ string 1, string 2, last string, String added in EJB, ]


fileStructure3.png
[Thumbnail for fileStructure3.png]
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some strange errors for which I have no logical explanation. I think your last two experiments are closer to reality situations though. So I wouldn't put too much effort in the other situations.
 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fritz and everyone who may be interested in this example,

I just realized that I made a mistake in the StatefulSession1Remote interface. I forgot to annotate this interface with @Remote.
After I put @Remote in StatefulSession1Remote interface, experiment 1 works as described in Ivan's notes.

The reason is without @Remote, but default it is @Local. That is why when I deployed the EJB and the servlet in the same .ear or include the .jar file (with the EJB in it) in .war's WEB-INF/lib, it works. And if I deployed .war and .jar separately, the servlet cannot lookup the bean as this bean is a local bean by default.

 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great that you solved it

Thanks for sharing!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic