package address;
import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;
import java.util.Collection;
import java.util.Iterator;
import java.util.Enumeration;
public class AddressesClient {
static final private int MAX_OUTPUT_LINE_LENGTH = 50;
private AddressesHome addressesHome = null;
//创建一个EJB测试客户端
public AddressesClient() {
try {
//得到名字上下文
Context ctx = getInitialContext();
//查询jndi名
Object ref = ctx.lookup("Addresses");
//通过强制转型得到Home接口
addressesHome = (AddressesHome) PortableRemoteObject.narrow(ref, AddressesHome.class);
}
catch(Exception e) {
e.printStackTrace();
}
}
//这个函数返回一个经过初始化的上下文
private Context getInitialContext() throws Exception {
String url = "t3://cgb-4wn01xj69v6:7001";
String user = null;
String password = null;
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
}
return new InitialContext(properties);
}
catch(Exception e) {
System.out.println("Unable to connect to WebLogic server at " + url);
System.out.println("Please make sure that the server is running.");
throw e;
}
}
// 工具函数
public AddressesHome getHome() {
return addressesHome;
}
private void log(String message) {
if (message == null) {
System.out.println("-- null");
return ;
}
if (message.length() > MAX_OUTPUT_LINE_LENGTH) {
System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) + " ...");
}
else {
System.out.println("-- " + message);
}
}
public void executeFindAll() {
try {
executeCollectionFinder(addressesHome.findAll());
}
catch(Exception e) {
e.printStackTrace();
}
}
public void executeCollectionFinder(Collection all) {
try {
Iterator iterator = all.iterator();
while (iterator.hasNext()) {
Object object = iterator.next();
Addresses addresses = (Addresses) PortableRemoteObject.narrow(object, Addresses.class);
log(addresses.toString());
}
}
catch(Exception e) {
e.printStackTrace();
}
}
public void executeEnumerationFinder(Enumeration all) {
try {
while (all.hasMoreElements()) {
Object object = all.nextElement();
Addresses addresses = (Addresses) PortableRemoteObject.narrow(object, Addresses.class);
log(addresses.toString());
}
}
catch(Exception e) {
e.printStackTrace();
}
}
//Main函数
public static void main(String[] args) {
AddressesClient client = new AddressesClient();
//用client的getHome()函数调用Home接口函数得到远程接口的引用
// 用远程接口的引用访问EJB
try {
Addresses Addressesobj=client.getHome().create("Jame","Janny");
Addressesobj.setCity("New York");
Addressesobj.setState("New York");
Addressesobj.setZip("310000");
Addressesobj.setAddress("Wall Street");
System.out.print("This is CMP EntityBean example!\n");
System.out.print("FirstName:"+Addressesobj.getFirstName()+"\n");
System.out.print("LastName:"+Addressesobj.getLastName()+"\n");
System.out.print("Address:"+Addressesobj.getAddress()+"\n");
System.out.print("State:"+Addressesobj.getState()+"\n");
System.out.print("The example is over!\n");
}
catch(Exception e) {
e.printStackTrace();
}
}
}
ejbCreate函数用于初始化一个EJB实例.7z
版权申诉
201 浏览量
2022-07-06
09:30:43
上传
评论
收藏 2KB 7Z 举报
qq_38220914
- 粉丝: 600
- 资源: 4313
最新资源
- 农村信用社联合社计算机信息系统投产与变更管理办.docx
- 农村信用社联合社计算机信息系统数据管理办法.docx
- 利用SPSS作临床效度分析线上计算网站介绍-医学研究部统计谘.(医学PPT课件).ppt
- 利用Zabbix监控mysqldump定时备份数据库状态.docx
- 利用计算机解决问题的基本过程.doc
- 化工铁路通信工程总结.doc
- 北京大学网络教育软件工程作业.docx
- 医药公司(连锁店)计算机操作规程未新系统的自行按照旧制修改-新系统过制的编号加修模版.doc
- 医药公司(连锁店)计算机系统操作规程模版.doc
- 医药连锁门店计算机系统的操作和管理程序未新系统的自行按照旧制修改-新系统过制的编号加修模版.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈