RMI注册值

时间:2014-05-12 17:05:03

标签: java rmi

我的rmi程序的问题是,当我发送回显消息时,我得到一个空值,但我初始化所有的值。它似乎没有记录我为它投入的值。

服务器

package server;

import interfaces.Compute;
import interfaces.Pi;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;

public class ComputeEngine extends UnicastRemoteObject implements Compute {

protected ComputeEngine() throws RemoteException {
    super();
}

public String executeTask(Pi t){
    System.out.println(t.message);
    return t.execute();
}

public String executeTask2(Pi t) {
    return t.execute2();
}

public static void main(String[] args) {
    try {
        LocateRegistry.createRegistry(1099);
        String name = "Echo";
        Compute engine = new ComputeEngine();
        Naming.rebind(name, engine);
        name = "Compute";
        Naming.bind(name, engine);

        System.out.println("System bound");
    } catch (Exception e) {
        System.err.println("ComputeEngine exception:");
        e.printStackTrace();
    }
}
}

客户

  package client;

import interfaces.Compute;
import interfaces.Pi;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;

public class Client {
Scanner scan = new Scanner(System.in);
int v1 = 0, v2 = 0;
String echoMessage = null;

public static void main(String args[]) {
    Client e = new Client();
    String name, output;
    Registry registry;
    Compute comp;
    Pi task;

    //System.setProperty("java.security.policy","file:./security.policy");
    //if (System.getSecurityManager() == null) {
    //    System.setSecurityManager(new SecurityManager());
    //}

    System.out.println("Enter something: ");
    String input = e.scan.nextLine();
    int temp = e.processInput(input);
    try {

        switch(temp){
        case 0:
             name = "Echo";
             registry = LocateRegistry.getRegistry();
             comp = (Compute) registry.lookup(name);
             task = new Pi(e.echoMessage);
             output = comp.executeTask(task);
             System.out.println(output);
            break;
        case 1:
             name = "Compute";
             registry = LocateRegistry.getRegistry();
             comp = (Compute) registry.lookup(name);
             task = new Pi(e.v1,e.v2);
             output = comp.executeTask2(task);
             System.out.println(output);
            break;
        default:

            break;
        }
    } catch (Exception e1) {
        System.err.println("ComputePi exception:");
        e1.printStackTrace();
    }
}

private int processInput(String input){
    String temp = input.toUpperCase();
    String arr[];

    if(temp.startsWith("ECHO")){
        arr = input.split(" ", 2);
        echoMessage = arr[1];
        return 0;
    }

    else if(Character.isDigit(temp.charAt(0))){
        arr = input.split(" ");
        v1 = Integer.parseInt(arr[0]);
        v2 = Integer.parseInt(arr[2]);

        return 1;
    }
        return -1;
}
}

接口

package interfaces;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Compute extends Remote {
String executeTask(Pi t) throws RemoteException;
String executeTask2(Pi t) throws RemoteException;
}

远程对象

package interfaces;

import java.io.Serializable;

 public class Pi implements Serializable {

private static final long serialVersionUID = 227L;
public static String message = null;
private static int value1 = 0, value2= 0;

public Pi(String mess) {
    this.message = mess;
}

public Pi(int v1, int v2) {
    this.value1 = v1;
    this.value2 = v2;
}

public String execute() {
    return returnEcho(message);
}

public String execute2(){
    return addNumbers(value1, value2);
}

public static String addNumbers(int v1, int v2){
    int total = v1 + v2;

    return Integer.toString(total);
}

public static String returnEcho(String n1) {
    return n1;
}
}

1 个答案:

答案 0 :(得分:0)

您在评论中描述的症状是非常不可能的。

可能你真的是指字段的' t'为null,这可以解释为它们是静态的,因此不会与对象序列化。

注意:此系统中的远程对象不是Pi,而是ComputeEngine。