📄 client.java
字号:
//定义本类在包examples.ejb20.homeMethods 中
package examples.ejb20.homeMethods;
//本接口用到的其他类
import java.rmi.RemoteException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.ObjectNotFoundException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
/**
* 这个类演示了如何调用一个实体EJB。
* 这个例子同时演示了怎样在JNDI树查找EJB主接口。
*/
public class Client {
//声明变量
private String m_url;
private AccountHome m_home;
//构造方法
public Client(String url)
throws NamingException
{
m_url = url;
//找找主接口,lookupHome是本例自定义方法。
m_home = lookupHome();
}
/**
* 在命令行运行这个实例:
* java examples.ejb20.homeMethods.Client "t3://localhost:7001"
* 参数是可选的
* @参数 url URL such as "t3://localhost:7001"
*/
public static void main(String[] args) throws NamingException {
System.out.println("\nBeginning ejb20.examples.homeMethods.Client...\n");
String url = "t3://localhost:7001";
// 解析命令行参数
if (args.length != 1) {
System.out.println("Usage: java examples.ejb20.homeMethods.Client t3://hostname:port");
return;
} else {
url = args[0];
}
Client client = null;
try {
//本类实例
client = new Client(url);
} catch (NamingException ne) {
//异常处理
ne.printStackTrace();
log("Unable to look up the beans home: " + ne.getMessage());
System.exit(1);
}
try {
//调用例程
client.example();
} catch (Exception e) {
//异常处理
log("There was an exception while creating and using the Accounts.");
log("This indicates that there was a problem communicating with the server: "+e);
}
System.out.println("\nEnd ejb20.examples.homeMethods.Client...\n");
}
//例程方法
public void example() throws RemoteException, NamingException {
//主方法
String message = m_home.homyMethod();
System.out.println("\n\n Here is the message from the remote invocation of the Home Method homyMethod: '"+
message+"'\n\n");
}
/**
* 使用JNDI查找bean主接口。
*/
private AccountHome lookupHome()
throws NamingException
{
//上下文
Context ctx = getInitialContext();
try {
//查找主接口
Object home = ctx.lookup("homeMethods.AccountHome");
return (AccountHome) PortableRemoteObject.narrow(home, AccountHome.class);
} catch (NamingException ne) {
//异常处理
log("The client was unable to lookup the EJBHome. Please make sure " +
"that you have deployed the ejb with the JNDI name " +
"homeMethods.AccountHome on the WebLogic server at "+m_url);
throw ne;
}
}
/**
* 从JNDI树中获取初始化上下文
*
* 可以使用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, m_url);
return new InitialContext(h);
} catch (NamingException ne) {
//异常处理
log("We were unable to get a connection to the WebLogic server at "+m_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 + -