答案: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 Inte易做图ce (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 inte易做图ce 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 inte易做图ce, which is used to demarcate transactional boundaries, which is 易做图ogous 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.
上一个:一道方法参数相关题
下一个:运行你的第一个 J2ME 程序 原创