0 Replies Latest reply on Jul 21, 2005 3:11 AM by Alan Ho

    JBOSS 4 Remote Access Stateless Session Bean

    Alan Ho Newbie

      I have gone thru the J2EE Tutorial which use the Sun Java System AS Platform Editon 8. I am now trying to deploy the currency conversion example (ConverterBean) into JBOSS4. The deployment looks OK as I can see the following in the JMX console:
      jboss.j2ee
      * jndiName=Converter,plugin=pool,service=EJB <<<<<
      * jndiName=Converter,service=EJB <<<<<<
      * module=converter-ejb.jar,service=EjbModule <<<<<
      * service=ClientDeployer
      * service=EARDeployer
      * service=EARDeployment,url='Converter.ear'
      jboss.web.deployment
      * id=-1738444633,war=ROOT.war
      * id=-1795632456,war=invoker.war
      * id=-26676653,war=jmx-console.war
      * id=-523202696,war=jbossmq-httpil.war
      * id=-811489709,war=web-console.war
      * id=1788975853,war=converter.war <<<<<
      * id=991583486,war=jboss-ws4ee.war

      However, when I access the JSP, the InitialContext lookup returns a NamingException error with the message 'Converter not bound'. I have lookedthru this forum with similar problem and try modifying the deployment descriptor but with no success. Could someone help? The following are a listing of the codes:

      [JSP Codes]
      <%@ page import="converter.Converter, converter.ConverterHome, javax.ejb.*, java.util.Properties, java.math.*, javax.naming.*, javax.rmi.PortableRemoteObject, java.rmi.RemoteException" %>
      <%!
      private Converter converter = null;
      String ErrorMsg = "";

      public void jspInit() {
      try {
      InitialContext ic = new InitialContext();
      ErrorMsg = "Got IC.";
      Object objRef = ic.lookup("java:comp/env/Converter");
      ErrorMsg = ErrorMsg + "lookup ConverterBean.";
      ConverterHome home = (ConverterHome)PortableRemoteObject.narrow(objRef, ConverterHome.class);
      converter = home.create();
      } catch (RemoteException ex) {
      System.out.println("Couldn't create converter bean."+ ex.getMessage());
      ErrorMsg = ErrorMsg + "Remote Ex Couldn't create converter bean."+ ex.getMessage();
      } catch (CreateException ex) {
      System.out.println("Couldn't create converter bean."+ ex.getMessage());
      ErrorMsg = ErrorMsg + "CreateEx Couldn't create converter bean."+ ex.getMessage();
      } catch (NamingException ex) {
      System.out.println("Unable to lookup home: "+ "ConverterBean "+ ex.getMessage());
      ErrorMsg = ErrorMsg + "Unable to lookup home: "+ "Converter "+ ex.getMessage();
      }
      }

      public void jspDestroy() {
      converter = null;
      }
      %>


      Converter



      <h1>Converter</h1>

      Enter an amount to convert:







      Status <%= ErrorMsg %>
      <%
      String amount = request.getParameter("amount");
      if ( amount != null && amount.length() > 0 ) {
      BigDecimal d = new BigDecimal (amount);
      %>

      <%= amount %> dollars are <%= converter.dollarToYen(d) %> Yen.

      <%= amount %> Yen are <%= converter.yenToEuro(d) %> Euro.
      <%
      }
      %>




      [ConverterBean Codes]
      package converter;

      import java.rmi.RemoteException;
      import javax.ejb.SessionBean;
      import javax.ejb.SessionContext;
      import java.math.*;

      public class ConverterBean implements SessionBean {
      BigDecimal yenRate = new BigDecimal("121.6000");
      BigDecimal euroRate = new BigDecimal("0.0077");

      public ConverterBean() {
      }

      public BigDecimal dollarToYen(BigDecimal dollars) {
      BigDecimal result = dollars.multiply(yenRate);

      return result.setScale(2, BigDecimal.ROUND_UP);
      }

      public BigDecimal yenToEuro(BigDecimal yen) {
      BigDecimal result = yen.multiply(euroRate);

      return result.setScale(2, BigDecimal.ROUND_UP);
      }

      public void ejbCreate() {
      }

      public void ejbRemove() {
      }

      public void ejbActivate() {
      }

      public void ejbPassivate() {
      }

      public void setSessionContext(SessionContext sc) {
      }
      }

      [Converter Home Interface]
      package converter;

      import java.rmi.RemoteException;
      import javax.ejb.CreateException;
      import javax.ejb.EJBHome;


      public interface ConverterHome extends EJBHome {
      Converter create() throws RemoteException, CreateException;
      }

      [Converter Remote Interface]
      package converter;

      import javax.ejb.EJBObject;
      import java.rmi.RemoteException;
      import java.math.*;


      public interface Converter extends EJBObject {
      public BigDecimal dollarToYen(BigDecimal dollars) throws RemoteException;

      public BigDecimal yenToEuro(BigDecimal yen) throws RemoteException;
      }

      [ejb-jar.xml]
      <?xml version="1.0" encoding="UTF-8"?>
      <ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee" version="2.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
      <enterprise-beans>

      <ejb-name>ConverterBean</ejb-name>
      converter.ConverterHome
      converter.Converter
      <ejb-class>converter.ConverterBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Bean</transaction-type>

      </enterprise-beans>
      </ejb-jar>

      [jboss.xml]
      <!DOCTYPE jboss PUBLIC
      "-//JBoss//DTD JBOSS 4.0//EN"
      "http://www.jboss.org/j2ee/dtd/jboss_4_0.dtd">

      <enterprise-beans>

      <ejb-name>ConverterBean</ejb-name>
      <jndi-name>Converter</jndi-name>

      </enterprise-beans>


      [web.xml]
      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      <display-name>converter</display-name>
      <ejb-ref>
      <ejb-ref-name>ConverterBean</ejb-ref-name>
      <ejb-ref-type>Session</ejb-ref-type>
      converter.ConverterHome
      converter.Converter
      </ejb-ref>
      </web-app>

      [jboss-web.xml]
      <?xml version="1.0" encoding="UTF-8"?>

      <!DOCTYPE jboss-web PUBLIC
      "-//JBoss//DTD Web Application 2.4//EN"
      "http://www.jboss.org/j2ee/dtd/jboss-web_4_0.dtd">

      <jboss-web>
      <context-root>/converter</context-root>
      <ejb-ref>
      <ejb-ref-name>ConverterBean</ejb-ref-name>
      <jndi-name>Converter</jndi-name>
      </ejb-ref>
      </jboss-web>