-Call ejb3 by EJB Client

Source: Internet
Author: User
When ejb3 is deploy to JBoss, The EJB client calls ejb3 in two cases: 1) Released on JBoss Of Web app As Client To call EJB EJB and Web applications that call ejbs are both released in the JBoss integrated environment. To publish a web application under JBoss, You need to package the web application into a war file. In addition In this environmentEJBYou do not needEJBIn/WEB-INF/classes/DirectoryOtherwise, a type conflict occurs when stateful bean is called, Raise the following exceptions Java. Lang. classcastexception: $ proxy84 Org. apache. JSP. statefulbeantest_jsp. _ jspservice (Org. apache. JSP. statefulbeantest_jsp: 55) If EJB and the web application that calls EJB are both released in the JBoss integrated environment, therefore, the local or remote interfaces of EJB can be called and released on the JBoss client. You do not need to explicitly set the context of the JNDI access. You can directly use initialcontext CTX = new initialcontext () after obtaining the Context Environment, the container automatically assigns the correct environment to initialcontext. For example: Initialcontext CTX = New Initialcontext (); // The client and JBoss run in the same JVM and do not need to input props Helloworld = (helloworld) CTX. Lookup ( "Helloworldbean/remote" ); If Initialcontext Setting the access attribute will lead to problems that cannot be transplanted. Because your application may be deployed on WebLogic and other application servers. ( This tutorial takes into account that some students may need J2se Calling EJB For the convenience of teaching, the access attribute is set In this way JBoss , J2se Or independent Tomcat , Can obtain the correct Initialcontext ) 2) In a separate Tomcat Or J2se (For example JUnit Test ). EJB In a formal production environment, most clients that call EJB may be independent tomcat or resin. The following describes how to call EJB on a single Tomcat server. In a separate Tomcat Server call EJB Follow these steps: : A) Call EJB Dependent Jar Copy package Tomcat Under /Shared/lib Directory or Web Application WEB-INF/lib Lower , Dependent Jar In JBoss Installation Directory Client Directory . You can set a custom library "ejb3 library" in eclipse and add these jars to the library, then add "ejb3 library" to classpath in your EJB Project B) Copy the EJB interface to the/WEB-INF/classes/directory of the application. C) The initialcontext environment attribute must be explicitly set when the client accesses EJB, Code As follows: Properties props = New Properties (); Props. setproperty ( "Java. Naming. Factory. Initial" , "Org. jnp. Interfaces. namingcontextfactory" ); Props. setproperty ( "Java. Naming. provider. url" , "Localhost: 1099" ); Props. setproperty ( "Java. Naming. Factory. url. pkgs" , "Org. JBoss. Naming" ); Initialcontext CTX = New Initialcontext (props ); // The client and JBoss run in the same JVM and do not need to input props Helloworld = (helloworld) CTX. Lookup ( "Helloworldbean/remote" ); Tip: In addition to setting environment attributes through hard encoding, you can also Classpath Place one JNDI. Properties File Note: it cannot be called in a separate Tomcat and j2se. EJB Of Local Interface, because it corresponds JBoss Not the same VM . J2se Calling Ejb3 You also need Jar And EJB The interface is placed under the application class path.

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.