📄 client.java
字号:
//定义本类在包examples.ejb.basic.statefulSession中
package examples.ejb.basic.statefulSession;
//本类用到的其他类
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;
/**
* 这个类演示了如何调用一个会话,并进行如下操作:
* 创建一个Trader远程对象
* 使用Trader远程对象购买一定数量的股票
* 出售一定数量的股票
* 清除Trader远程对象
*/
public class Client {
//定义JNDI服务中EJB的名称
private static final String JNDI_NAME = "statefulSession.TraderHome";
//声明应用服务器的URL变量
private String url;
//声明主接口变量
private TraderHome home;
public Client(String url)
throws NamingException
{
this.url = url;
//寻找主接口
home = lookupHome();
}
/**
* 在命令行运行这个实例:
* java examples.ejb.basic.statefulSession.Client "t3://localhost:7001"
* 参数是可选的
*/
public static void main(String[] args) {
log("\nBeginning statefulSession.Client...\n");
//声明应用服务器地址变量
String url = "t3://localhost:7001";
// Parse the argument list
if (args.length != 1) {
//打印用法提示
System.out.println("Usage: java examples.ejb.basic.statefulSession.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 statefulSession.Client...\n");
}
/**
* 运行实例
*/
public void example()
throws CreateException, ProcessingErrorException, RemoteException,
RemoveException
{
//声明顾客名字变量
String customerName = "Matt";
// 创建远程对象
log("创建 trader\n");
Trader trader = (Trader) narrow(home.create(), Trader.class);
// 出售一些股票
//股票名
String stockName = "MSFT";
//股票数量
int numberOfShares = 200;
log("Selling " + numberOfShares + " of " + stockName);
//调用远程方法sell
TradeResult tr = trader.sell(customerName, stockName, numberOfShares);
log(tr.toString());
// 买进一些股票
//股票名
stockName = "BEAS";
//股票数量
numberOfShares = 250;
log("Buying " + numberOfShares + " of " + stockName);
//调用远程方法buy
tr = trader.buy(customerName, stockName, numberOfShares);
log(tr.toString());
// Get change in Cash Account from EJBean
log("Change in Cash Account: $" + trader.getBalance()
+ "\n");
log("Removing trader\n");
//清除远程对象
trader.remove();
}
/**
* 验证远程对象的类型
*/
private Object narrow(Object ref, Class c) {
return PortableRemoteObject.narrow(ref, c);
}
/**
* 从JNDI树中寻找EJB主接口
*/
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;
}
}
/**
* 使用属性对象获取JNDI服务上下文
* clients
*/
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;
}
}
/**
* 这是获取上下文的第二个版本,依赖一个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 + -