Hallo liebe Forenteilnehmer,
ich beschäftige mich mit RMI und habe folgende Klassen und Interfaces erstellt. leider erhalte ich beim Starten des Clients die Fehlermeldung: "Connection refused to host: 127.0.0.1; nested exception is: java.net.ConnectException: Connection refused: connect"
Der Server soll eine Instanz des Typs Service per RMI zur Verfügung stellen. Clients sollen darüber Zugriff auf das Datenobject Data erhalten. Im Anfangszustand offen soll eine Referenz übergeben werden. Der Aufruf der Methode close() soll die Datensammlung für alle Clients schließen, so dass nur noch eine Kopie der Daten übergeben wird.
Vielleicht könnte mir jemand bezüglich RMI mal auf die Sprünge helfen. Da sind sicher einige Fehler im Projekt. Danke im Voraus und ein gutes neues Jahr!
Chris
ich beschäftige mich mit RMI und habe folgende Klassen und Interfaces erstellt. leider erhalte ich beim Starten des Clients die Fehlermeldung: "Connection refused to host: 127.0.0.1; nested exception is: java.net.ConnectException: Connection refused: connect"
Der Server soll eine Instanz des Typs Service per RMI zur Verfügung stellen. Clients sollen darüber Zugriff auf das Datenobject Data erhalten. Im Anfangszustand offen soll eine Referenz übergeben werden. Der Aufruf der Methode close() soll die Datensammlung für alle Clients schließen, so dass nur noch eine Kopie der Daten übergeben wird.
Code:
package datensammlung;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;
public interface Data extends Remote, Serializable
{
String DEFAULT_NAME = "Data";
public void append(String s) throws RemoteException, InterruptedException, MalformedURLException, NotBoundException;
public ArrayList<String> getValues() throws RemoteException;
}
Code:
package datensammlung;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Service extends Remote
{
String DEFAULT_NAME = "Service";
Data open() throws RemoteException, InterruptedException, MalformedURLException, NotBoundException;
Data get() throws RemoteException, InterruptedException, MalformedURLException, NotBoundException;
Data close() throws RemoteException, InterruptedException, MalformedURLException, NotBoundException;
boolean isOpen() throws RemoteException;
}
Code:
package datensammlung;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
public class DataImpl implements Data, Serializable
{
/**
*
*/
private static final long serialVersionUID = -9100586865132248453L;
public DataImpl()
{
}
@Override
public synchronized ArrayList<String> getValues() throws RemoteException
{
DataImpl dataImpl = new DataImpl();
try
{
UnicastRemoteObject.exportObject(dataImpl, 0);
Naming.rebind(Service.DEFAULT_NAME, dataImpl);
for (int i = 0; i < (dataImpl.getValues()).size(); i++)
{
System.out.println("Eintrag: " + (dataImpl.getValues()).get(i));
}
}
catch (Exception e)
{
}
return dataImpl.getValues();
}
@Override
public synchronized void append(String s)
throws RemoteException, MalformedURLException, InterruptedException, NotBoundException
{
Service service = new ServiceImpl();
((service.open()).getValues()).add(s);
}
}
Code:
package datensammlung;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class ServiceImpl extends UnicastRemoteObject implements Service
{
/**
*
*/
private static final long serialVersionUID = -2208500659594897934L;
private DataImpl dataImpl = new DataImpl();
private String url = "rmi://127.0.0.1/";
private boolean open = true;
public ServiceImpl() throws RemoteException
{
}
@Override
public synchronized Data open()
throws RemoteException, InterruptedException, MalformedURLException, NotBoundException
{
open = true;
return get();
}
@Override
public synchronized Data get() throws RemoteException, MalformedURLException, NotBoundException
{
if (isOpen())
{
Data myData = (Data) Naming.lookup("rmi://127.0.0.1/Data");
return myData;
}
return dataImpl;
}
@Override
public synchronized Data close()
throws RemoteException, InterruptedException, MalformedURLException, NotBoundException
{
open = false;
return get();
}
@Override
public synchronized boolean isOpen() throws RemoteException
{
return open;
}
}
Code:
package datensammlung;
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Server
{
static int port;
public Server(int port)
{
Server.port = port;
}
public static void main(String[] args) throws MalformedURLException, AlreadyBoundException
{
System.out.println("...richte Server ein...");
try
{
Service myService = new ServiceImpl();
Registry registry = LocateRegistry.createRegistry(port);
registry.rebind("Service", myService);
// Naming.rebind("Service", myService);
System.out.println("Server gestartet!");
}
catch (RemoteException e)
{
System.out.println("Ausnahme: " + e.getMessage());
e.printStackTrace();
}
/*
* DataImpl dataImpl = new DataImpl();
* try
* {
* UnicastRemoteObject.exportObject(dataImpl, 0);
* Naming.rebind(Data.DEFAULT_NAME, dataImpl);
* }
* catch (Exception e)
* {
* System.out.println("Ausnahme: " + e.getMessage());
* e.printStackTrace();
* }
*/
}
}
Code:
package datensammlung;
import java.rmi.Naming;
public class Client
{
public Client(final String... args)
{
try
{
Service myService = (Service) Naming.lookup("rmi://127.0.0.1:1010/Service");
System.out.println((myService.get()).getValues());
for (int i = 0; i < args.length; i++)
{
(myService.get()).append(args[i]);
}
System.out.println(myService.close().getValues());
System.out.println(myService.open().getValues());
System.out.println(myService.isOpen());
}
catch (Exception e)
{
System.out.println("Ausnahme_Client: " + e.getMessage());
}
}
public static void main(String[] args)
{
Client x = new Client("egon", "sabine", "fred");
}
}
Vielleicht könnte mir jemand bezüglich RMI mal auf die Sprünge helfen. Da sind sicher einige Fehler im Projekt. Danke im Voraus und ein gutes neues Jahr!
Chris