这里存在各种各样其它的小问题,可能需要某些调整和客户代码的再编译,但它们的工作量都不大。 在下面的样本中,企业JavaBeans 客户程序演示了如何定位一个企业JavaBean
并启动它的远程方法。
DemoClient.java (源代码)
/**
* DemoClient -- demonstrates using a minimal
* Java application to talk to the DemoBean
* stateless session bean
*/
package ejb.demo;
import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import java.util.Properties;
import examples.ejb.statelessSession.interfaces.*;
/**
* DemoClient demonstrates using a minimal stateless session
* bean.
* Remember view session beans as an extension of your client
* running in the server.
*/
public class demoClient {
public static void main(String[] args) {
System.out.println("
Begin demoClient...
");
parseArgs(args);
try {
// Create A DemoBean object, in the server
// Note: the name of the class corresponds to the JNDI
// property declared in the DeploymentDescriptor
// From DeploymentDescriptor ...
// beanHomeName demo.DemoHome
Context ctx = getInitialContext();
DemoHome dhome = (DemoHome) ctx.lookup("demo.DemoHome");
// Now you have a reference to the DemoHome object factory
// use it to ask the container to creat an instance of
// the Demo bean
System.out.println("Creating Demo
");
Demo demo = dhome.create();
// Here is the call that executes the method on the
// server side object
System.out.println("The result is "
+ demo.demoSelect();
}
catch (Exception e) {
System.out.println(" = Error <=");
e.printStackTrace();
}
System.out.println("
End demoClient...
");
}
static void parseArgs(String args[]) {
if ((args == null) || (args.length == 0))
return;
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-url"))
Integer.parseInt(args[++i]);
else if (args[i].equals("-user"))
user = args[++i];
else if (args[i].equals("-password"))
password = args[++i];
}
}
static String user = null;
static String password = null;
static String url = "t3://localhost:7001";
/**
* Gets an initial context.
*
* @return Context
* @exception java.lang.Exception if there is
* an error in getting a Context
*/
static public Context getInitialContext() throws Exception {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.T3InitialContextFactory");
p.put(Context.PROVIDER_URL, url);
if (user != null) {
System.out.println ("user: " + user);
p.put(Context.SECURITY_PRINCIPAL, user);
if (password == null)
password = "";
p.put(Context.SECURITY_CREDENTIALS, password);
}
return new InitialContext(p);
}
}
步骤 8: 编译并运行客户程序(Client)
所余下来要作的就是编译并运行 Client 程序以确认安装在服务器上的企业 JavaBeans 的功能是否正确。
1). 编译客户程序
编译和运行客户程序对所有的平台是等同的,就象运行客户程序的结果对所有平台也应该是等同的一样。
javac ejb/Demo/democlient.java
2). 运行客户程序
"Hello World" 串出自企业JavaBeans的 DemoSelect() 方法。运行客户程序应给出下列消息:
Java ejb.Demo.democlient
Begin DemoClient...
Creating Demo
The result is Hello World
End DemoClient...
4. 小结
你现在有了可以运行的 DemoBean 例子,并且你还成功地运行了客户程序。
建立你的第一个无状态会话Bean--HelloWorld之一
建立你的第一个无状态会话Bean--HelloWorld之二
建立你的第一个无状态会话Bean--HelloWorld之三
收藏到ViVi 收藏此页到365Key
上一篇:优化Entity Beans的七条守则
下一篇:建立你的第一个无状态会话Bean--HelloWorld之二