📄 client.java
字号:
//定义本类在包examples.ejb.extensions.readMostly中
package examples.ejb.extensions.readMostly;
//本类用到的其他类
import java.rmi.RemoteException;
import java.util.Properties;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
/**
* 这个类演示怎样使用一个只读EJB和一个可读写EJB提供访问一组数据的"read-mostly"模式。
*
*/
public final class Client {
private final static boolean verbose = true;
private String url;
//声明主接口
private StockHome readerHome;
private StockWriterHome writerHome;
//构造方法
public Client(String url)
throws NamingException
{
this.url = url;
//查找主接口
readerHome = (StockHome)
narrow(lookupHome("readMostly.StockHome"), StockHome.class);
writerHome = (StockWriterHome)
narrow(lookupHome("readMostly.StockWriterHome"), StockWriterHome.class);
}
//主方法
public static void main(String[] args) {
System.out.println("\nBeginning readMostly.Client...\n");
//服务器url
String url = "t3://localhost:7001";
// Parse the argument list
if (args.length != 1) {
//打印用法提示
System.out.println("用法: java examples.ejb.extensions.readMostly.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 Beans.");
log("This indicates that there was a problem communicating with the server: "+e);
}
System.out.println("\nEnd readMostly.Client...\n");
}
//本类实例
public void example() throws CreateException, FinderException, RemoteException, RemoveException {
String stock = "BEAS";
log("Creating a StockWriter for " + stock);
//创建远程对象StockWriter
StockWriter writer = (StockWriter)
narrow(writerHome.create(stock, 100, 1000), StockWriter.class);
log("Looking up Stock info for " + stock);
//创建远程对象Stock
Stock reader = (Stock)
narrow(readerHome.findByPrimaryKey(stock), Stock.class);
//调用远程方法
log(reader.getSymbol() + ":");
log("\tPrice: " + reader.getPrice());
log("\tVolume: " + reader.getVolume());
log("\t52 Week High: " + reader.get52weekHigh());
log("\t52 Week Low: " + reader.get52weekLow());
log("Removing the StockWriter");
writer.remove();
}
/**
* RMI/IIOP 客户端应该用PortableRemoteObject.narrow()方法
*/
private Object narrow(Object ref, Class c) {
return PortableRemoteObject.narrow(ref, c);
}
//查找远程对象
private Object lookupHome(String homeName)
throws NamingException
{
// 使用JNDI查找主接口
Context ctx = getInitialContext();
try {
return ctx.lookup(homeName);
} 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 "+homeName+" 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 + -