2014-05-12 76 views
0

我的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; 
} 
} 
+0

你得到一個空值在哪裏?客戶端上的 – EJP

+0

。輸出= comp.executeTask(任務);任務有價值,但在服務器端的任務變爲null或0 – user3629420

+0

因此,'t'參數在它到達服務器的executeTask()時爲空? – EJP

回答

0

您在評論中描述的症狀是完全不可能的。

可能你確實意味着't'的字段爲空,這是由它們是靜態的,因此不與對象串行化的事實解釋的。

NB此係統中的遠程對象不是Pi而是ComputeEngine。

+0

我不能相信這是簡單的。我刪除了靜態,它的工作原理。謝謝你,我很感激 – user3629420