• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Local Interfaces

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am following the examples in the Head First EJB book regarding local interfaces. I have the Local Home interface, the Local interface, the Bean, and the Local Client. My problem is that I can only deploy this application if I put all four files in the same jar. Otherwise, the application will not deploy. And if the four files are in the same jar, then the question arises "How do I run the client?" What should I do? I am listing the files, and the app server that I am using is Sun's App Server 8 Q2:

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

public interface AdviceLocal extends EJBLocalObject {

public String getAdvice();

}

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

public interface AdviceHomeLocal extends EJBLocalHome {

public AdviceLocal create() throws CreateException;

}

import javax.ejb.*;

public class AdviceBean implements SessionBean {

private String[] adviceStrings = {"Read a Book","Listen to some music"};

public void ejbActivate(){}

public void ejbPassivate(){}

public void ejbRemove(){}

public void setSessionContext(SessionContext ctx){}

public void ejbCreate(){}

public String getAdvice() {

System.out.println("Ready for advice?");
int random = (int) (Math.random() * adviceStrings.length);
return adviceStrings[random];

}
}

import javax.naming.*;
import javax.ejb.*;

public class AdviceLocalClient {

public static void main(String[] args) {

new AdviceLocalClient().go();

}

public void go() {

Object objref = null;

try {

Context initial = new InitialContext();
objref = initial.lookup("java:comp/env/ejb/Advisor");

} catch (NamingException ex) {

ex.printStackTrace();

}

AdviceHomeLocal home = (AdviceHomeLocal) objref;
AdviceLocal advisor = null;

try {

advisor = home.create();

} catch (CreateException ex) {

ex.printStackTrace();

}

System.out.println(advisor.getAdvice());


}
}
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

The jar that is deployed to the container should contain the following :

Component interface
Home interface
Bean class

Your interfaces should be remote to cater for remote clients (your client app or beans in another contain in a cluster setup) not running in the same VM (i.e. other beans or a web front). You shouldnt have the client class in here as there is no way for it to ever be executed.


The client jar should NOT contain your bean class but should contain your
Component interface and
Home interface.

To reach the deployed bean you need to do a JNDI lookup to find the bean - the whole PortableRemoteObject.narrow(context.lookup(........ thing.

You tell the code where to do the lookup by specifying properties in the InitialContext, either by defining the properties on the context or by a separate properties file that is found on the classpath - these properties are set according to the container you use and the they should be somewhere in the documentation.

Hope this helps,
Chris
[ November 16, 2005: Message edited by: Chris Brat ]
 
sid ansari
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chris,

You are telling me how to deploy with remote interfaces. I know how to do that. I was wondering if someone could show me how to deploy local interfaces with a local client. I don't see why I have to do a PortableRemoteObject... lookup with a local client. I am not using IIOP so I don't see why I have to do that. I just need to know how to get the local client to reference the bean when I have local interfaces.

The question arises: can I even do that or is there a serious mistake in the Head First EJB book, because the example is taken staright from the book. How do I contact the authors?

Regards

Sid
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sid,


You are telling me how to deploy with remote interfaces. I know how to do that. I was wondering if someone could show me how to deploy local interfaces with a local client. I don't see why I have to do a PortableRemoteObject... lookup with a local client. I am not using IIOP so I don't see why I have to do that. I just need to know how to get the local client to reference the bean when I have local interfaces.


In order for your client to access the EJBs via their local interfaces, couple of conditions needs to be met:
  • The client and the EJB must run within the same JVM.
  • The client and the server classes must be loaded by the same classloader (check your container�s classloading architecture)


  • For all J2EE compatible containers this basically means that your clients need to be other server-side components (like JSPs, Servlets, Struts action classes, etc) and they should be packed together as an ear. Otherwise you have no choice but to use remote clients.
    Regards.
     
    sid ansari
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Rene,

    So let me see if I understand what you are saying. You are saying that I cannot use a stand alone local client running in the same VM as the bean with local interfaces to reference the bean

    I think that if this is true then the authors of Head First EJB should take the example that I have posted out of their book because it is extremely misleading. How do I contact the authors?

    Sid
     
    Chris Brat
    Ranch Hand
    Posts: 108
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Sid,

    Sorry that for not answering your question fully - I got focused on one part and missed the plot.

    To access a bean with local interfaces it you need the jar deployed to the server to contain the

    Local home interface,
    Local component interface
    and the bean class.

    The client of this bean will then have to run in the same VM either as another bean or as a JSP or servlet.

    To access it you still do the lookup :

    MyHome someHome = (MyHome)ctx.lookup(......)
    MyComp comp = someHome.create();
    comp.method1()

    In reality I dont think you would ever have a client app, as it is written in your example, accessing your beans besides possibly to test it.

    Chris
     
    sid ansari
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Chris,

    Thanks for your help. I will try out what you suggest.

    Thanks again

    Sid
     
    sid ansari
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Chris,

    I have placed AdviceLocal, AdviceHomeLocal and the bean in one jar file. I have also written a servlet and html as follows:

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.naming.*;
    import javax.rmi.*;
    import javax.ejb.*;


    public class AdviceServlet extends HttpServlet {

    AdviceLocal advisor = null;
    Object objref = null;

    public void init(ServletConfig config) throws ServletException {

    try {

    InitialContext ic = new InitialContext();
    Object objref = ic.lookup("java:comp/env/ejb/Advisor");

    } catch (NamingException ex) {
    ex.printStackTrace();
    }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    AdviceHomeLocal home = (AdviceHomeLocal)objref;

    try {

    advisor = home.create();

    } catch (CreateException ex) {

    ex.printStackTrace();

    }

    System.out.println(advisor.getAdvice());


    }


    public void destroy() {

    System.out.println("AdviceServlet: destroy()");
    }
    }


    the html is:

    <html>
    <head>
    <title>Advice</title>
    </head>

    <body bgcolor="white">
    <h1><b><center>Converter</center></b></h1>
    <hr>
    <p>Click on Button for Advise:</p>
    <form action="http://localhost:8080/advice/stateful" method="get">
    <br>
    <p>
    <input type="submit" value="Submit">
    </form>

    </body>
    </html>

    How do I deploy these two. How do I make sure that the servlet is in the same VM as the beans and the interfaces. I have the app server on my laptop.

    Thanks in advance if you can help.

    Sid
     
    Chris Brat
    Ranch Hand
    Posts: 108
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Sid,

    I package all web related source (JSP, js, html etc) into a war, ejb source into their own ejb jar and then other lib code into their respective jars.

    These are all then packaged into an ear which I deploy to the server.
    On Weblogic 8.1 you have a nice GUI with which you can browse for the ear and deploy it; On JBOSS you can simply copy the ear into the server's deploy directory and it is immediately deployed - sorry these are the only two ejb containers I've worked with so i cant give you information on yours.

    For ear assembly and deployment details check out these sites :

    http://docs.sun.com/source/817-6087/dgdeploy.html and
    http://developers.sun.com/prodtech/javatools/jsenterprise/reference/docs/j2eeapps.pdf

    I know the pdf is specifically title for Sun One but this is a quote from the book and explains why I think it will help you -

    "This book is intended for anyone who uses the Sun ONE Studio 5 IDE to assemble, deploy, or execute J2EE applications. The first chapter summarizes the J2EE platform concepts of assembly and deployment, and it should benefit anyone seeking a general understanding of assembly and deployment."

    Cheers
    Chris
     
    sid ansari
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Chris,

    Thanks for your help. I guess I didn't make myself clear when I asked for help on deploying this application. I know how to deploy web applications and beans. My problem is that when I put the local interfaces and bean in a jar, and the servlet and html in a war and then deploy them in an application, I get a Null Pointer error. Could you try them and see if you get the same error. I suspect it has to do with the fact that the servlet cannot get a reference to the EJBLocalObject.

    Thanks again for your help

    Sid
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic