Kontrollieren eines Threads mit Hilfe von RMI

dandiii

Grünschnabel
Hallo,

habe ein Problem mit Threads...

Ich benötige einen Thread der alle Sekunden etwas ausführt (als Beispiel habe ich das inkrementieren einer Zahl verwendet). Von diesem Thread möchte ich Ergebnisse über eine andere VM auslesen. Als Lösung hab ich mir gedacht ich verwende RMI. Leider funktioniert dies nicht so wie gewollt... :confused:

Meine Klassen:

RC erweitert das Remote Interface für eine RMI Schnittstelle
Code:
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface RC extends Remote {
	public int getI() throws RemoteException;
	public void start() throws RemoteException;
}

Die Klasse NumberThread inkrementiert jede Sekunde die Variable i und implementiert die Interfaces RC (siehe oben) und Runnable. Hier möchte ich
die Variable i auslesen...
Code:
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class NumbersThread extends UnicastRemoteObject implements Runnable, RC {

	private static final long serialVersionUID = 1234L;
	private int i = 0;
	
	public NumbersThread() throws RemoteException {
		super();
	}
	
	public void run() {
		System.out.println("START");
		do {
			System.out.println(i++);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {break;}
		} while(true);
		System.out.println("STOP");
	}
	
	public int getI() throws RemoteException{
		return i;
	}

	public void start() throws RemoteException{
		Thread t = new Thread(new NumbersThread());
		t.start();
	}
}

Die Klasse RCServer bindet die RMI Schnittstelle ein...
Code:
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

public class RCServer {

	public static void main(String[] args) throws RemoteException, MalformedURLException, AlreadyBoundException {
		LocateRegistry.createRegistry(1099);
		NumbersThread n = new NumbersThread();
		n.start();
		Naming.bind("rmi://localhost/numbers", n);
	}
}

Die KLasse RCClient sollte die vom Thread hochgezählte Variable i auslesen...
Code:
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class RCClient {
	
	public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException {
		RC rc = (RC) Naming.lookup("rmi://localhost/numbers");
//		rc.start();
		System.out.println(rc.getI());
	}

}

Mein Problem: Beim laufen der Klasse RCClient erhalte als Ausgabe immer 0! :mad:

Über Hilfe wär ich sehr Dankbar....
Es kann auch eine andere Idee sein, wie ich den Thread auslesen könnte (anstelle von RMI...)
 
Hallo nochmals,
Hab inzwischen mein Problem gelöst... (!)

Für alle die es interessiert: Hier meine Lösung...

Ich habe den Thread im Server gestartet...

Code:
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface RC extends Remote {
	public int getI() throws RemoteException;
}

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

public class WriteNumbers extends UnicastRemoteObject implements Runnable, RC {

	private static final long serialVersionUID = 5632216182533216566L;
	private int i = 0;
	
	public WriteNumbers() throws RemoteException {
		super();	}
	
	public void run() {
		System.out.println("START");
		do {
			System.out.println(i++);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				break;
			}
		} while(true);
		System.out.println("STOP");
	}
	
	public int getI() throws RemoteException{
		return i;
	}
}

Code:
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

public class RCServer {
	public static void main(String[] args) throws RemoteException, MalformedURLException, AlreadyBoundException {
		LocateRegistry.createRegistry(1099);
		WriteNumbers wn = new WriteNumbers();
		Thread t = new Thread(wn);
		t.start();
		Naming.bind("rmi://localhost/numbers", wn);
	}
}

Code:
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class RCClient {
	public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException {
		RC rc = (RC) Naming.lookup("rmi://localhost/numbers");
		System.out.println(rc.getI());
	}
}
 
Zurück