📄 client.java
字号:
//声明本接口所在的包
package examples.rmi_iiop.ejb.rmi_iiop;
//声明本类引入的其他类
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;
/**
* 这个客户端程序演示怎样调用一个无状态会话Bean,包括如下内容:
*
* 创建一个远程对象Trader
* 使用Trader买进一定数量的股票
* 使用Trader卖出一定数量的股票
* 删除Trader
*
*/
public class Client {
//声明EJB的JNDI名
private static final String JNDI_NAME = "rmi_iiop/TraderHome";
//服务器地址
private String url;
//主接口
private TraderHome home;
//构造方法
public Client(String url)
throws NamingException
{
this.url = url;
//查找主接口
home = lookupHome();
}
/**
* 在命令行运行这个例程:
*
* java examples.rmi_iiop.ejb.rmi_iiop.Client "iiop://localhost:7001"
*
* @参数 url URL such as "iiop://localhost:7001" of Server
*/
public static void main(String[] args) throws Exception {
//程序开始
log("\nBeginning statelessSession.Client...\n");
String url = "iiop://localhost:7001";
// 解析命令行参数
if (args.length != 1) {
//打印用法提示:
System.out.println("Usage: java examples.rmi_iiop.ejb.rmi_iiop.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 Trader.");
log("This indicates that there was a problem communicating with the server: "+e);
}
log("\nEnd statelessSession.Client...\n");
}
/**
* 运行这个实例
*/
public void example()
throws CreateException, RemoteException, RemoveException
{
// 创建Trader
log("Creating a trader");
Trader trader = (Trader) narrow(home.create(), Trader.class);
String [] stocks = {"BEAS", "MSFT", "AMZN", "HWP" };
// 执行买进操作
for (int i=0; i<stocks.length; i++) {
int shares = (i+1) * 100;
log("Buying "+shares+" shares of "+stocks[i]+".");
trader.buy(stocks[i], shares);
}
// 执行卖出操作
for (int i=0; i<stocks.length; i++) {
int shares = (i+1) * 100;
log("Selling "+shares+" shares of "+stocks[i]+".");
trader.sell(stocks[i], shares);
}
// 清除Trader
log("Removing the trader");
trader.remove();
}
/**
* RMI/IIOP客户端应该使用这个narrow方法
*/
private Object narrow(Object ref, Class c) {
return PortableRemoteObject.narrow(ref, c);
}
/**
* 在JNDI树中查找EJBs主接口
*/
private TraderHome lookupHome()
throws NamingException
{
// 查找JNDI上下文
Context ctx = getInitialContext();
try {
//查找主接口实现类
Object home = ctx.lookup(JNDI_NAME);
return (TraderHome) narrow(home, TraderHome.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;
}
}
/**
* 使用一个属性对象,创建上下文
*/
private Context getInitialContext() throws NamingException {
try {
// 声明属性对象
Properties h = new Properties();
// 设置属性
h.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.cosnaming.CNCtxFactory");
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;
}
}
/**
* 也可以这样实现。
* 这种实现方式依赖属性文件jndi.properties
*
*/
// private static Context getInitialContext()
// throws NamingException
// {
// return new InitialContext();
// }
private static void log(String s) {
System.out.println(s);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -