RMI服务器无法启动

时间:2018-03-31 04:48:17

标签: java rmi

我正在尝试在eclipse neon IDE中使用RMI构建火警系统。但是当我在启动rmiregistry后尝试运行Server.java文件时,它会说错误。

java.rmi.server.ExportException: object already exported
    at sun.rmi.transport.ObjectTable.putTarget(Unknown Source)
    at sun.rmi.transport.Transport.exportObject(Unknown Source)
    at sun.rmi.transport.tcp.TCPTransport.exportObject(Unknown Source)
    at sun.rmi.transport.tcp.TCPEndpoint.exportObject(Unknown Source)
    at sun.rmi.transport.LiveRef.exportObject(Unknown Source)
    at sun.rmi.server.UnicastServerRef.exportObject(Unknown Source)
    at java.rmi.server.UnicastRemoteObject.exportObject(Unknown Source)
    at java.rmi.server.UnicastRemoteObject.exportObject(Unknown Source)
    at server.Server.main(Server.java:33)

以下是我项目中所有课程的代码

Service.java文件

package server;

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

public interface Service extends Remote{
public void startService() throws RemoteException;

}

FireAlarmService.java文件

package server;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import sensors.BatterySensor;
import sensors.CO2Sensor;
import sensors.HumiditySensor;
import sensors.Sensor;
import sensors.TempSensor;

public class FireAlarmService extends UnicastRemoteObject implements     Service,Runnable {
protected FireAlarmService() throws RemoteException {
    super();
}

Sensor tempSensor = new TempSensor();
Sensor humiditySensor = new HumiditySensor();
Sensor co2Sensor = new CO2Sensor();
Sensor batterySensor = new BatterySensor();

@Override
public void run() {
    Thread tempThread = new Thread(tempSensor);
    Thread humidityThread = new Thread(humiditySensor);
    Thread co2Thread = new Thread(co2Sensor);
    Thread batteryThread = new Thread(batterySensor);

    tempThread.start();
    humidityThread.start();
    co2Thread.start();
    batteryThread.start();
    System.out.println();

    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

    @Override
    public void startService() {
        while(true){
            run();  
        }       
    }
}

Server.java文件

package server;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class Server{
    public static void main(String args[]){

        //new code tried to apply.ended in an 3 excception
        /*try {     
            FireAlarmService service = new FireAlarmService();
            Naming.rebind("rmi://localhost/fireAlarmService", service);         
            System.out.println("Server is ready.");         
        }catch (Exception e) {      
            System.out.println("Addition Server failed: " + e);         
        }
    }*/

        FireAlarmService service = null;
        try {
            service = new FireAlarmService();
        } catch (RemoteException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            System.setProperty("java.rmi.server.hostname","192.168.1.2");
            Service stub = (Service)        UnicastRemoteObject.exportObject(service,9000);
            Registry registry = LocateRegistry.getRegistry();
            registry.bind("AlarmService", stub);
            System.out.println("Server ready");
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (AlreadyBoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }/* catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/

    }
}

Sensor.java

package sensors;

public abstract class Sensor implements Runnable{
    String sensorID = "Not Set";

    public void setSensorID(String sensorID){
        this.sensorID = sensorID;
    }

    public String getSensorID() {
        return this.sensorID;
    }

    public abstract void setSensorData();
    public abstract double getSensorData();
}

BatterySensor.java

package sensors;

public class BatterySensor extends Sensor{
    double batteryLevel = 100;

    @Override
    public void setSensorData() {
        if(batteryLevel <= 0){
            batteryLevel = 100;
        }
        else{
            batteryLevel--; 
        }
    }

    @Override
    public double getSensorData() {
        return batteryLevel;
    }

    @Override
    public void run() {
        setSensorData();        
        System.out.println("Battery Level : " + getSensorData());

        /*try {     
            Thread.sleep(3000);         
        } catch (InterruptedException e) {  
            e.printStackTrace();            
        }*/
    }

    /*public static void main(String args[]){
        BatterySensor sensor = new BatterySensor();

        while(true){
            sensor.run();   
        }       
    }*/
}

CO2Sensor.java

package sensors;

public class CO2Sensor extends Sensor{
    double co2Level = 0;

    @Override
    public void setSensorData() {
        co2Level = Math.round(Math.random()) * 10;
    }

    @Override
    public double getSensorData() {
        return co2Level;
    }

    @Override
    public void run() {             
        setSensorData();        
        System.out.println("CO2 Level : " + getSensorData());

        /*try {     
            Thread.sleep(3000);         
        } catch (InterruptedException e) {  
            e.printStackTrace();            
        }*/
    }

    /*public static void main(String args[]){
        CO2Sensor sensor = new CO2Sensor();

        while(true){
            sensor.run();   
        }       
    }*/

}

HumiditySensor.java

package sensors;

public class HumiditySensor extends Sensor{
    double humidity = 0;
    @Override
    public void setSensorData() {
        humidity = Math.round(Math.random()) * 10;
    }

    @Override
    public double getSensorData() {
        return humidity;
    }

    @Override
    public void run() {
        setSensorData();        
        System.out.println("Humidity : " + getSensorData());

        /*try {     
            Thread.sleep(3000);         
        } catch (InterruptedException e) {  
            e.printStackTrace();            
        }*/
    }

    /*public static void main(String args[]){
        CO2Sensor sensor = new CO2Sensor();

        while(true){
            sensor.run();   
        }       
    }*/
}

TempSensor.java

package sensors;

public class TempSensor extends Sensor{
    double temperature = 0;
    @Override
    public void setSensorData() {
        temperature = Math.round(Math.random()) * 10;
    }

    @Override
    public double getSensorData() {
        return temperature;
    }

    @Override
    public void run() {
        setSensorData();        
        System.out.println("Temperature : " + getSensorData());

        /*try {     
            Thread.sleep(3000);         
        } catch (InterruptedException e) {  
            e.printStackTrace();            
        }*/
    }

    /*public static void main(String args[]){
        CO2Sensor sensor = new CO2Sensor();

        while(true){
            sensor.run();   
        }       
    }*/
}

Client.java

package client;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import server.Service;

public class Client implements Runnable{
    static Service stub = null;
    @Override
    public void run() {
        try {
            stub.startService();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
    }

    public static void main(String args[]){
        try {
            Registry registry = LocateRegistry.getRegistry();
            stub = (Service) registry.lookup("FireAlarmService");
            new Client().run();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NotBoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

我没有检查Client.java ..如果可以请检查它是否也有错误。 附: :我不是在试图让你们失望。但链接JAVA: RMI Callback -> object already exported上的答案并不适用于我。

1 个答案:

答案 0 :(得分:0)

此:

FireAlarmService extends UnicastRemoteObject

表示此类的实例将在构造时自动导出。

因此,您不能也不能使用此类的实例调用UnicastRemoteObject.exportObject()