Ola a todos,
Estou fazendo alguns testes com EJBs e esbarrei em alguns problemas de injeção. Basicamente não estou tendo sucesso ao tentar injetar um bean local em outro com a anotação @EJB e por lookup tudo funciona bem. Fiz uma pesquisa e fui chegando nessa implementação:
public interface DAO<T, I extends Serializable> {
// Implementação...
}
[code]/** Interface principal com as definições dos metodos para
-
as interfaces Remota e Local e o JNDI da interface Remota
/
public interface DAOFactory {
/* Java Naming and Directory Interface */
public static final String JNDI = “br.com.jp.DAOFactoryRemote”;Object get(final Class dao) throws Exception;
}[/code]
[code]@Local // Testado com e sem a anotação
public interface DAOFactoryLocal extends DAOFactory {
}
@Remote // Testado com e sem a anotação
public interface DAOFactoryRemote extends DAOFactory {
}[/code]
No caso dessa factory, o @Remote nem é necessário, só adicionei para fazer o lookup manualmente e testar o funcionamento.
[code]@Stateless // << Já tentei com name e mappedName definidos
@Local({DAOFactoryLocal.class})
@Remote({DAOFactoryRemote.class})
public class DAOFactoryBean implements DAOFactoryLocal, DAOFactoryRemote {
@PersistenceContext(name = Constants.PERSISTENCE_UNIT)
private EntityManager entityManager;
@Override
public Object get(Class<T> iDao) throws DAOException {
// Implementação...
}
//...
}[/code]
[code]@Stateless
@Local({TesteServiceFacadeLocal.class})
@Remote({TesteServiceFacadeRemote.class})
public class TesteServiceFacadeBean implements TesteServiceFacadeLocal, TesteServiceFacadeRemote {
/* Declarações de EJBs */
@EJB
private DAOFactoryLocal factory; // << O factory esta sempre nulo quanto utilizado o @EJB, por lookup esta ok.
/* Declarações de DAOs */
private TesteDAO testeDAO;
public TesteServiceFacadeBean(){
testeDAO = (TesteDAO) factory.get(TesteDAO.class);
}
//...
}[/code]
Essas são as informações de JNDI dos EJBs gerados no deploy:
INFO: EJB5181:Portable JNDI names for EJB DAOFactoryBean: [java:global/jP-ear-1.0/jP-ejb-1.0/DAOFactoryBean!br.com.jp.DAOFactoryLocal, java:global/jP-ear-1.0/jP-ejb-1.0/DAOFactoryBean!br.com.jp.DAOFactoryRemote]
INFO: EJB5182:Glassfish-specific (Non-portable) JNDI names for EJB DAOFactoryBean: [br.com.jp.DAOFactoryRemote, br.com.jp.DAOFactoryRemote#br.com.jp.DAOFactoryRemote]
E essa é a Exception que ocorre na classe TesteServiceFacadeBean devido ao DAOFactory nulo, mas como ela é resultado do null pointer do Factory, acredito que fica totalmete irrelevante ao problema.
AVISO: javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:454)
at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2547)
at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1899)
at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:205)
at com.sun.ejb.containers.EJBObjectInvocationHandlerDelegate.invoke(EJBObjectInvocationHandlerDelegate.java:79)
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.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:241)
at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invoke(StubInvocationHandlerImpl.java:152)
at com.sun.corba.ee.impl.presentation.rmi.codegen.CodegenStubBase.invoke(CodegenStubBase.java:227)
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.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
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 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
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: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:726)
at com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:247)
at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:449)
... 68 more
Caused by: javax.ejb.CreateException: Could not create stateless EJB
at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:534)
at com.sun.ejb.containers.StatelessSessionContainer.access$000(StatelessSessionContainer.java:95)
at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:724)
... 70 more
Caused by: java.lang.NullPointerException
at br.com.jp.TesteServiceFacadeBean.<init>(TesteServiceFacadeBean.java:34)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at com.sun.ejb.containers.BaseContainer._constructEJBInstance(BaseContainer.java:1663)
at com.sun.ejb.containers.BaseContainer.createEjbInstanceAndContext(BaseContainer.java:1646)
at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:475)
... 72 more
Eu acredito que possa ser alguma configuração ou algo que não esta com uma versão compatível com os recursos que eu estou utilizando.
Eu estou utilizando:
Maven 2, Hibernate 4, JDK 1.6, EJB3, GLASSFISH 3.1.2
Eu encontrei esses e em muitos outros links, mas não solucionam o problema que tive:
http://www.guj.com.br/java/198791-resolvido-testar-primeiro-ejb--null-pointer-exception
http://www.coderanch.com/t/441128/EJB-JEE/java/Inject-local-bean-EJB
https://forums.oracle.com/message/9410705
http://tomee.apache.org/examples-trunk/ejb-examples/
http://www.guj.com.br/java/301344-ejb--cdi--jsf--glassfish
http://www.guj.com.br/java/302850-glassfish--ejb--cdi--jsf2-problemas-na-unitpersistencesolved
http://www.guj.com.br/java/108569-problema-de-injecao-ejb
http://www.guj.com.br/java/221260-jsf-com-ejb-3---managedbean----facade----dao---problema-na-injecao-de-persistencia
Alguem tem alguma sugestão pesquisa?
Obrigado