• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

Corba客户端调用EJB的例子来了,呵呵

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 17次 | 进入软件测试论坛讨论

领测软件测试网 1. 概述
Understanding How to Call Beans
We now take a look at the other half of the world--the client side. We are now customers of the beans’ business logic, and we are trying to solve some real-world problem by using one or more beans together.  There are two different kinds of clients:
Java RMI-IIOP based clients. These clients use the Java Naming and Directory Interface (JNDI) to look up objects over a.network, and they use the Java Transaction API (JTA) to control transactions.
CORBA clients. Clients can also be written to the CORBA standard. This would primarily be useful if you want to call your EJB components using another language, such as C++.  CORBA clients use the CORBA Naming Service (COS Naming) to look up objects over the network, and they use the CORBA’s Object Transaction Service (OTS) to control transactions.
Whether you’re using CORBA or RMI-IIOP, your client code typically looks like this:
1.    Look up a home object.
2.    Use the home object to create an EJB object.
3.    Call business methods on the EJB object.
4.    Remove the EJB object.
Let’s go through each of these steps with RMI-IIOP based clients.


2.例子
Now let’s write some sample code to illustrate how to use CORBA to call an EJB component.  We@#ll use the "Hello, World" bean developed in Chapter X.  The key thing to notice is that we are taking a bean that we called using RMI-IIOP in Chapter X, and are reusing the bean without modification and accessing it from a CORBA client.
We’ll use the following to access our bean:
•    COS Naming to look up the home object
•    OTS to demarcate transaction boundaries
•    The Java language to write our CORBA client
Source X.X shows the implementation.
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.
     */
    Properties p = new Properties();
    p.put("org.omg.CORBA.ORBClass", <..Your ORB class..>);
    org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, p);

    /*
     * 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);
  }
}
Source X.X  Example CORBA EJB client.
As you can see, CORBA clients are a bit more complex than RMI-IIOP clients. We first need to initialize the ORB before beginning any CORBA operations. Next, we get a reference to a naming context via COS Naming, which we use to look up home objects. Once we’ve retrieved the home object, calling methods on enterprise beans is syntactically similar to RMI-IIOP. We also get a reference to the OTS Current interface, which is used to demarcate transactional boundaries, which is analogous to the Java Transaction API (JTA) described in Chapter X. The begin() and commit() calls have the same semantic meaning as their JTA equivalents.  See the book@#s accompanying source code for vendor-specific build scripts for this code.

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网