client.java
来自「weblogic应用全实例」· Java 代码 · 共 487 行 · 第 1/2 页
JAVA
487 行
//定义本类在包examples.cluster.ejb 中
package examples.cluster.ejb;
//本接口用到的其他类
import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import java.util.Hashtable;
import examples.cluster.utils.*;
import examples.cluster.ejb.teller.*;
import examples.cluster.ejb.account.AccountResult;
/**
* 这个类使用容器管理的JDBC实体EJB演示负载平衡.
* 包括如下操作:
*
* 启动一个循环,重复查看结算值,存入和提取业务。
* 如果业务失败,检查这个业务是否确实被触发,如果没有,则触发这个业务。
* 然后,显示每次调用的情况。
*
*/
public class Client {
//静态变量
//服务器url
static String url = "t3://localhost:7001";
//账号1
static String account1 = "10000";
//账号2
static String account2 = "10005";
//循环次数
static int ITERATIONS = 5;
//最大尝试数
static final int MAXATTEMPTS = 5;
//
static final int SLEEP = 1000; //time to sleep between invocations
//窗口主接口
static TellerHome bank;
//窗口
static Teller teller;
//工具
static ClusterUtils stats = new ClusterUtils();
static int i;
//业务数
static int trans;
Client() {
}
/**
* 用命令行运行这个例程。如:
* java examples.cluster.ejb.Client "t3://localhost:7001"
*
* @参数 url URL 如 "t3://localhost:7001"
*/
public static void main(String[] args)
{
//实例开始
System.out.println("\nBeginning examples.cluster.ejb.Client...\n");
//本类实例
Client client = new Client();
// 解析命令行参数
if (args.length != 1) {
//打印用法提示
System.out.println("Usage: java examples.cluster.ejb.Client t3://hostname:port");
return;
} else {
//服务器url
url = args[0];
}
//存入数量初始值
double depositAmount = 0;
//提取数量初始值
double withdrawalAmount = 0;
//转账数量初始值
double transferAmount = 0;
try {
//获取上下文
Context ctx = getInitialContext();
// 获取TellerHome对象(bank), 从那我们创建Tellers
bank = (TellerHome)ctx.lookup("examples.cluster.ejb.TellerHome");
// 重复每个业务
for (i = 0 ; i < ITERATIONS; i++) {
//业务开始
System.out.println("Start of transaction set " + (i+1));
// 数量
depositAmount = (100 * (i+1));
transferAmount = depositAmount/2;
withdrawalAmount = transferAmount;
//业务数
trans = 0;
//结算
client.new Balance(account1).transaction();
Thread.sleep(SLEEP);
//存入
client.new Deposit(account1, depositAmount).transaction();
Thread.sleep(SLEEP);
//提取
client.new Withdrawal(account1, withdrawalAmount).transaction();
Thread.sleep(SLEEP);
//转账
client.new Transfer(account1, account2, transferAmount).transaction();
Thread.sleep(SLEEP);
//业务结束
System.out.println("End of transaction set " + (i+1) + "\n");
}
}
catch (TellerException pe) {
//异常处理
System.out.println("Processing Error: " + pe);
}
catch (Exception e) {
//其它异常
System.out.println(":::::::::::::: Error :::::::::::::::::");
e.printStackTrace();
}
System.out.println("\nStatistics for different servers:\n");
System.out.println(stats.processStatistics());
System.out.println("\nEnd examples.cluster.ejb.Client...\n");
}
/**
* 获取初始化上下文
*
* @返回 Context
* @异常 java.lang.Exception 如果通信错误
*/
static public Context getInitialContext() throws Exception {
//属性对象
Hashtable h = new Hashtable();
//设置属性
h.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, url);
//创建上下文实例
return new InitialContext(h);
}
/**
* 内部类用来调用会话beans.
*
*/
class Transaction{
//结果
TellerResult result;
//服务器
String tellerServer;
//业务id
String transId = getTransactionID();
double amount;
//账号1id
String account1Id;
//账号1服务器名
String account1Server;
//账号1结算值
double account1Balance;
//账号2id
String account2Id;
//账号2服务器名
String account2Server;
//账号2结算值
double account2Balance;
//窗口消息
String tellerMessage;
//动作消息
String actionMessage;
//账号1消息
String account1Message;
//账号2消息
String account2Message;
/**
* 调用窗口TellerBean的业务方法
* 它会捕获TellerBean的任何RemoteExceptions
* 检查业务是否驱动,
*
* @异常 Exception if the transaction is not
* completed in the MAXATTEMPTS
* @异常 TellerException if there is
* an error in performing the transaction
* @异常 RemoteException if there is
* a communications or systems failure
* @异常 CreateException if there is
* a problem creating a teller
*/
void transaction() throws Exception, TellerException, RemoteException, CreateException {
//业务计数加1
trans++;
int attempts = 0;
int set = i;
boolean committed = false;
boolean invoke = true;
//打印业务计数和业务id
System.out.println("Transaction " + trans + " of set " + (set + 1) +
" (" + transId + ")");
while (attempts < MAXATTEMPTS) {
try {
attempts++;
System.out.println(" Attempt " + attempts);
if (teller == null)
//创建窗口对象
teller = bank.create();
if (invoke) {
//触发业务
invokeTransaction();
//打印报告
buildReport();
printReport();
//业务结束
System.out.println(" End of transaction " + trans +
" of set " + (set + 1));
return;
} else { // 检查业务
//检查业务id是否触发
committed = teller.checkTransactionId(transId);
System.out.println(" Checked transaction " + transId +
": committed = " + committed);
if (committed) {
return;
} else {
//没触发
System.out.println(" Attempting Transaction " + trans + " again");
invoke = true;
}
}
}
catch (RemoteException re) {
System.out.println(" Error: " + re);
teller = null;
invoke = false;
}
}
throw new Exception(" Transaction " + trans + " of set " +
(set + 1) + " ended unsuccessfully");
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?