Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: Problems creating EJB with Spring

  1. #1
    Join Date
    Apr 2006
    Posts
    28

    Default Problems creating EJB with Spring

    Hello,

    I have some problems creating a stateless SessionBean with spring. My code is this:

    public void ejbCreate() throws CreateException {
    System.out.println("ejbCreate");
    super.ejbCreate();
    }
    public void onEjbCreate() throws CreateException {
    System.out.println("onEjbCreate");
    }
    public void setSessionContext(SessionContext arg0)
    throws EJBException {
    try{
    super.setSessionContext(arg0);
    } catch (Exception e) {
    System.out.println(e);
    throw new EJBException(e.getMessage());
    }
    }

    <session >
    <description><![CDATA[]]></description>
    <ejb-name>BookLoanEJB</ejb-name>
    <home>com.geccodummy.ejb.BookLoanEJBHome</home>
    <remote>com.geccodummy.ejb.BookLoanEJBRemote</remote>
    <ejb-class>com.geccodummy.ejb.BookLoanEJB</ejb-class>
    <session-type>Stateless</session-type>
    <transaction-type>Bean</transaction-type>
    <env-entry> <env-entry-name>ejb/BeanFactoryPath</env-entry-name> <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>beanRefFactory.xml</env-entry-value>
    </env-entry>

    </session>
    </enterprise-beans>
    <session >
    <description><![CDATA[]]></description>
    <ejb-name>BookLoanEJB</ejb-name>
    <home>com.geccodummy.ejb.BookLoanEJBHome</home>
    <remote>com.geccodummy.ejb.BookLoanEJBRemote</remote>
    <ejb-class>com.geccodummy.ejb.BookLoanEJB</ejb-class>
    <session-type>Stateless</session-type>
    <transaction-type>Bean</transaction-type>
    <env-entry>
    <env-entry-name>ejb/BeanFactoryPath</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>beanRefFactory.xml</env-entry-value>
    </env-entry>
    </session>
    </enterprise-beans>


    beanRefFactory.xml:
    <bean id="global.spring.ejb.context" class="org.springframework.context.support.ClassPa thXmlApplicationContext"
    lazy-init="true">
    <constructor-arg>
    <list>
    <value>userapplication.xml</value>
    </list>
    </constructor-arg>
    </bean>
    If I call the EJB, I get this exception:

    $ 08:32:12,917 INFO [STDOUT] ejbCreate
    08:32:12,927 INFO [ContextJndiBeanFactoryLocator] BeanFactoryPath from JNDI is [beanRefFactory.xml]
    08:32:13,017 INFO [XmlBeanDefinitionReader] Loading XML bean definitions from class path resource [beanRefFactory.xml]
    08:32:13,017 ERROR [LogInterceptor] EJBException in method: public abstract com.geccodummy.ejb.BookLoanEJBRemote com.gec
    codummy.ejb.BookLoanEJBHome.create() throws java.rmi.RemoteException,javax.ejb.CreateException :
    javax.ejb.EJBException: Could not instantiate bean
    at org.jboss.ejb.plugins.AbstractInstancePool.get(Abs tractInstancePool.java:180)
    Changing the code has no effect:

    public void setSessionContext(SessionContext arg0) { setBeanFactoryLocatorKey("global.spring.ejb.contex t");
    setBeanFactoryLocator(ContextSingletonBeanFactoryL ocator.getInstance());
    }

    $ 08:42:55,161 INFO [STDOUT] ejbCreate
    08:42:55,161 ERROR [LogInterceptor] EJBException in method: public abstract com.geccodummy.ejb.BookLoanEJBRemote com.gec
    codummy.ejb.BookLoanEJBHome.create() throws java.rmi.RemoteException,javax.ejb.CreateException :
    javax.ejb.EJBException: Could not instantiate bean
    at org.jboss.ejb.plugins.AbstractInstancePool.get(Abs tractInstancePool.java:180)

    Any idea what the failire is?

    nort

  2. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Can you provide the full stacktrace?

    Regards,
    Andreas

  3. #3
    Join Date
    Apr 2006
    Posts
    28

    Default

    08:49:55,635 ERROR [LogInterceptor] EJBException in method: public abstract com.geccodummy.ejb.BookLoanEJBRemote com.gec
    codummy.ejb.BookLoanEJBHome.create() throws java.rmi.RemoteException,javax.ejb.CreateException :
    javax.ejb.EJBException: Could not instantiate bean
    at org.jboss.ejb.plugins.AbstractInstancePool.get(Abs tractInstancePool.java:180)
    at org.jboss.ejb.plugins.StatelessSessionInstanceInte rceptor.invokeHome(StatelessSessionInstanceInterce ptor.java
    :78)
    at org.jboss.ejb.plugins.SecurityInterceptor.invokeHo me(SecurityInterceptor.java:116)
    at org.jboss.ejb.plugins.LogInterceptor.invokeHome(Lo gInterceptor.java:121)
    at org.jboss.ejb.plugins.ProxyFactoryFinderIntercepto r.invokeHome(ProxyFactoryFinderInterceptor.java:93 )
    at org.jboss.ejb.SessionContainer.internalInvokeHome( SessionContainer.java:613)
    at org.jboss.ejb.Container.invoke(Container.java:894)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at org.jboss.mx.interceptor.ReflectedDispatcher.invok e(ReflectedDispatcher.java:141)
    at org.jboss.mx.server.Invocation.dispatch(Invocation .java:80)
    at org.jboss.mx.server.Invocation.invoke(Invocation.j ava:72)
    at org.jboss.mx.server.AbstractMBeanInvoker.invoke(Ab stractMBeanInvoker.java:249)
    at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanSe rverImpl.java:644)
    at org.jboss.invocation.jrmp.server.JRMPInvoker$MBean ServerAction.invoke(JRMPInvoker.java:805)
    at org.jboss.invocation.jrmp.server.JRMPInvoker.invok e(JRMPInvoker.java:406)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastSe rverRef.java:294)
    at sun.rmi.transport.Transport$1.run(Transport.java:1 53)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport. java:149)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages( TCPTransport.java:460)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandl er.run(TCPTransport.java:701)
    at java.lang.Thread.run(Thread.java:595)

  4. #4
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Unfortunately that seems not very meaningful to me, as only internal JBoss methods are mentioned. Are there further interesting informations in the JBoss log concerning your EJB?

    If not, maybe you can post the full code of your EJB class (if not too large) and the relevant part of your ejb-jar.xml file.

    Regards,
    Andreas

  5. #5
    Join Date
    Apr 2006
    Posts
    28

    Default

    Hello Andreas,

    unfortunatelly I do not have more stack. But I provide you the source code, it is a simle dummy-try of spring.

    public class BookLoanEJB extends AbstractStatelessSessionBean
    implements BookLoanEJBServiceInterface {

    private static final long serialVersionUID = 1L;
    private BookLoanServiceInterface bookService;

    public BookLoanEJB() {
    super();
    }

    public void ejbActivate() throws EJBException {
    }

    public void ejbPassivate() throws EJBException {
    }

    public void ejbRemove() throws EJBException {
    }

    public void ejbCreate() throws CreateException {
    System.out.println("ejbCreate");
    super.ejbCreate();
    }

    public void onEjbCreate() throws CreateException {
    System.out.println("onEjbCreate");
    try {
    bookService = (BookLoanServiceInterface) getBeanFactory().getBean(
    "bookService");
    } catch (Exception e) {
    System.out.println(e);
    throw new CreateException(e.getMessage());
    }

    }

    public void setSessionContext(SessionContext arg0)
    throws EJBException {
    super.setSessionContext(arg0);
    try{
    setBeanFactoryLocatorKey("global.spring.ejb.contex t");
    setBeanFactoryLocator(ContextSingletonBeanFactoryL ocator.getInstance());
    } catch (Exception e) {
    System.out.println(e);
    throw new EJBException(e.getMessage());
    }
    }

    public String sayHello() {
    System.out.println("sayHello");
    System.out.println("bookService: " + bookService);

    // return bookService.sayHello();
    return "a";
    }

    }

    <enterprise-beans>

    <!-- Session Beans -->
    <session >
    <description><![CDATA[]]></description>
    <ejb-name>BookLoanEJB</ejb-name>
    <home>com.geccodummy.ejb.BookLoanEJBHome</home>
    <remote>com.geccodummy.ejb.BookLoanEJBRemote</remote>
    <ejb-class>com.geccodummy.ejb.BookLoanEJB</ejb-class>
    <session-type>Stateless</session-type>
    <transaction-type>Bean</transaction-type>
    <env-entry>
    <env-entry-name>ejb/BeanFactoryPath</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>beanRefFactory.xml</env-entry-value>
    </env-entry>
    </session>
    </enterprise-beans>
    <bean id="global.spring.ejb.context"
    class="org.springframework.context.support.ClassPa thXmlApplicationContext"
    lazy-init="true">
    <constructor-arg>
    <list>
    <value>userapplication.xml</value>
    </list>
    </constructor-arg>
    </bean>
    I post it also as attachments.

    Thank you.
    Attached Files Attached Files

  6. #6
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Maybe you should remove the try/catch blocks inside setSessionContext() and onEjbCreate(), just letting exceptions pass through. That way, nested exceptions should appear in the log.
    Besides that, you could get rid of ejbRemove(), ejbCreate(), ejbActivate() and ejbPassivate() methods, as they are implemented in your superclass. One point in using the Spring superclass is to relieve the developer from implementing these methods.

    If you can produce a more detailed log without your exception handling code, please provide that also.

    Regards,
    Andreas

  7. #7
    Join Date
    Apr 2006
    Posts
    28

    Red face

    okay here we go. I have solved the problem it is running now.

    First I have changed my EJB to a simple EJB implementing this within the business method:

    ApplicationContext appContext =
    new ClassPathXmlApplicationContext(
    "/userapplication.xml");
    System.out.println("appContext: "+appContext);

    BeanFactory bf = (BeanFactory) appContext;
    System.out.println("BeanFactory: "+bf);

    bookService = (BookLoanServiceInterface)
    bf.getBean("bookService");
    So I was able to run my spring service. After this tep I have extended AbstractStatelessSessionBean and used these methods:

    public void onEjbCreate() throws CreateException {
    bookService = (BookLoanServiceInterface)
    getBeanFactory().getBean("bookService");
    }

    public void setSessionContext(SessionContext arg0) {
    super.setSessionContext(arg0);
    setBeanFactoryLocatorKey("global.spring.ejb.contex t");
    setBeanFactoryLocator(
    ContextSingletonBeanFactoryLocator.getInstance());
    }
    First I got the exception that the bean "global.spring.ejb.context" could not be found in the beanRefContext.xml configuration. So I have created this file and stored in my app.jar (in EAR) with the following content:

    <bean id="global.spring.ejb.context"
    class="org.springframework.context.support.ClassPa thXmlApplicationContext"
    lazy-init="true">
    <constructor-arg>
    <list>
    <value>userapplication.xml</value>
    </list>
    </constructor-arg>
    </bean>
    No I wonder why the env-entry in the EJB deployment descriptor has no effect. I have used this content:

    <session >
    <description><![CDATA[]]></description>
    <ejb-name>BookLoanEJB</ejb-name>
    <home>com.geccodummy.ejb.BookLoanEJBHome</home>
    <remote>com.geccodummy.ejb.BookLoanEJBRemote</remote>
    <ejb-class>com.geccodummy.ejb.BookLoanEJB</ejb-class>
    <session-type>Stateless</session-type>
    <transaction-type>Bean</transaction-type>
    <env-entry>
    <env-entry-name>ejb/BeanFactoryPath</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>beanRefReference.xml</env-entry-value>
    </env-entry>
    </session>
    My expectation was that the BeanFactory will search for this file. But instead to search the "beanRefReference.xml" was the "beanRefContext.xml" requested.

    Thank you.

  8. #8
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Quote Originally Posted by nort
    My expectation was that the BeanFactory will search for this file. But instead to search the "beanRefReference.xml" was the "beanRefContext.xml" requested.
    "java:comp/env/ejb/BeanFactoryPath" is the default BeanFactoryLocatorKey. Actually you overrode that key with your own value, also specifying another BeanFactoryLocator. Therefore the entry you provided in your ejb environment entry is not used.

    Have a look at the sources of AbstractEnterpriseBean, if you are curious about the lookup.

    Regards,
    Andreas

  9. #9
    Join Date
    Apr 2006
    Posts
    28

    Default

    I will, thank you Andreas

  10. #10
    Join Date
    Jan 2006
    Posts
    5

    Default

    Hi all,
    I am new in spring, when I try to create an MDB I got the same some issue as well, but i got it at the appserver startup moment.
    Looks like it try to invoke MessageListener.onMessage(Message msg) method at the startup.

    Here is error log:

    2006-05-25 15:44:49,092 DEBUG [org.springframework.beans.factory.xml.XmlBeanDefin itionReader] Using JAXP implementation [org.apache.xerces.jaxp.DocumentBuilderFactoryImpl@ 1292d12]
    2006-05-25 15:44:49,092 INFO [org.springframework.beans.factory.xml.XmlBeanDefin itionReader] Loading XML bean definitions from class path resource [applicationContext.xml]
    2006-05-25 15:44:49,112 ERROR [org.jboss.ejb.plugins.LogInterceptor] TransactionRolledbackLocalException in method: public abstract void javax.jms.MessageListener.onMessage(javax.jms.Mess age), causedBy:
    javax.ejb.EJBException: Could not instantiate bean
    at org.jboss.ejb.plugins.AbstractInstancePool.get(Abs tractInstancePool.java:180)
    at org.jboss.ejb.plugins.MessageDrivenInstanceInterce ptor.invoke(MessageDrivenInstanceInterceptor.java: 67)
    at org.jboss.ejb.plugins.CallValidationInterceptor.in voke(CallValidationInterceptor.java:48)
    at org.jboss.ejb.plugins.AbstractTxInterceptor.invoke Next(AbstractTxInterceptor.java:105)
    at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTran sactions(TxInterceptorCMT.java:335)
    at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxIn terceptorCMT.java:166)
    at org.jboss.ejb.plugins.RunAsSecurityInterceptor.inv oke(RunAsSecurityInterceptor.java:94)
    at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInt erceptor.java:192)
    at org.jboss.ejb.plugins.ProxyFactoryFinderIntercepto r.invoke(ProxyFactoryFinderInterceptor.java:122)
    at org.jboss.ejb.MessageDrivenContainer.internalInvok e(MessageDrivenContainer.java:389)
    at org.jboss.ejb.Container.invoke(Container.java:873)
    at org.jboss.ejb.plugins.jms.JMSContainerInvoker.invo ke(JMSContainerInvoker.java:1090)
    at org.jboss.ejb.plugins.jms.JMSContainerInvoker$Mess ageListenerImpl.onMessage(JMSContainerInvoker.java :1392)
    at org.jboss.jms.asf.StdServerSession.onMessage(StdSe rverSession.java:256)
    at org.jboss.mq.SpyMessageConsumer.sessionConsumerPro cessMessage(SpyMessageConsumer.java:904)
    at org.jboss.mq.SpyMessageConsumer.addMessage(SpyMess ageConsumer.java:160)
    at org.jboss.mq.SpySession.run(SpySession.java:333)
    at org.jboss.jms.asf.StdServerSession.run(StdServerSe ssion.java:180)
    at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Wo rker.run(PooledExecutor.java:743)
    at java.lang.Thread.run(Thread.java:595)
    This is my MDB code:

    public class TestMDBBean extends AbstractJmsMessageDrivenBean {

    private Logger logger = Logger.getLogger(TestMDBBean.class);

    protected void onEjbCreate() {
    // TODO Auto-generated method stub
    logger.info("%%%===>> It's try to initiate TestMDBBean!");
    }

    public void onMessage(Message arg0) {
    // TODO Auto-generated method stub
    logger.info("%%%===>> It's try to Send TestMDBBean!");
    }

    }
    and here is my ejb-jar.xml and jboss.xml

    ======>ejb-jar.xml
    <ejb-jar>
    <enterprise-beans>
    <message-driven>
    <ejb-name>TestMDBBean</ejb-name>
    <ejb-class>TestMDBBean</ejb-class>
    <transaction-type>Container</transaction-type>
    <acknowledge-mode>AUTO_ACKNOWLEDGE</acknowledge-mode>
    <message-driven-destination>
    <destination-type>javax.jms.Queue</destination-type>
    </message-driven-destination>
    <env-entry>
    <env-entry-name>ejb/BeanFactoryPath</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>applicationContext.xml</env-entry-value>
    </env-entry>
    <resource-ref>
    <res-ref-name>java:/XAConnectionFactory</res-ref-name>
    <res-type>javax.jms.QueueConnectionFactory</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Unshareable</res-sharing-scope>
    </resource-ref>
    </message-driven>
    </enterprise-beans>
    </ejb-jar>

    ======> jboss.xml
    <jboss>
    <enterprise-beans>
    <message-driven>
    <ejb-name>TestMDBBean</ejb-name>
    <destination-jndi-name>queue/testQueue</destination-jndi-name>
    <resource-ref>
    <res-ref-name>java:/XAConnectionFactory</res-ref-name>
    <jndi-name>XAConnectionFactory</jndi-name>
    </resource-ref>
    </message-driven>
    </enterprise-beans>
    </jboss>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •