科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道Beans入门必读之经典EJB例子代码

Beans入门必读之经典EJB例子代码

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

rmi-iiop ejb客户端例子      A Java RMI-IIOP client with a proprietary EJB server.

作者:中国IT实验室 来源:中国IT实验室 2007年9月7日

关键字: Bean

  • 评论
  • 分享微博
  • 分享邮件
  rmi-iiop ejb客户端例子
  
  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
  */
  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
  */
  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);
  }
  }

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章