?? bmpclienttest.java
字號:
package bmp;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Properties;
import java.util.Vector;
import java.util.Iterator;
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;
* <p>Title: 客戶端</p>
* <p>Description: 這個類演示了如何調用一個實體EJB,并進行如下操作</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: bmpClientTest.java</p>
* @author 杜江
* @version 1.0
*/
public class bmpClientTest {
//聲明變量
private String url;
private bmpTradeHome home;
//構造方法
public bmpClientTest(String url)
throws NamingException
{
this.url = url;
//查找主接口,lookupHome是本例自定義方法。
home = lookupHome();
}
/**
* 在命令行運行這個實例:
* java bmp.bmpClientTest "t3://localhost:7001"
* 參數是可選的
* @參數 url URL such as "t3://localhost:7001" of Server
*/
public static void main(String[] args) throws NamingException {
System.out.println("\nBeginning bmp.bmpClientTest...\n");
String url = "t3://localhost:7001";
// 解析命令行參數,如果沒有則使用默認的"t3://localhost:7001"
if (args.length == 1) {
url = args[0];
}
System.out.println("URL="+url);
bmpClientTest bmpClientTest = null;
try {
//實例化本類
bmpClientTest = new bmpClientTest(url);
} catch (NamingException ne) {
//異常處理
log("Unable to look up the beans home: " + ne.getMessage());
System.exit(1);
}
try {
//運行例程
bmpClientTest.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 bmp.bmpClientTest...\n");
}
//執行實例
public void example()
throws CreateException, RemoteException, FinderException,
RemoveException
{
int numBeans = 10;
//聲明并創建賬號數組
bmpTrade[] bmpTrade = new bmpTrade[numBeans];
//創建10個賬號
for (int i=1; i<numBeans; i++) {
bmpTrade [i] = findOrCreateAccount(i+"", i * 200);
}
// 打印賬號結算
for (int i=1; i<numBeans; i++) {
log("Account: :"+bmpTrade[i].getPrimaryKey()+" has a balance of "+bmpTrade[i].getBalance());
}
// 查找所有結算大于500的賬號
findBigAccounts(500.0);
// 清除所有賬號
log("Removing beans...");
for (int i=1; i<numBeans; i++) {
bmpTrade[i].remove();
}
}
/**
* 列出所有結算大于給定值的賬號
* 這個finder方法演示返回枚舉賬號
*/
private void findBigAccounts(double balanceGreaterThan)
throws RemoteException, FinderException
{
log("\nQuerying for fund with a balance greater than "+balanceGreaterThan + "...");
//調用主接口創建賬號方法findBigAccounts,返回賬號集合
Collection col = home.findBigAccounts(balanceGreaterThan);
if(col.isEmpty()) {
log("No fund were found with a balance greater that "+balanceGreaterThan);
}
Iterator it = col.iterator();
while (it.hasNext()) {
//創建遠程對象
bmpTrade accountGT = (bmpTrade) PortableRemoteObject.narrow(it.next(),bmpTrade.class);
//列出合乎要求的賬戶
log("Account " + accountGT.getPrimaryKey() + "; fund is $" + accountGT.getBalance());
}
}
//如果對應id的賬號以存在,則返回這個id,否則創建它
private bmpTrade findOrCreateAccount(String id, double balance)
throws CreateException, RemoteException, FinderException
{
try {
log("Trying to find account with id: "+id);
return (bmpTrade) PortableRemoteObject.narrow(home.findByPrimaryKey(id),bmpTrade.class);
} catch (ObjectNotFoundException onfe) {
// 賬號不存在,創建它
return (bmpTrade) PortableRemoteObject.narrow(home.create(id, balance),bmpTrade.class);
}
}
// 給定id和結算創建一個新的賬號
private bmpTrade createAccount(String id, double balance)
throws CreateException, RemoteException
{
log("Creating account " + id + " with a balance of " +
balance + "...");
//創建遠程賬號對象
bmpTrade ac = (bmpTrade) PortableRemoteObject.narrow(home.create(id, balance),bmpTrade.class);
log("Account " + id + " successfully created");
return ac;
}
//使用JNDI查找bean的主接口
private bmpTradeHome lookupHome()
throws NamingException
{
Context ctx = getInitialContext();
try {
//查找主接口
Object home = ctx.lookup("bmpbean");
return (bmpTradeHome) PortableRemoteObject.narrow(home, bmpTradeHome.class);
} catch (NamingException ne) {
//異常處理
log("The bmpClientTest was unable to lookup the EJBHome. Please make sure " +
"that you have deployed the ejb with the JNDI name " +
"bmp on the WebLogic server at "+url);
throw ne;
}
}
//獲取初始化上下文
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 + -