1 Reply Latest reply on Oct 7, 2007 6:05 AM by Ales Justin

    Newbie trying to use Spring 2.0 and EJB3 on Jboss 4.2.1

    Vasudha Venkatesha Murthy Newbie

      Hello,

      I am a newbie trying to use Spring 2.0 and EJB3 together for a project.

      I have EJB3 APIs that work fine when I access them through the JSP, withput the Spring framework on JBoss 4.2.1.
      Now I'm trying to introduce Spring 2.0 to the picture and want the Spring beans to call my EJB Session beans.

      The following is my Jboss-spring.xml file

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/jee
       http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
      
      <bean id="personstatelessejb" class="org.springframework.jndi.JndiObjectFactoryBean">
       <property name="jndiName" value="Lms/PersonStatelessBean/remote" />
       <property name="resourceRef" value="true" />
      </bean>
      <bean id="personSpringBean" class="com.lms.spring.PersonSpringBean">
       <property name="personStateless" ref="personstatelessejb" />
      </bean>
      </beans>
      



      This is my EJB session bean

      package com.lms.ejbs;
      
      import javax.ejb.*;
      import javax.persistence.*;
      import java.util.*;
      
      @Stateless
      public class PersonStatelessBean implements PersonStateless {
       @PersistenceContext(unitName="lmsPU") private EntityManager em;
       public String testBean() {
       return "the server returned this string";
       }
      
       public List<Person> getAllPersons()
       {
       ArrayList<Person> toReturn = new ArrayList();
       Query q = em.createQuery("SELECT o FROM Person o");
       for (Object po : q.getResultList()) {
       toReturn.add((Person) po);
       }
       return toReturn;
       }
      /**validates user and returns true/false
      
      
       @author Vasudha*/
      
       public boolean validateUser(String email, String password) throws UserNotFoundException
       {
       boolean result = false;
       System.out.println("validateUser() begin");
       Query q = em.createQuery("select person from com.lms.ejbs.Person as person where person.email = :email and person.password = :password");
       q.setParameter("email",email);
       q.setParameter("password",password);
       try
       {
      
       Person person = (Person)q.getSingleResult();
      
       if(person != null)
       {
       result = true;
       System.out.println("Output from named query");
       System.out.println(person.getFirstName());
       System.out.println(person.getLastName());
      
       em.flush();
       }
       }
       catch(NoResultException nre)
       {
       System.out.println("Invalid Username password ");
      throw new UserNotFoundException("Invalid username and password");
      
       }
       catch(NonUniqueResultException nure)
       {
       System.out.println("Multiple results");
       throw new UserNotFoundException("Invalid username and password");
       }
      
       System.out.println("validateUser() end");
       return result;
      
      
       }
      
       /**retrieves a Person's profile object
      
       @throws UserNotFoundException
       @author Vasudha*/
      
       public Person getPerson(String email) throws UserNotFoundException
       {
       Query q = em.createQuery("select person from com.lms.ejbs.Person as person where person.email =:email");
       q.setParameter("email",email);
       Person person = null;
       try
       {
       person = (Person)q.getSingleResult();
       }
       catch(NoResultException nre)
       {
       System.out.println("Invalid username ");
       throw new UserNotFoundException("Invalid username");
       }
       catch(NonUniqueResultException nure)
       {
       System.out.println("Multiple results");
       throw new UserNotFoundException("Invalid username");
       }
      
       return person;
       }
      
      
      }



      The following is my Spring bean code:

      package com.lms.spring;
      
      import java.util.List;
      
      import com.lms.ejbs.Person;
      import com.lms.ejbs.PersonStateless;
      
      public class PersonSpringBean implements IPersonSpring {
      
      
       private PersonStateless personStateless;
       public PersonSpringBean() {
       }
      
       // Setter injection of PersonStatelessEJB
       public void setPersonStateless(PersonStateless personStateless) {
       this.personStateless = personStateless;
       }
      
       public String testBean() {
       return personStateless.testBean();
       }
       public List<Person> getAllPersons() {
       List<Person> lp = null;
       try {
       lp = personStateless.getAllPersons();
       } catch(Exception e) {
       e.printStackTrace();
       }
       return lp;
       }
      
       public boolean validateUser(String email, String password) {
       try {
       return personStateless.validateUser(email, password);
       } catch(Exception e) {
       e.printStackTrace();
       }
       return false;
       }
       public Person getPerson(String email) {
       Person p = null;
       try {
       p = personStateless.getPerson(email);
       } catch(Exception e) {
       e.printStackTrace();
       }
       return p;
       }
      }
      


      The following is my servelet code:

      package com.lms.web;
      
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      import javax.servlet.*;
      import javax.servlet.http.*;
      
      import org.springframework.beans.factory.BeanFactory;
      import org.springframework.beans.factory.xml.XmlBeanFactory;
      import org.springframework.core.io.ClassPathResource;
      
      import java.io.*;
      import java.util.List;
      
      import com.lms.ejbs.*;
      import com.lms.spring.PersonSpringBean;
      
      
      
      public class LmsStatelessEJBServlet extends HttpServlet {
      
       private PersonSpringBean statelessBean;
       //private PersonStateless statelessBean;
      
      
       public void init() {
       /*try {
       Context context = new InitialContext();
       statelessBean = (PersonStateless) context.lookup("PersonStatelessBean/remote");
       } catch (NamingException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       }
       */
       try {
       Context context = new InitialContext();
       statelessBean = (PersonSpringBean) context.lookup("Lmss/personSpringBean");
       System.out.println(statelessBean);
       } catch (NamingException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       }
      
      
       // BeanFactory factory =
       // new XmlBeanFactory(new ClassPathResource("jboss-spring.xml"));
       //statelessBean =
       // (PersonSpringBean) factory.getBean("personSpringBean");
       }
      
       public void doPost(HttpServletRequest req, HttpServletResponse resp)
       throws ServletException, IOException {
       doGet(req, resp);
       }
      
       public void doGet(HttpServletRequest req, HttpServletResponse resp)
       throws ServletException, IOException
       {
       PrintWriter writer = resp.getWriter();
       writer.write("The stateless bean returned this string: " +
       statelessBean.testBean());
      
      
      
      
       List<Person> people = statelessBean.getAllPersons();
       for (Person person : people) {
       writer.write("Email retrieved: " + person.getEmail() + "\n");
       }
       try
       {
       boolean result = statelessBean.validateUser("marzo@mail.com", "fred");
       writer.write("Result: " + result + "\n");
       }
       catch(Exception unfe)
       {
       writer.write("Invalid username/password");
       }
       }
      }


      I downloaded the jboss-spring-jdk5.jar. But there is no other associated files available for download for the latest version. Hence I included the jboss-service.xml from the previous version. And setup the deployer directory in the deploy folder of Jboss.

      I have three different packages that I deploy to the Jboss deploy folder

      1)Lms.ejb3 containing

      ->com.lms.ejbs.ejb3 classes
      ->META-INF/persistence.xml
      ->jndi.properties

      2)Lmss.jar containing

      ->spring bean classes,
      ->META-INF/jboss-spring.xml
      ->jndi.properties.

      3)Lmsw.war containing

      ->WEB-INF/classes/com.lms.web.LmsStatelessEjbServlet
      ->web.xml
      ->WEB-INF/log4j.properties
      ->jboss-spring.xml


      This is the error I get when I run the application:

      20:57:25,515 INFO [Server] JBoss (MX MicroKernel) [4.2.1.GA (build: SVNTag=JBos
      s_4_2_1_GA date=200707131605)] Started in 38s:31ms
      20:57:32,265 ERROR [STDERR] javax.naming.NameNotFoundException: Lmss not bound
      20:57:32,265 ERROR [STDERR] at org.jnp.server.NamingServer.getBinding(Naming
      Server.java:529)
      20:57:32,265 ERROR [STDERR] at org.jnp.server.NamingServer.getBinding(Naming
      Server.java:537)
      20:57:32,265 ERROR [STDERR] at org.jnp.server.NamingServer.getObject(NamingS
      erver.java:543)
      20:57:32,265 ERROR [STDERR] at org.jnp.server.NamingServer.lookup(NamingServ
      er.java:267)
      20:57:32,265 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(
      Native Method)
      20:57:32,265 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(N
      ativeMethodAccessorImpl.java:39)
      20:57:32,265 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invo
      ke(DelegatingMethodAccessorImpl.java:25)
      20:57:32,281 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:5
      85)
      20:57:32,281 ERROR [STDERR] at sun.rmi.server.UnicastServerRef.dispatch(Unic
      astServerRef.java:294)
      20:57:32,281 ERROR [STDERR] at sun.rmi.transport.Transport$1.run(Transport.j
      ava:153)
      20:57:32,281 ERROR [STDERR] at java.security.AccessController.doPrivileged(N
      ative Method)
      20:57:32,281 ERROR [STDERR] at sun.rmi.transport.Transport.serviceCall(Trans
      port.java:149)
      20:57:32,281 ERROR [STDERR] at sun.rmi.transport.tcp.TCPTransport.handleMess
      ages(TCPTransport.java:466)
      20:57:32,281 ERROR [STDERR] at sun.rmi.transport.tcp.TCPTransport$Connection
      Handler.run(TCPTransport.java:707)
      20:57:32,281 ERROR [STDERR] at java.lang.Thread.run(Thread.java:595)
      20:57:32,281 ERROR [STDERR] at sun.rmi.transport.StreamRemoteCall.exceptionR
      eceivedFromServer(StreamRemoteCall.java:247)
      20:57:32,281 ERROR [STDERR] at sun.rmi.transport.StreamRemoteCall.executeCal
      l(StreamRemoteCall.java:223)
      20:57:32,281 ERROR [STDERR] at sun.rmi.server.UnicastRef.invoke(UnicastRef.j
      ava:126)
      20:57:32,281 ERROR [STDERR] at org.jnp.server.NamingServer_Stub.lookup(Unkno
      wn Source)
      20:57:32,281 ERROR [STDERR] at org.jnp.interfaces.NamingContext.lookup(Namin
      gContext.java:627)
      20:57:32,281 ERROR [STDERR] at org.jnp.interfaces.NamingContext.lookup(Namin
      gContext.java:589)
      20:57:32,281 ERROR [STDERR] at javax.naming.InitialContext.lookup(InitialCon
      text.java:351)
      20:57:32,281 ERROR [STDERR] at com.lms.web.LmsStatelessEJBServlet.init(LmsSt
      atelessEJBServlet.java:38)
      20:57:32,281 ERROR [STDERR] at javax.servlet.GenericServlet.init(GenericServ
      let.java:212)
      20:57:32,281 ERROR [STDERR] at org.apache.catalina.core.StandardWrapper.load
      Servlet(StandardWrapper.java:1161)
      20:57:32,281 ERROR [STDERR] at org.apache.catalina.core.StandardWrapper.allo
      cate(StandardWrapper.java:806)
      20:57:32,281 ERROR [STDERR] at org.apache.catalina.core.StandardWrapperValve
      .invoke(StandardWrapperValve.java:129)
      20:57:32,281 ERROR [STDERR] at org.apache.catalina.core.StandardContextValve
      .invoke(StandardContextValve.java:175)
      20:57:32,281 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityAssocia
      tionValve.invoke(SecurityAssociationValve.java:179)
      20:57:32,281 ERROR [STDERR] at org.jboss.web.tomcat.security.JaccContextValv
      e.invoke(JaccContextValve.java:84)
      20:57:32,281 ERROR [STDERR] at org.apache.catalina.core.StandardHostValve.in
      voke(StandardHostValve.java:128)
      20:57:32,281 ERROR [STDERR] at org.apache.catalina.valves.ErrorReportValve.i
      nvoke(ErrorReportValve.java:104)
      20:57:32,281 ERROR [STDERR] at org.jboss.web.tomcat.service.jca.CachedConnec
      tionValve.invoke(CachedConnectionValve.java:157)
      20:57:32,281 ERROR [STDERR] at org.apache.catalina.core.StandardEngineValve.
      invoke(StandardEngineValve.java:109)
      20:57:32,281 ERROR [STDERR] at org.apache.catalina.connector.CoyoteAdapter.s
      ervice(CoyoteAdapter.java:241)
      20:57:32,281 ERROR [STDERR] at org.apache.coyote.http11.Http11Processor.proc
      ess(Http11Processor.java:844)
      20:57:32,281 ERROR [STDERR] at org.apache.coyote.http11.Http11Protocol$Http1
      1ConnectionHandler.process(Http11Protocol.java:580)
      20:57:32,281 ERROR [STDERR] at org.apache.tomcat.util.net.JIoEndpoint$Worker
      .run(JIoEndpoint.java:447)
      20:57:32,281 ERROR [STDERR] at java.lang.Thread.run(Thread.java:595)
      20:57:32,312 ERROR [[LmsStatelessEJBServlet]] Servlet.service() for servlet LmsS
      tatelessEJBServlet threw exception
      java.lang.NullPointerException
      at com.lms.web.LmsStatelessEJBServlet.doGet(LmsStatelessEJBServlet.java:
      62)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
      icationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
      ilterChain.java:206)
      at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFi
      lter.java:96)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
      icationFilterChain.java:235)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
      ilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
      alve.java:230)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
      alve.java:175)
      at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(Securit
      yAssociationValve.java:179)
      at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValv
      e.java:84)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
      ava:128)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
      ava:104)
      at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedC
      onnectionValve.java:157)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
      ve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
      a:241)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
      :844)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
      ss(Http11Protocol.java:580)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:44
      7)
      at java.lang.Thread.run(Thread.java:595)




      Can somebody please point out where I am wrong and throw some light on how to go about this?


      Thanks,
      Vasudha