1 Reply Latest reply on Apr 4, 2005 1:01 PM by Franco

    Client can't connect to EJB

    peter weik Newbie

      Hello,

      I am a newbie and I am trying to call a simple stateless ejb which is deployed (this worked) in Jboss 4.0,
      which doesn't work, which drives me cracy.
      The Client as shown below alway runs into the javax.naming.NamingException.
      My Operating System: Windows XP Prof,JBoss Version 4.0.0.
      Client and JBossServer are running on the same machine.

      It would be very kind of you if you could help me with this problem.

      Thanks,Peter



      /*The Client looks like this: */


      package peterprivat.testpeter;
      import java.util.Hashtable;
      import javax.naming.Context;
      import javax.naming.NamingException;
      import javax.ejb.CreateException;
      import javax.ejb.RemoveException;
      import java.rmi.RemoteException;
      import javax.naming.InitialContext;
      import com.weik.projectmanagement.EuroExchange;
      import com.weik.projectmanagement.EuroExchangeHome;
      import com.weik.projectmanagement.EuroExchangeBean;
      import javax.naming.NamingEnumeration;

      public class TestSessionBean {

      /** Creates a new instance of TestSessionBean */
      public TestSessionBean() {
      }

      private static javax.naming.Context getInitialContext() throws javax.naming.NamingException{
      Hashtable h = new Hashtable();
      h.put(Context.INITIAL_CONTEXT_FACTORY,
      "org.jnp.interfaces.NamingContextFactory");
      h.put(Context.PROVIDER_URL,
      "IIOP://odyseus:1099");
      h.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
      return new InitialContext(h);
      }
      /**
      * @param args the command line arguments
      */
      public static void main(String[] args) {
      javax.naming.Context ctx = null;
      String BeanName = "EuroExchangeBean";
      EuroExchangeHome home = null;
      EuroExchange remote = null;
      try{
      ctx = getInitialContext();
      /*NamingEnumeration enumx = ctx.list("java:com/env/ejb");
      while(enumx.hasMore()){
      String x = (String)enumx.next();
      System.out.println(x);
      }*/





      home = (EuroExchangeHome)javax.rmi.PortableRemoteObject.narrow(ctx.lookup(BeanName),
      EuroExchangeHome.class);
      remote = home.create();
      remote.setExchangeRate("Dollar", 2,3);
      remote.remove();
      }catch(javax.ejb.CreateException e){
      System.out.println("Fehler beim erzeugen der Bean");
      }catch(javax.ejb.RemoveException e){
      System.out.println("Fehler beim Löschen der Bean");
      }catch(javax.naming.NamingException e){
      System.out.println("Namensdienst meldet Fehler" + e.getMessage());
      }catch(RemoteException e){
      System.out.println("Probleme mit der Netzwerkverbindung");

      }






      }

      }






      --That are different messages of the jboss Console
      ...
      07:51:41,770 INFO [NamingService] Started jnpPort=1099, rmiPort=1098, backlog=50, bindAddress=/0.0.0.0, Client SocketFactory=null
      , Server SocketFactory=org.jboss.net.sockets.DefaultSocketFactory@ad093076
      ...
      7:52:59,031 INFO [EjbModule] Deploying EuroExchangeBean

      --that is the message in the jmx Console
      jndiName=EuroExchangeBean,plugin=pool,service=EJB
      jndiName=EuroExchangeBean,service=EJB

      --that is the ejb-jar.xml

      <ejb-name>EuroExchangeBean</ejb-name>
      com.weik.projectmanagement.EuroExchangeHome
      com.weik.projectmanagement.EuroExchange
      <ejb-class>com.weik.projectmanagement.EuroExchangeBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Bean</transaction-type>
      <resource-ref>
      <res-ref-name>jdbc/MySqlDS</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
      <res-sharing-scope>Shareable</res-sharing-scope>
      </resource-ref>

      *************************************************************
      --That is the Session Bean
      package com.weik.projectmanagement;
      import java.sql.Connection;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.sql.SQLException;

      import javax.ejb.CreateException;
      import javax.ejb.EJBException;
      import javax.ejb.SessionBean;
      import javax.ejb.SessionContext;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      import javax.sql.DataSource;

      public class EuroExchangeBean implements SessionBean {
      public static final String dbRef = "Java:/MySqlDS";
      private SessionContext beanCtx = null;
      private DataSource dataSource = null;

      public void ejbCreate() throws CreateException{
      try{
      Context c = new InitialContext();
      this.dataSource = (DataSource)c.lookup(dbRef);
      }catch(NamingException ex){
      String msg = "Cannot get Resource-Factory:" + ex.getMessage();
      throw new CreateException(msg);
      }
      }
      public void ejbRemove(){
      this.dataSource = null;
      }
      public void ejbPassivate(){}
      public void ejbActivate(){}
      public void setSessionContext(SessionContext ctx){
      this.beanCtx = ctx;
      }


      //The other Methods of the Remote Interface have been implemented as well ...
      public void setExchangeRate(String currency,
      float euro,
      float foreignCurr){
      if(currency == null){
      throw new EJBException("illegal argument: currency");
      }
      final String delQuery = "DELETE FROM EURO_EXCHANGE (CURRENCY, EURO,FOREIGNCURR) VALUES(?,?,?)";
      final String insQuery = "INSERT INTO EURO_EXCHANGE WHERE CURRENCY=?";
      Connection con = null;
      PreparedStatement del = null;
      PreparedStatement ins = null;
      boolean success = false;
      try{
      con = this.dataSource.getConnection();
      con.setAutoCommit(false);
      del = con.prepareStatement(delQuery);
      del.setString(1,currency);
      del.executeUpdate();

      ins = con.prepareStatement(insQuery);
      ins.setString(1,currency);
      ins.setFloat(2,euro);
      ins.setFloat(3,foreignCurr);
      ins.executeUpdate();

      success = true;
      }catch(SQLException ex){
      ex.printStackTrace();
      throw new EJBException("db.error:" + ex.getMessage());

      }finally{
      if(success){
      try{con.commit(); } catch(Exception ex){}
      } else{
      try{con.rollback(); } catch(Exception ex){}
      }


      try{del.close(); } catch(Exception ex){}
      try{ins.close(); } catch(Exception ex){}
      try{con.setAutoCommit(true); } catch(Exception ex){}
      try{con.close(); } catch(Exception ex){}

      }

      }
      /***********************
      That is the HomeInterface */
      package com.weik.projectmanagement;

      import java.rmi.RemoteException;
      import javax.ejb.CreateException;
      import javax.ejb.EJBHome;
      /*Home Interface der zustandslosen Session Bean EuroExchangeSL*/
      public interface EuroExchangeHome extends EJBHome {
      public EuroExchange create()
      throws RemoteException,CreateException;
      }

      /*******************************
      That is the Remote Interface*/
      package com.weik.projectmanagement;

      import java.rmi.RemoteException;
      import javax.ejb.EJBObject;
      /*Remote Interface der zustandslosen Session Bean EuroExchangeSL*/
      public interface EuroExchange extends EJBObject {

      public float changeFromEuro(String currency, float amount) throws RemoteException;

      public float changeToEuro(String currency, float amount) throws RemoteException;

      public void setExchangeRate(String currency, float euro,float foreignCurr) throws RemoteException;
      }