Get started with beans: Classic EJB example code

Source: Internet
Author: User

Example of RMI-iiopejb Client
  
A Java RMI-IIOP client with a proprietary EJB server.
  
Package com. Wiley. compbooks. Roman. CORBA. helloworld;
Import javax. EJB .*;
Import javax. Naming .*;
Import javax. RMI .*;
Import java. util. properties;
Import javax. transaction. usertransaction;
/**
* This class is an example of client code that invokes
* Methods on a simple stateless Session Bean.
*/
Public class rmiclient {
Public static void main (string [] ARGs ){
Try {
/*
* Get System Properties for JNDI Initialization
*/
Properties props = system. getproperties ();
/*
* Use JNDI to look up the home object
*/
Context CTX = new initialcontext (props );
Hellohome home = (hellohome)
Javax. RMI. portableremoteobject. Narrow (
CTX. Lookup ("hellohome "),
Hellohome. Class );
/*
* Use JNDI to look up the JTA
* Usertransaction Interface

Java heaven

*/
Usertransaction usertran = (usertransaction)
CTX. Lookup ("javax. transaction. usertransaction ");
/*
* Start the transaction
*/
Usertran. Begin ();
/*
* Use the home object to create the hello EJB object
*/
Hello = home. Create ();
/*
* Call the hello () method, and print it
*/
System. Out. println (hello. hello ());
/*
* Done with EJB object, so remove it
*/
Hello. Remove ();
/*
* Commit the transaction
*/
Usertran. Commit ();
} Catch (exception e ){
E. printstacktrace ();
}
}
}
Example RMI-IIOP EJB Client
  
  
A corba client with a CORBA-based EJB server.
Package com. Wiley. compbooks. Roman. CORBA. helloworld;
Import java. util .*;
Import org. Omg. cosnaming .*;
Import org. Omg. costransactions .*;
Public class corbaclient {
Public static void main (string [] ARGs) throws exception {
/*
* Initialize the orb.
*
* A more portable way to do this is:
*
* Properties P = new properties ();
* P. Put ("org. Omg. CORBA. orbclass", <... orb class...> );
* Org. Omg. CORBA. Orb orb = org. Omg. CORBA. Orb. INIT (ARGs, P );
*/
Org. Omg. CORBA. Orb orb = com. Inprise. EJB. Global. Orb ();
/*
* Get a reference to a naming context
*/
Namingcontext context = namingcontexthelper. Narrow
(ORB. resolve_initial_references ("nameservice "));
/*
* Look up the home object using cos naming
*/
Namecomponent [] names = {New namecomponent ("hellohome ","")};
Hellohome = hellohomehelper. Narrow
(Context. Resolve (names ));
/*
* Get the corba ots current interface
* Controlling transactions
*/
Current currenttx = currenthelper. Narrow
(ORB. resolve_initial_references ("transactioncurrent "));
/*
* Begin the transaction

Java heaven

*/
Currenttx. Begin ();
/*
* Use the home object to create an EJB object
*/
Hello = hellohome. Create ();
/*
* Call a Business Method
*/
System. Out. println (hello. hello ());
/*
* Remove the EJB object
*/
Hello. Remove ();
/*
* Commit the transaction
*/
Currenttx. Commit (true );
}
}

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.