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实例
共5个文件
java:5个
需积分: 0 0 下载量 149 浏览量
2022-08-27
14:18:25
上传
评论
收藏 3KB RAR 举报
温馨提示
ejbCreate函数用于初始化一个EJB实例
资源推荐
资源详情
资源评论
收起资源包目录
ejbCreate函数用于初始化一个EJB实例.rar (5个子文件)
codefans.net
cmp
address
AddressesPK.java 655B
AddressesHome.java 630B
Addresses.java 685B
AddressesBean.java 2KB
AddressesClient.java 4KB
共 5 条
- 1
资源评论
「已注销」
- 粉丝: 119
- 资源: 213
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功