JAVA-RMI 鎵閲囩敤鐨勬満鍒訛細
1. 鏌怬bject闇瑕乺emote call 錛屽垯瀹冩墍鏈夌殑鏂規硶閮芥斁鍦ㄤ竴涓柊澹版槑鐨勬帴鍙d腑錛岃鎺ュ彛蹇呴』extends Remote鎺ュ彛
Compute鎺ュ彛涓瓨鏀炬墍鏈夐渶瑕佽繘琛岃繙紼嬭皟鐢ㄧ殑鏂規硶錛岃瀹炵幇璇ユ帴鍙g殑綾繪潵瀹炵幇榪欎簺琚繙紼嬭皟鐢ㄧ殑鏂規硶
package compute;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Compute extends Remote {
Object executeTask(Task t) throws RemoteException;
}
2.閲囩敤榪滅▼璋冪敤鐨勬柟娉曞悓鏍鋒湁涓浜涘弬鏁板拰榪斿洖鍊鹼紝瀵逛簬榪欎簺鍙傛暟鍜岃繑鍥炲鹼紝鍦ㄤ笉鍚孞AVA铏氭嫙鏈洪棿浼犺緭錛孯M I閲囧彇鐨勬槸鍊間紶閫掞紝鎵浠ユ墍鏈夌殑鍙傛暟鍜岃繑鍥炲煎繀欏籩xtends Serializable
package compute;
import java.io.Serializable;
public interface Task extends Serializable {
Object execute();
}
/*******************************************************************************************
涓婇潰涓虹涓閮ㄥ垎錛屽湪榪欏綋涓紝鎴戜滑璁捐浜嗕竴涓帴鍙o紝鍏朵腑鍖呭惈闇瑕佽繙紼嬭皟鐢ㄧ殑鏂規硶錛岀劧鍚庤璇ユ帴鍙g殑瀹炵幇鑰咃紙鍗寵繙紼嬪璞$殑owner錛夋潵瀹炵幇榪欎簺鏂規硶
*******************************************************************************************/
3. 鐜板湪鎴戜滑涓轟簡寰楀埌榪滅▼瀵硅薄錛屽氨鏉ュ疄鐜頒竴涓嬩笂闈㈠畾涔夌殑鎺ュ彛錛?br />
main鍑芥暟涓紝鏂板緩涓涓繙紼嬪璞ngine,騫跺皢瀹冧笌鍚嶅瓧name閭﹀畾錛歂aming.rebind(name, engine);
娉細The superclass UnicastRemoteObject supplies implementations for a number of java.lang.Object methods (equals, hashCode, toString) so that they are defined appropriately for remote objects. UnicastRemoteObjectalso includes constructors and static methods used to export a remote object, that is, make the remote object available to receive incoming calls from clients.
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) {
System.setSecurityManager(new RMISecurityManager());
}
String name = "http://host/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();
}
}
}
/*******************************************************************************************
涓婇潰涓虹浜岄儴鍒嗭紝鍦ㄨ繖褰撲腑錛屾垜浠緱鍒頒簡涓涓疄鐜頒簡Compute鎺ュ彛鐨勮繙紼嬪璞ngine錛屾敞鎰忥細engine涓彧鏈夐偅浜汣ompute鎺ュ彛涓殑鏂規硶鑳藉琚繙紼嬭皟鐢紝鍏朵粬鏂規硶鍙兘鏈湴璋冪敤銆傜幇鍦紝灝嗚繙紼嬪璞ngine閭﹀畾鍒癛MI Registry 銆?client鍙互浠巖egistry閭i噷寰楀埌engine鐨勫紩鐢紝瀹為檯涓婃槸涓涓猻tub錛岀浉褰撲簬涓涓猠ngine鐨勪唬鐞嗭紝榪滅▼鏂規硶琚玞lient璋冪敤浠ュ悗錛屽彲浠ユ敼鍙榚ngine鐨勭姸鎬?br />
*******************************************************************************************/
4. 涓嬮潰鎴戜滑瀹炵幇閭d簺榪滅▼鏂規硶璋冪敤鏃舵墍瑕佺敤鍒扮殑鍙傛暟錛歍ask
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).multiply(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
* arc tangent:
*
* 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;
}
}
5. 鏈鍚庢垜浠疄鐜癱lient瀵硅薄
榪滅▼瀵硅薄comp鏄氳繃RMI鐨勬敞鍐岃〃鏉ユ煡鎵懼埌鐨?/p>
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 = "http://" + 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();
}
}
}