helloclusterclient.java
来自「weblogic应用全实例」· Java 代码 · 共 116 行
JAVA
116 行
//声明这个类定义在包examples.cluster.rmi中
package examples.cluster.rmi;
//声明引入的其它类
import weblogic.rmi.RemoteException;
import weblogic.jndi.Environment;
import weblogic.jndi.WLInitialContextFactory;
import javax.naming.*;
import examples.cluster.utils.ClusterUtils;
/**
* 客户端程序
*
*/
public class HelloClusterClient {
//远程接口
private HelloCluster hello;
private static String usage = "java examples.cluster.rmi.HelloClusterClient [-options]\n\n"
+"where options are:\n"
+" -url URL of a WebLogic Server, can be any member of a Cluster\n"
+" -i Number of iterations to execute, default is 10\n"
+" -noverbose Supresses responses from RMI Object\n"
+" -help This message";
private boolean verbose = true;
public static String url = null;
public static int iterations = 10;
public String hello() throws RemoteException{
return hello.sayHello();
}
public static int getIterations(){
return iterations;
}
/**
* 构造方法
*/
public HelloClusterClient(String[] args) throws NamingException {
if ((args.length == 0)){
exit();
}
//解析命令行参数
for (int i = 0; i < args.length; i++) {
try{
if (args[i].equals("-url") && (i+1 < args.length))
url = args[++i];
else if (args[i].equals("-i") && (i+1 < args.length))
iterations = Integer.parseInt(args[++i]);
else if (args[i].equalsIgnoreCase("-noverbose"))
verbose = false;
else if (args[i].equalsIgnoreCase("-h") || (args[i].equals("-help"))){
exit();
}
}catch(NumberFormatException e){
exit();
}
}
if(url==null){
exit();
}
// 获取名称上下文
Environment env = new Environment();
env.setProviderUrl(url);
Context context = env.getInitialContext();
// 查找RMI服务
hello = (HelloCluster)context.lookup(HelloClusterImpl.BINDNAME);
}
/**
* main方法
*/
public static void main(String inArgs[]) {
HelloClusterClient client = null;
ClusterUtils utils = null;
try {
//本类实例化
client = new HelloClusterClient(inArgs);
} catch(NamingException e) {
//异常处理
System.out.println("Exception with server: " + url);
e.printStackTrace();
return;
}
//簇工具
utils = new ClusterUtils();
for(int i=0; i<getIterations(); i++) {
try {
String returnHello = client.hello();
if(client.verbose)
System.out.println(returnHello);
utils.addClusterMessage(returnHello);
// Thread.sleep(200); // pause for clarity
} catch(Exception e) {
e.printStackTrace();
utils.incrementExceptionCount();
}
}
System.out.println("\nCompleted " + utils.getIterations() +
" iterations with " + utils.getExceptionCount() +
" number of exceptions.");
System.out.println("\nNow processing aggregate results.\n\n");
System.out.println(utils.processStatistics());
}
private void exit(){
System.out.println(usage);
System.exit(-1);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?