Discussions

EJB programming & troubleshooting: Session Facade - Where am I going Wrong?

  1. Session Facade - Where am I going Wrong? (6 messages)

    Hi All,
    I am trying to learn EJB along with session facade. I created a Stateless session bean called Trading. It worked fine. Later I created a CMP called Customer. My intension is to keep the Trading as a facade to CMP customer. I created only Local home and remote interfaces for Customer but did not create any Remote or Home interface (the remote ones). I updated Trading xmls and included this Customer souce code and deploy the jar. Now in my client it looking up for Trading bean remotely and is fine but it's failed lookup for my local Customer interface. Am I going wron any where? Please let me know. Here are my Java Files and XMLs

    Trading Remote interface looks like this
    public interface Trading extends EJBObject{
     public String loginUser(String userName, String password)
        throws RemoteException;
    public CustomerData getCustomerData(String userID)
        throws RemoteException;
    }

    The create method in my trading bean looks like this
    public void ejbCreate ()
           throws javax.ejb.CreateException, RemoteException {
     customerLocalHome = CustomerUtil.getLocalHome();
    }

    The method getLocalHome() actually looks up for local bean and its implementation looks like this
     public static CustomerLocalHome getLocalHome(){
      CustomerLocalHome localHome = null;
       try {
        InitialContext ic = new InitialContext();
         Object ref = ic.lookup("CustomerLocal");
         localHome = (CustomerLocalHome) ref;
       } catch (Exception e) {
        System.out.println(e.getExplanation());
      }
      return localHome;
    }

    Further the getCustomerData on my Trading looks like this Bean
    public CustomerData getCustomerData(){
     return new CustomerData ( getCustomerID(), getUserID(),
       getFirstName(), getLastName(), getAddress(), getPhone(),
       getStatus(), getCreditLimit(), getBlockAfterLimit());
    } Customer Data is a normal java bean for my Customer CMP, like a DataBean.

    This is how I defined my XMLs
    ejb-jar.xml...............
    <ejb-jar>
     <enterprise-beans>
      <session>
       <ejb-name>Trading</ejb-name>
        <home>com.vj.trading.facade.TradingHome</home>
        <remote>com.vj.trading.facade.Trading</remote>
        <local-home>com.vj.trading.facade.TradingLocalHome
             </local-home>
        <local>com.vj.trading.facade.TradingLocal</local>
        <ejb-class>com.vj.trading.facade.TradingSession
             </ejb-class>
        <session-type>Stateless</session-type>
        <transaction-type>Container</transaction-type>

        <ejb-local-ref>
         <ejb-ref-name>ejb/CustomerLocal</ejb-ref-name>
         <ejb-ref-type>Entity</ejb-ref-type>
         <local-home>inv.customer.CustomerLocalHome</local-home>
         <local>inv.customer.CustomerLocal</local>
         <ejb-link>Customer</ejb-link>
        </ejb-local-ref>
        
        <resource-ref>
    <res-ref-name>jdbc/trading</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
        </resource-ref>
       </session>
     </enterprise-beans>
    </ejb-jar>

    jboss.xml ..........
    <ejb-jar>
     <enterprise-beans>
      <session>
       <ejb-name>Trading</ejb-name>
       <jndi-name>TradingBean</jndi-name>
       <local-jndi-name>TradingLocal</local-jndi-name>
       
       <ejb-local-ref>
        <ejb-ref-name>ejb/CustomerLocal</ejb-ref-name>
        <jndi-name>CustomerLocal</jndi-name>
       </ejb-local-ref>
       
       <resource-ref>
        <res-ref-name>jdbc/trading</res-ref-name>
        <jndi-name>trading</jndi-name>
       </resource-ref>
      </session>
     </enterprise-beans>
    </ejb-jar>

    Given the above status of my file I get an error "CustomerLocal not bound" ::)

    Please someone let me know where I am going wrong? Do I have to define Remote and Home Interfaces though I am not going to use them?
    Thanks for Reading this Thread.
    Gautham
  2. Hi All,I am trying to learn EJB along with session facade. I created a Stateless session bean called Trading. It worked fine. Later I created a CMP called Customer. My intension is to keep the Trading as a facade to CMP customer. I created only Local home and remote interfaces for Customer but did not create any Remote or Home interface (the remote ones). I updated Trading xmls and included this Customer souce code and deploy the jar. Now in my client it looking up for Trading bean remotely and is fine but it's failed lookup for my local Customer interface. Am I going wron any where? Please let me know. Here are my Java Files and XMLsTrading Remote interface looks like thispublic interface Trading extends EJBObject{ public String loginUser(String userName, String password)    throws RemoteException;public CustomerData getCustomerData(String userID)    throws RemoteException;}The create method in my trading bean looks like thispublic void ejbCreate ()        throws javax.ejb.CreateException, RemoteException { customerLocalHome = CustomerUtil.getLocalHome();}The method getLocalHome() actually looks up for local bean and its implementation looks like this public static CustomerLocalHome getLocalHome(){   CustomerLocalHome localHome = null;   try {    InitialContext ic = new InitialContext();     Object ref = ic.lookup("CustomerLocal");     localHome = (CustomerLocalHome) ref;    } catch (Exception e) {    System.out.println(e.getExplanation());  }   return localHome; }Further the getCustomerData on my Trading looks like this Bean public CustomerData getCustomerData(){ return new CustomerData ( getCustomerID(), getUserID(),   getFirstName(), getLastName(), getAddress(), getPhone(),   getStatus(), getCreditLimit(), getBlockAfterLimit());} Customer Data is a normal java bean for my Customer CMP, like a DataBean.This is how I defined my XMLsejb-jar.xml...............<ejb-jar> <enterprise-beans>  <session>   <ejb-name>Trading</ejb-name>    <home>com.vj.trading.facade.TradingHome</home>     <remote>com.vj.trading.facade.Trading</remote>    <local-home>com.vj.trading.facade.TradingLocalHome         </local-home>    <local>com.vj.trading.facade.TradingLocal</local>    <ejb-class>com.vj.trading.facade.TradingSession         </ejb-class>    <session-type>Stateless</session-type>    <transaction-type>Container</transaction-type>    <ejb-local-ref>     <ejb-ref-name>ejb/CustomerLocal</ejb-ref-name>     <ejb-ref-type>Entity</ejb-ref-type>     <local-home>inv.customer.CustomerLocalHome</local-home>     <local>inv.customer.CustomerLocal</local>     <ejb-link>Customer</ejb-link>    </ejb-local-ref>        <resource-ref> <res-ref-name>jdbc/trading</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth>    </resource-ref>   </session> </enterprise-beans></ejb-jar>jboss.xml ..........<ejb-jar> <enterprise-beans>  <session>   <ejb-name>Trading</ejb-name>   <jndi-name>TradingBean</jndi-name>   <local-jndi-name>TradingLocal</local-jndi-name>      <ejb-local-ref>    <ejb-ref-name>ejb/CustomerLocal</ejb-ref-name>    <jndi-name>CustomerLocal</jndi-name>   </ejb-local-ref>      <resource-ref>    <res-ref-name>jdbc/trading</res-ref-name>    <jndi-name>trading</jndi-name>   </resource-ref>  </session> </enterprise-beans></ejb-jar>Given the above status of my file I get an error "CustomerLocal not bound" ::)Please someone let me know where I am going wrong? Do I have to define Remote and Home Interfaces though I am not going to use them?Thanks for Reading this Thread.Gautham
    Hi Gautam,

    I think you are doing lookup by JNDI name but i have experienced it works if you make it by ejb-ref-name as "ejb/CustomerLocal" So i think u might have to change the lookup line.
    So instead of writing it as

    Object ref = ic.lookup("CustomerLocal");

    try it as

    Object ref = ic.lookup("java:comp/env/ejb/CustomerLocal");

    I hope it works for you...

    Regards,

    Rahul.
  3. Thanks for the Reply Rahul,
    I tried all possible combinations but didn't workout. I am not sure whether I need to define Remote and Home interfaces for Customer too.
    Regards,
    Gautham
  4. Session Facade - Where am I going Wrong?[ Go to top ]

    Myself was able to fix for this but I am really not convinced, can anyone let me know whether I am correct or ??
    I didn't create any remote or home interface for Customer(only local interfaces) and had to introduce the following stuff in my ejb-jar.xml
    <entity>
     <ejb-name>ejb/CustomerLocal</ejb-name>
     <local-home>com.vj.trading.customer.CustomerLocalHome</local-home>
     <local>com.vj.trading.customer.CustomerLocal</local>
     <ejb-class>com.vj.trading.customer.CustomerBMP</ejb-class>
     <persistence-type>Bean</persistence-type>
     <prim-key-class>com.vj.trading.customer.CustomerPK</prim-key-class>
     <reentrant>False</reentrant>
    </entity>

    in jboss.xml I had to introduce
    <entity>
     <ejb-name>ejb/CustomerLocal</ejb-name>
     <jndi-name>CustomerBean</jndi-name>
     <local-jndi-name>CustomerLocal</local-jndi-name>
    </entity>

    More importantly I can keep the local references in Session bean or I can delete them both the cases it works fine. Further it is using local interfaces only. My googling says that I have to link them with <ejb-local-ref> which I am not doing now.

    I appreciate someone who clarify Whether this approah has any limitations.
    Thanks
    Gautham
  5. Hi there,
    if you look at the managment console(http://localhost:8080/jmx-console/) provided by jboss you will see the name
    which your local ejb gets bound to.
    it will have local attached to it,
    i did this long time back cant remember exatly but you have to use local/yourejbname i guess give it a try
    good luck

    parthi
  6. Hi Parthi,
    Thanks for the response. Request you to have a look at my last posting, where the issue is with ejb-jar.xml and jboss.xml. I am not linking my local BMP bean with Session bean, but is working fine. I am looking forward for any limitation to the approach in terms of persormance or roburstness.

    BTW when I deploy my EJB and have a look at JBOSS console I can get to know the name to which it has been bound. If I remove the defination of CMP from ejb-jar it will not deply my CMP even if I link it to Session bean using local-ref
    Thanks,
    Gautham
  7. Your fix is correct[ Go to top ]

    Your approach/fix is correct.
    Every bean (session or entity) you create needs to be mentioned in the ejb-jar.xml. So, while you had only Trading bean inside <session> ... </session>, you were not having Customer within <entity>... </entity>. This is the minimum you need to do to tell the container about what beans are present in your jar. This you will need to include, even if there was no reference from Trading to Customer - I guess that is easy to understand. References are then captured under <ejb-ref> ... </ejb-ref> sections but that section is not enough to define the bean for the container.

    Similar arguments for jboss.xml

    Hope that helps!
    Nitesh