📄 client.java
字号:
//声明本类定义在包examples.ejb.subclass中
package examples.ejb.subclass;
//声明本类引入的其它类或包
import java.rmi.RemoteException;
import java.util.Properties;
import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
/**
* 这个类演示怎样使用一个EJBean,它是另一个bean的子类:
* 创建ChildBean的一个实例
* 调用ChildBean重载的方法
* 调用继承ParentBean的方法
* 调用ChildBean中的方法
* 清除ChildBean实例
*
*/
public class Client {
//声明jndi名
private static final String JNDI_NAME = "SubClass.ChildHome";
//声明服务器url
private String url;
//声明主接口
private ChildHome home;
//构造方法
public Client(String url)
throws NamingException
{
this.url = url;
//查找主接口
home = lookupHome();
}
/**
* 在命令行运行本类,如:
* java examples.ejb.subclass.Client "t3://localhost:7001"
*
* @参数 url URL 例如: "t3://localhost:7001"
*/
public static void main(String[] args) {
System.out.println("\nBegin subclass.Client...\n");
String url = "t3://localhost:7001";
// 解析命令行参数
if (args.length != 1) {
System.out.println("Usage: java examples.ejb.subclass.Client t3://hostname:port");
return;
} else {
url = args[0];
}
Client client = null;
try {
//本类实例
client = new Client(url);
} catch (NamingException ne) {
System.exit(1);
}
try {
//本例方法
client.example();
} catch (Exception e) {
log("There was an exception while creating and using the ChildBean.");
log("This indicates that there was a problem communicating with the server: "+e);
}
System.out.println("\nEnd subclass.Client...\n");
}
/**
* 运行这例程
*/
public void example()
throws CreateException, RemoteException, RemoveException
{
System.out.println("Creating ChildBean...\n");
//创建远程对象
Child child = (Child) narrow(home.create(), Child.class);
log("Calling method overloaded by ChildBean class");
//调用远程方法,重载的方法
String message = child.sayHello();
log(message);
log("Calling method inherited by ChildBean class");
//调用远程方法,继承的方法
message = child.nonOverloadedMethod();
log(message);
log("Calling method that only exists in ChildBean class");
//调用远程方法,子类定义的方法
message = child.childOnlyMethod();
log(message);
System.out.println("Removing ChildBean...\n");
child.remove();
}
/**
* RMI/IIOP 客户端必须使用PortableRemoteObject.narrow()方法
*/
private Object narrow(Object ref, Class c) {
return PortableRemoteObject.narrow(ref, c);
}
//查找主接口
private ChildHome lookupHome()
throws NamingException
{
// 使用JNDI查找beans
Context ctx = getInitialContext();
try {
//查找主接口对象
Object home = ctx.lookup(JNDI_NAME);
return (ChildHome) narrow(home, ChildHome.class);
} catch (NamingException ne) {
log("The client was unable to lookup the EJBHome. Please make sure ");
log("that you have deployed the ejb with the JNDI name "+JNDI_NAME+" on the WebLogic server at "+url);
throw ne;
}
}
/**
* 另一个版本,必须使用属性文件jndi.properties定义了
* INITIAL_CONTEXT_FACTORY和PROVIDER_URL属性。
*/
// private Context getInitialContext() throws NamingException {
// return new InitialContext();
// }
/**
* 使用属性对象,创建上下文
*/
private Context getInitialContext() throws NamingException {
try {
// 获取初始化上下文
Properties h = new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, url);
return new InitialContext(h);
} catch (NamingException ne) {
log("We were unable to get a connection to the WebLogic server at "+url);
log("Please make sure that the server is running.");
throw ne;
}
}
private static void log(String s) {
System.out.println(s);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -