Discussions

EJB programming & troubleshooting: session bean - newbie question

  1. session bean - newbie question (1 messages)

    Hi,

    I trying to build my first session bean. I'm using JBoss. It seems

    All the files are below, [ ejb-jar.xml, jboss.xml, cart.java, CartEJB.java, CartHome.java, Client.java]
    Here is the error I get from the client in ant..

    client:
    [java] Communication error: CartEJB not bound

    When I deploy the bean I get...

    22:17:35,531 INFO [MainDeployer] Starting deployment of package: file:/C:/jboss-3/server/default/deploy/stateful.jar
    22:17:35,621 INFO [EjbModule] Creating
    22:17:35,641 INFO [EjbModule] Deploying CartEJB
    22:17:35,661 INFO [EjbModule] Created
    22:17:35,661 INFO [EjbModule] Starting
    22:17:35,701 INFO [EjbModule] Started
    22:17:35,711 INFO [MainDeployer] Deployed package: file:/C:/jboss-3/server/default/deploy/stateful.jar

    which looks fine right ?

    Where am I making the mistake? Thanks


    ------- cart.java ---------

    package com.stateful.cart;

    import java.util.*;
    import javax.ejb.*;
    import java.rmi.RemoteException;

    public interface Cart extends EJBObject
    {
    public void addProduct(String product) throws RemoteException;
    public Collection getProducts() throws RemoteException;
    }

    ------- CartEJB -----------
    package com.stateful.cart;

    import java.util.*;
    import javax.ejb.*;
    import java.rmi.RemoteException;

    public class CartEJB implements SessionBean
    {
    private Collection products = new ArrayList();
    public void ejbCreate() { }

    public void addProduct(String product)
    { products.add(product); }


    public Collection getProducts()
    { return products; }

    public void ejbActivate() { }

    public void ejbPassivate() { }

    public void ejbRemove() { }

    public void setSessionContext(SessionContext context) { }
    }


    --------- CartHome.java -----------
    package com.stateful.cart;

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

    /**
     * The home for the Carts, this is where new Carts are created.
     */
    public interface CartHome extends EJBHome
    {
    public Cart create() throws CreateException, RemoteException;
    }


    --- client.java ----


    package com.stateful.client;

    import com.stateful.cart.Cart;
    import com.stateful.cart.CartHome;

    import javax.ejb.*;
    import javax.naming.*;
    import javax.rmi.PortableRemoteObject;
    import java.rmi.RemoteException;

    public class Client
    {
    public static void main(String[] args)
    {
    try
    {
    Context context = new InitialContext();
    Object homeObject = context.lookup("CartEJB");
    CartHome home = (CartHome)PortableRemoteObject.narrow(homeObject, CartHome.class);
    Cart cart = (Cart)PortableRemoteObject.narrow(home.create(), Cart.class);
    // Add some items to the Cart.
    cart.addProduct("Milk");
    cart.addProduct("Apples");
    cart.addProduct("Pizza");

    System.out.println("Number of products added: "+cart.getProducts().size());
    }
    catch(RemoteException e)
    { System.err.println("System/communication error: " + e.getMessage()); }
    catch(NamingException e)
    { System.err.println("Communication error: " + e.getMessage()); }
    catch(CreateException e)
    { System.err.println("Error creating cart: " + e.getMessage()); }
    }
    }

    --------- jboss.xml --------
    <?xml version="1.0"?>
    <jboss>
         <enterprise-beans>
           <entity>
    <ejb-name>CartEJB</ejb-name>
    <jndi-name>CartHome</jndi-name>
           </entity>
         </enterprise-beans>
    </jboss>

    --------- ejb-jar.xml --------
    <?xml version="1.0"?>
    <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">

    <ejb-jar>
    <display-name>A simple cart jar</display-name>
    <description>A demo cart bean package.</description>
    <enterprise-beans>
    <session>
    <display-name>Shopping Cart</display-name>
    <description>A simple shopping cart.</description>
    <ejb-name>CartEJB</ejb-name>
    <home>com.stateful.cart.CartHome</home>
    <remote>com.stateful.cart.Cart</remote>
    <ejb-class>com.stateful.cart.CartEJB</ejb-class>
    <session-type>Stateful</session-type>
    <transaction-type>Container</transaction-type>
    </session>
    </enterprise-beans>
    </ejb-jar>
  2. session bean - newbie question[ Go to top ]

    Since you declared the jndi-name in jboss.xml as "CartHome", you should use CartHome in your call to lookup. context.lookup("CartHome") instead of context.lookup("CartEJB")