RMI, Serialisierung und Marshalling

nero110

Mitglied
Hi zusammen,

ich habe hier eine Client / Server Architektur, bei der ein Client mittels Remote Method Invocation (RMI) auf Remote Methode eines Servers zugreift und dort Datenbankabfragen durchführt.

Die Rückgabewerte der Remote Methoden sind vom Typ String[], leider erhalte ich hierbei auf der Client Seite permanent NullPointerExceptions. Ich gehe davon aus, dass da serialisiert werden muss. Aber macht RMI das durch Marshalling nicht automatisch?
Testweise: Wenn ich einen Rückgabewert des Typs String habe, dann klappt es.

Jemand eine Idee, was ich machen soll?

Danke,

Chris
 
Hallo!

Also bei mir funktionietr die Übertragung von String[] wunderbar:
Als Basis für das Beispiel siehe hier:
http://www.tutorials.de/tutorials179039.html&highlight=RMI

Unser "erweitertes" Interface:
Code:
package de.tutorials.rmi;

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

public interface ITimeService extends Remote {
	public long getServerTime() throws RemoteException;

	public String[] getData() throws RemoteException;
}

Der Server:
Code:
package de.tutorials.rmi.server;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import de.tutorials.rmi.ITimeService;

public class Server implements ITimeService {

	public final static int STD_RMI_PORT = 1099;

	public Server() {
	}

	private void start() {
		try {
			ITimeService stub = (ITimeService) UnicastRemoteObject
					.exportObject(this, 0);

			Registry registry = LocateRegistry.getRegistry("127.0.0.1",
					STD_RMI_PORT);

			System.out.println(registry);

			registry.rebind("TIMESERVICE", stub);

		} catch (RemoteException e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		new Server().start();
	}

	public long getServerTime() throws RemoteException {
		return System.currentTimeMillis();
	}

	/* (non-Javadoc)
	 * @see de.tutorials.rmi.ITimeService#getData()
	 */
	public String[] getData() throws RemoteException {
		// TODO Auto-generated method stub
		return new String[]{"Hallo","Welt"};
	}
}

Der Client
Code:
package de.tutorials.rmi.client;

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

import de.tutorials.rmi.ITimeService;

public class Client {

	public final static int STD_RMI_PORT = 1099;

	public static void main(String[] args) {
		new Client().start();
	}

	private void start() {
		try {
			Registry registry = LocateRegistry.getRegistry("127.0.0.1",
					STD_RMI_PORT);
			ITimeService timeService = (ITimeService) registry
					.lookup("TIMESERVICE");
			System.out.println(new Date(timeService.getServerTime()));
			String[] data = timeService.getData();
			for (int i = 0; i < data.length; i++) {
				System.out.println(data[i]);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
	}
}

Die Ausgabe:
Code:
Fri Jan 07 00:45:54 CET 2005

Hallo

Welt

HTH, Gruß Tom
 
Zurück