Beans入門必讀之經典EJB例子代碼

來源:互聯網
上載者:User

rmi-iiopejb用戶端例子
  
  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天堂

  */
  UserTransaction userTran = (UserTransaction)
  ctx.lookup("javax.transaction.UserTransaction");
  /*
  * Start the transaction
  */
  userTran.begin();
  /*
  * Use the home object to create the Hello EJB Object
  */
  Hello 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 helloHome = HelloHomeHelper.narrow
  (context.resolve(names));
  /*
  * Get the CORBA OTS Current interface for
  * controlling transactions
  */
  Current currentTX = CurrentHelper.narrow
  (orb.resolve_initial_references("TransactionCurrent"));
  /*
  * Begin the transaction

JAVA天堂

  */
  currentTX.begin();
  /*
  * Use the home object to create an EJB object
  */
  Hello hello = helloHome.create();
  /*
  * Call a business method
  */
  System.out.println(hello.hello());
  /*
  * Remove the EJB object
  */
  hello.remove();
  /*
  * Commit the transaction
  */
  currentTX.commit(true);
  }
  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.