Accessing an EJB 3.1 Singleton Bean with @Startup from a Servlet in the Same JVM Instance

I am building a project whereby I have a Singleton Bean that I want to fire up when the .jar is deployed in the EJB container, thus I have it annotated as follows:

package com.ryanchapin.ejbTest

@Startup
@Singleton

public class SingletonTest{}

Of course, I have the requisite Local and Remote interfaces set, up, I’ve just left that off for brevity. I am attempting to do a JNDI lookup, from a Servlet in the same JVM using the Local interface which results in:

22:14:31,481 ERROR
[org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/TestWebProject].[ChartData]]
Allocate exception for servlet TestServlet: java.lang.ClassCastException:
$Proxy188 cannot be cast to com.ryanchapin.ejbTestLocal
?at com.ryanchapin.TestServlet<init>(TestServlet.java:51) [:]
?at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)[:1.6.0_17]
?at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)[:1.6.0_17]
?at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)[:1.6.0_17]
?at java.lang.reflect.Constructor.newInstance(Constructor.java:532)[:1.6.0_17]
?at java.lang.Class.newInstance0(Class.java:372) [:1.6.0_17]
?at java.lang.Class.newInstance(Class.java:325) [:1.6.0_17]
?at org.jboss.web.tomcat.service.TomcatInjectionContainer.newInstance(TomcatInjectionContainer.java:280)[:6.0.0.Final]
?at org.jboss.web.tomcat.service.TomcatInjectionContainer.newInstance(TomcatInjectionContainer.java:264)[:6.0.0.Final]
?at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1162)[:6.0.0.Final]
?at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:955)[:6.0.0.Final]
?at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:188)[:6.0.0.Final]
?at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)[:6.0.0.Final]
?at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181)[:6.0.0.Final]
?at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285)[:1.1.0.Final]
?at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261)[:1.1.0.Final]
?at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88)[:6.0.0.Final]
?at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100)[:6.0.0.Final]
?at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)[:6.0.0.Final]
?at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)[:6.0.0.Final]
?at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)[:6.0.0.Final]
?at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)[:6.0.0.Final]
?at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53)[:6.0.0.Final]
?at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362)[:6.0.0.Final]
?at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)[:6.0.0.Final]
?at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654)[:6.0.0.Final]
?at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951)[:6.0.0.Final]
?at java.lang.Thread.run(Thread.java:636) [:1.6.0_17]

When I lookup the Remote interface JNDI name in the Servlet it works just fine. This only occurs if I have the bean annotated with @Startup. If I remove the @Startup annotation I can access the EJB via the local interface without a problem.

Perhaps a bug in Jboss-6.0.0, I’m not sure. For the time-being I am just going to use the remote interface.

?

Leave a Reply