computer 版 (精华区)
发信人: arbo (arbo), 信区: program
标 题: java笔记12
发信站: 听涛站 (2001年09月17日10:52:56 星期一), 站内信件
发信人: airforce1 (五湖废人), 信区: Java
标 题: java笔记12
发信站: BBS 水木清华站 (Mon Apr 3 14:04:56 2000)
下午开始将RMI了。
在Sun教材里附录里只有简单的教程。
老师给了一个教程。可以到Sun站点去Dl.
RMI, 服务器和client通过借口交互
在server 上做4个步骤。
1. 定义接口
package compute;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Compute extends Remote {
Object executeTask(Task t) throws RemoteException;
}
///*******
package compute;
import java.io.Serializable;
public interface Task extends Serializable {
Object execute();
}
2。 实现方法
package engine;
import java.rmi.*;
import java.rmi.server.*;
import compute.*;
public class ComputeEngine extends UnicastRemoteObject
implements Compute
{
public ComputeEngine() throws RemoteException {
super();
}
public Object executeTask(Task t) {
return t.execute();
}
public static void main(String[] args) {
if (System.getSecurityManager() == null) {
//RMI, 安全管理器
System.setSecurityManager(new RMISecurityManager());
}
//对象在服务器上的名字
String name = "//localhost/Compute";
try {
Compute engine = new ComputeEngine();
Naming.rebind(name, engine);//创建对象给个名字
System.out.println("ComputeEngine bound");
} catch (Exception e) {
System.err.println("ComputeEngine exception: " + e.getMessage()
);
e.printStackTrace();
}
}
}
把程序打包,然后可以开始服务器程序。(不要忘了设置classpath and path
set CLASSPATH=l:\javat\rmi\program;
set path=c:\jdk1.2\bin;
rmiregistry
另一个窗口
java -Djava.rmi.server.codebase=http://localhost/rmi
-Djava.security.policy=java.policy engine.ComputeEngine
客户机
package client;
import java.rmi.*;
import java.math.*;
import compute.*;
public class ComputePi {
public static void main(String args[]) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
String name = "//" + args[0] + "/Compute";
Compute comp = (Compute) Naming.lookup(name);
Pi task = new Pi(Integer.parseInt(args[1]));
BigDecimal pi = (BigDecimal) (comp.executeTask(task));
System.out.println(pi);
} catch (Exception e) {
System.err.println("ComputePi exception: " + e.getMessage());
e.printStackTrace();
}
}
}
/***********
package client;
import compute.*;
import java.math.*;
public class Pi implements Task {
/** constants used in pi computation */
private static final BigDecimal ZERO =
BigDecimal.valueOf(0);
private static final BigDecimal ONE =
BigDecimal.valueOf(1);
private static final BigDecimal FOUR =
BigDecimal.valueOf(4);
/** rounding mode to use during pi computation */
private static final int roundingMode =
BigDecimal.ROUND_HALF_EVEN;
/** digits of precision after the decimal point */
private int digits;
/**
* Construct a task to calculate pi to the specified
* precision.
*/
public Pi(int digits) {
this.digits = digits;
}
/**
* Calculate pi.
*/
public Object execute() {
return computePi(digits);
}
/**
* Compute the value of pi to the specified number of
* digits after the decimal point. The value is
* computed using Machin's formula:
*
* pi/4 = 4*arctan(1/5) - arctan(1/239)
*
* and a power series expansion of arctan(x) to
* sufficient precision.
*/
public static BigDecimal computePi(int digits) {
int scale = digits + 5;
BigDecimal arctan1_5 = arctan(5, scale);
BigDecimal arctan1_239 = arctan(239, scale);
BigDecimal pi = arctan1_5.multiply(FOUR).subtract(arctan1_239).multi
ply((FOUR);
return pi.setScale(digits,
BigDecimal.ROUND_HALF_UP);
}
/**
* Compute the value, in radians, of the arctangent of
* the inverse of the supplied integer to the speficied
* number of digits after the decimal point. The value
* is computed using the power series expansion for the
* arctangent:
*
* arctan(x) = x - (x^3)/3 + (x^5)/5 - (x^7)/7 +
* (x^9)/9 ...
*/
public static BigDecimal arctan(int inverseX,
int scale)
{
BigDecimal result, numer, term;
BigDecimal invX = BigDecimal.valueOf(inverseX);
BigDecimal invX2 =
BigDecimal.valueOf(inverseX * inverseX);
numer = ONE.divide(invX, scale, roundingMode);
result = numer;
int i = 1;
do {
numer =
numer.divide(invX2, scale, roundingMode);
int denom = 2 * i + 1;
term =
numer.divide(BigDecimal.valueOf(denom),
scale, roundingMode);
if ((i % 2) != 0) {
result = result.subtract(term);
} else {
result = result.add(term);
}
i++;
} while (term.compareTo(ZERO) != 0);
return result;
}
}
clinet 上
java -Djava.rmi.server.codebase=http://localhost/rmi
-Djava.security.policy=java.policy client.ComputePi localhost 2
要有一个java.policy文件,如下
grant {
permission java.net.SocketPermission "*:1024-65535",
"connect,accept";
permission java.net.SocketPermission "*:80", "connect";
};
客户机上要计算Pi的值,计算量大,就把任务传到服务器上执行
RMI步骤:
1. 编译compute目录下class,
javac -d c:\myp *.java
copy于http://http://localhost/rmi/compute
2. 编译engine目录下class,
set classpath=c:\myp;c:\jdk1.2\bin;.
Javac ComputeEngine.java
上一层目录下:
rmic -d . engine.ComputeEngine
copy于http://http://localhost/rmi/engine
3. 编译client目录下class,
javac ComputePi.java
javac Pi.java
copy于http://http://www.shu.edu.cn/~xyx/rmi
PC机DOS窗口, classpath设向无class目录,start rmiregistry
PC机DOS窗口,启动服务
Engine目录下:(可写于批处理文件中来执行)
java -Djava.rmi.server.codebase=http://localhost/rmi/
-Djava.rmi.server.hostname=localhost
-Djava.security.policy=java.policy
engine.ComputeEngine
PC机DOS窗口,执行客户端Pi 20位长度
Client父目录下:
java -Djava.rmi.server.codebase=http://localhost/rmi
-Djava.security.policy=java.policy client.ComputePi localhost 20
---------------------------
方法二。
Jar file,包含一系列接口提供给Client和Server开发人员
L:\javat\rmi\program>
javac compute\Compute.java
javac compute\Task.java
jar cvf compute.jar compute\*.class
compute\*.class server\compute
Server
L:\javat\rmi\program>
javac engine\ComputeEngine.java
创建Stub
rmic -d . engine.ComputeEngine
engine\*.class server\engine
Client
L:\javat\rmi\program>
javac client\ComputePi.java
javac client\Pi.java
Run:
DOS窗口一: Start rmiregistry and server
set CLASSPATH=l:\javat\rmi\program;(Important!!)
set path=c:\jdk1.2\bin;c:\windows;c:\windows\command
start rmiregistry
set CLASSPATH=c:\jdk1.2\bin;l:\javat\rmi\program\compute.jar;.
set path=c:\jdk1.2\bin;c:\windows;c:\windows\command
java -Djava.rmi.server.codebase=http://localhost/rmi/
-Djava.rmi.server.hostname=202.120.127.202 -Djava.security.policy=java.p
oliccy engine.ComputeEngine
DOS窗口二:
set CLASSPATH=c:\jdk1.2\bin;l:\javat\rmi\program\compute.jar;.
set path=c:\jdk1.2\bin;c:\windows;c:\windows\command
set CLASSPATH=l:\javat\rmi\program;(Important!!)
set path=c:\jdk1.2\bin;c:\windows;c:\windows\command
start rmiregistry
set CLASSPATH=c:\jdk1.2\bin;l:\javat\rmi\program\compute.jar;.
set path=c:\jdk1.2\bin;c:\windows;c:\windows\command
java -Djava.rmi.server.codebase=http://localhost/rmi/
-Djava.rmi.server.hostname=202.120.127.202 -Djava.security.policy=java.p
olic
DOS窗口二:
set CLASSPATH=c:\jdk1.2\bin;l:\javat\rmi\program\compute.jar;.
set path=c:\jdk1.2\bin;c:\windows;c:\windows\command
java -Djava.rmi.server.codebase=http://localhost/rmi/
-Djava.security.policy=java.policy client.ComputePi localhost 300
教程里的内容也贴出来,谁帮忙排下版把。
--
※ 来源:·听涛站 tingtao.dhs.org·[FROM: 匿名天使的家]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:1.401毫秒