session bean - newbie question


EJB programming & troubleshooting: session bean - newbie question

  1. session bean - newbie question (1 messages)


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

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

    [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

    ------- ---------

    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) { }

    --------- -----------
    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;

    --- ----

    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)
    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.

    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"?>

    --------- ejb-jar.xml --------
    <?xml version="1.0"?>
    <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "">

    <display-name>A simple cart jar</display-name>
    <description>A demo cart bean package.</description>
    <display-name>Shopping Cart</display-name>
    <description>A simple shopping cart.</description>
  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")