Several scenarios for EJB invocation in GlassFish

Source: Internet
Author: User
Tags glassfish
The most commonly used invocation of EJBS deployed on a server is in a servlet or JSP in the same application. But there are other scenarios, such as EJB clients that run separately, and calls across application servers, and so on. In the documentation, the methods of these calls are not clearly stated. The following is an example of GlassFish to illustrate several different invocation methods for EJBS.

I. EJB invocation in the same application

The EJB invocation in the same ear application is the most common invocation of the EJB. Both EJB and servlet are included in the same ear. The invocation of EJBS in the servlet is simpler. And when developing EJBS, you can use the EJB's local interface to improve performance. The following is a code snippet for a EJB3.0 call:

@EJB
Private Newsessionremote Newsessionbean;

protected void ProcessRequest (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.setcontenttype ("Text/html;charset=utf-8");
PrintWriter out = Response.getwriter ();
Out.println ("Out.println ("Out.println ("<title>servlet myservlet</title>");
Out.println ("Out.println ("<body>");
Out.println ("Out.println ("</body>");
Out.println ("Out.close ();
}two. EJB calls with different applications in the same GlassFish instance

Multiple applications can be deployed on the same GlassFish instance (the same JVM), where the EJB is deployed in application A, and the servlet in application B can invoke the EJB in application A. In this case, the servlet's calling code is exactly the same as the above, with some differences: EJB calls must go through the remote interface. The remote interface class of the EJB must be placed under the local classpath (web-inf/lib jar file or web-inf/classes) three. Client calls in the application container (ACC)

Through the ACC to make the client call, in the GlassFish species have a detailed description, see (Http://docs.sun.com/app/docs/doc/819-3659/6n5s6m5a6?q=java+ee+5&a=view )。 four. Independent client Invoke EJB

If you are invoking an EJB on a stand-alone Java client, you must do the following: The EJB call must go through the remote interface. The remote interface class of the EJB must be placed under the classpath under the class path and include several other jar files in the GlassFish lib directory. Include Javaee.jar,appserv-rt.jar,appserv-ext.jar and appserv-deploy-client.jar the following parameters need to be included in the Java command, for example:

Java-dorg.omg.corba.orbinitialhost=com.acme.host1 (Specify glassfish hostname or IP address, default is localhost)
- dorg.omg.corba.orbinitialport=9876 (Specifies the IIOP port that the EJB listens to, the default is 3700)

The

EJB lookup and invocation that is displayed in code. For example,
.....
public static void Main (string[] args) {
        try {
             initialcontext IC =new initialcontext ();
            newsessionremote SayHello = ( Newsessionremote) ic.lookup ("MyPackage"). Newsessionremote ");
            String result = Sayhello.sayhello ("Dad");
            System.out.println (result);
       } catch (Namingexception ex) {
             Ex.printstacktrace ();
       }       
    }
... five. Calling (Tomcat)

in another Web container

The servlet and JSP in other web containers can also make remote calls to the EJB on the GlassFish, which, like the "independent client on EJB invocation" described in (iv), makes a difference in the code invoked. It is in these web containers that usually have their own implementation methods and parameter settings for the Jdni container, you need to force these settings to successfully invoke the EJB in GlassFish, the solution is to add the following code:

Properties Props = new properties ();
Props.setproperty ("Java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
Props.setproperty ("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
Props.setproperty ("Java.naming.factory.state", "Com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
Props.setproperty ("Org.omg.CORBA.ORBInitialHost", "puma.prc.sun.com");
Props.setproperty ("Org.omg.CORBA.ORBInitialPort", "6262");

InitialContext IC = new InitialContext (props);

Of course, the remote interface of the EJB and the jar files associated with the application server with the GlassFish will also be placed in the appropriate classpath. Six. Call between different GlassFish containers

If the application of the caller and the callee is on the GlassFish in the scenario of (v), the invocation process can be greatly simplified. The remote EJB can be referenced in the invoked Web application through the technology of "CORBA interop", and no modification is required in the code, as in the case of an EJB on the same GlassFish instance . For example, in the caller's servlet:

Package Wangyu;

Import java.io.*;
Import java.net.*;
Import Javax.ejb.EJB;

Import javax.servlet.*;
Import javax.servlet.http.*;
Import MyPackage. Newsessionremote;

/**
*
* @author Wangyu
* @version
*/
public class Callremote extends HttpServlet {

@EJB
Private Newsessionremote Newsessionbean;

protected void ProcessRequest (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.setcontenttype ("Text/html;charset=utf-8");
PrintWriter out = Response.getwriter ();

Out.println ("Out.println ("Out.println ("<title>servlet callremote</title>");
Out.println ("Out.println ("<body>");
Out.println ("Out.println ("</body>");
Out.println ("Out.close ();
}


protected void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
ProcessRequest (request, response);
}


protected void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
ProcessRequest (request, response);
}

/** Returns A short description of the servlet.
*/
Public String Getservletinfo () {
Return "Short description";
}
</editor-fold>
}

The code is exactly the same as in (a). However, in Sun-web.xml, you need to add the EJB reference configuration:

<ejb-ref>
<ejb-ref-name>wangyu. Callremote/newsessionbean</ejb-ref-name>
<jndi-name>corbaname:iiop:puma.prc.sun.com:3700#mypackage.NewSessionRemote</jndi-name>
</ejb-ref>

An EJB in the name of Jndi that points to another GlassFish instance (possibly on another machine).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.