客戶文件:
package account;import javax.ejb.*;
import javax.naming.*;
import javax.rmi.*;
import java.rmi.*;
import java.util.*;public class AccountClient{
private static Context getInitialContext() throws Exception
{
String url = "t3://localhost: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)
{
throw e;
}
} public static void main(String[] args) throws Exception{
Account account=null;
try{
// Context ctx=new InitialContext(System.getProperties());
Context ctx = getInitialContext();
Object obj=ctx.lookup("AccountHome"); AccountHome Home=(AccountHome)PortableRemoteObject.narrow(obj,AccountHome.class);
System.out.println("Total of all accounts in bank initially="+Home.getTotalBankValue());
Home.create("123-456-7890","John Smith");
Iterator i = Home.findByOwnerName("John Smith").iterator();
if(i.hasNext()){
account=(Account)javax.rmi.PortableRemoteObject.narrow(i.next(),Account.class);
}else{
throw new Exception("Could not find account");
}
System.out.println("Initial Balance="+account.getBalance());
account.deposit(100);
System.out.println("After depositing 100,account balance="+account.getBalance());
System.out.println("Total of all account in bank now="+Home.getTotalBankValue());
AccountPK pk=(AccountPK)account.getPrimaryKey();
account=null;
account=Home.findByPrimaryKey(pk);
System.out.println("Found account with ID"+pk+".Balance="+account.getBalance());
System.out.println("Now trying to withdraw $150,which is more than is currently available.This should generate an exception...");
account.withdraw(150);
}catch(Exception e){
System.out.println("Caught exception!");
e.printStackTrace();
}finally{
try{
System.out.println("Destroying account..");
if(account!=null){
account.remove();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
};
package account;import javax.ejb.*;
import javax.naming.*;
import javax.rmi.*;
import java.rmi.*;
import java.util.*;public class AccountClient{
private static Context getInitialContext() throws Exception
{
String url = "t3://localhost: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)
{
throw e;
}
} public static void main(String[] args) throws Exception{
Account account=null;
try{
// Context ctx=new InitialContext(System.getProperties());
Context ctx = getInitialContext();
Object obj=ctx.lookup("AccountHome"); AccountHome Home=(AccountHome)PortableRemoteObject.narrow(obj,AccountHome.class);
System.out.println("Total of all accounts in bank initially="+Home.getTotalBankValue());
Home.create("123-456-7890","John Smith");
Iterator i = Home.findByOwnerName("John Smith").iterator();
if(i.hasNext()){
account=(Account)javax.rmi.PortableRemoteObject.narrow(i.next(),Account.class);
}else{
throw new Exception("Could not find account");
}
System.out.println("Initial Balance="+account.getBalance());
account.deposit(100);
System.out.println("After depositing 100,account balance="+account.getBalance());
System.out.println("Total of all account in bank now="+Home.getTotalBankValue());
AccountPK pk=(AccountPK)account.getPrimaryKey();
account=null;
account=Home.findByPrimaryKey(pk);
System.out.println("Found account with ID"+pk+".Balance="+account.getBalance());
System.out.println("Now trying to withdraw $150,which is more than is currently available.This should generate an exception...");
account.withdraw(150);
}catch(Exception e){
System.out.println("Caught exception!");
e.printStackTrace();
}finally{
try{
System.out.println("Destroying account..");
if(account!=null){
account.remove();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
};
Caught exception!
account.AccountException: javax.naming.LinkException: [Root exception is javax.
naming.NameNotFoundException: Unable to resolve 'ejbPool' Resolved: '' Unresolve
d:'ejbPool' ; remaining name 'ejbPool']; Link Remaining Name: 'ejbPool'Start server side stack trace:
account.AccountException: javax.naming.LinkException: [Root exception is javax.
naming.NameNotFoundException: Unable to resolve 'ejbPool' Resolved: '' Unresolve
d:'ejbPool' ; remaining name 'ejbPool']; Link Remaining Name: 'ejbPool'
at account.AccountBean.ejbHomeGetTotalBankValue(AccountBean.java:71)
at account.AccountBean_8259n1_Impl.ejbHomeGetTotalBankValue(AccountBean_
8259n1_Impl.java:212)
at account.AccountBean_8259n1_HomeImpl.getTotalBankValue(AccountBean_825
9n1_HomeImpl.java:161)
at account.AccountBean_8259n1_HomeImpl_WLSkel.invoke(Unknown Source)
at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:359)
at weblogic.rmi.cluster.ReplicaAwareServerRef.invoke(ReplicaAwareServerR
ef.java:114)
at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:313)
at weblogic.security.service.SecurityServiceManager.runAs(SecurityServic
eManager.java:762)
at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.jav
a:308)
at weblogic.rmi.internal.BasicExecuteRequest.execute(BasicExecuteRequest
.java:30)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:152)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:133)
End server side stack trace at weblogic.rmi.internal.BasicOutboundRequest.sendReceive(BasicOutboundR
equest.java:109)
at weblogic.rmi.cluster.ReplicaAwareRemoteRef.invoke(ReplicaAwareRemoteR
ef.java:262)
at weblogic.rmi.cluster.ReplicaAwareRemoteRef.invoke(ReplicaAwareRemoteR
ef.java:229)
at account.AccountBean_8259n1_HomeImpl_WLStub.getTotalBankValue(Unknown
Source)
at account.AccountClient.main(AccountClient.java:43)
Destroying account..Press any key to continue...