Пишу клиент-серверное приложение, использую rmi, наткнулся на использование rmi callback, нашел в интернете кучу примеров, с подключением потоков задач, били даже с пуллом соединений, но на практике ни один из них запустить и опробовать не смог, на всех вылетает ошибка(jvm 1.6): java.rmi.StubNotFoundException: Stub class not found: server.RMIServer_Stub; nested exception is: java.lang.ClassNotFoundException: server.RMIServer_Stub не найден класс RMIServer_Stub, что такое _Stub? Если есть другой - работающий пример пожалуйста поделитесь, а то время потратил уйма, а ничего рабочего не нашел | Код | // Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-mml2/magercises/RMICallback/Solution/RMIServer.java#2 $ import interfaces.TimeMonitor; import interfaces.TimeServer; import java.net.*; import java.io.*; import java.util.Date;
import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.LocateRegistry;
/** * RMIServer acts as a server for the remote "TimeServer" service. * * RMIServer starts execution at the standard entry point "public static void main"; * It creates an instance of itself and continues processing in the constructor. * */
public class RMIServer implements Remote, TimeServer { private static final int PORT = 10005;
// // -> Change the name to your own computer name // private static final String HOST_NAME = "127.0.0.1";
// Instance of ourselves private static RMIServer rmi;
public static void main ( String[] args ) { // We need to set the security manager to the RMISecurityManager System.setSecurityManager( new RMISecurityManager() );
try { rmi = new RMIServer();
LocateRegistry.createRegistry( PORT );
System.out.println( "Registry created" );
UnicastRemoteObject.exportObject( ((TimeServer)rmi) );
Naming.rebind( "//" + HOST_NAME + ":" + Integer.toString( PORT ) + "/" + "TimeServer", rmi );
System.out.println( "Bindings Finished" );
System.out.println( "Waiting for Client requests" );
} catch ( java.rmi.UnknownHostException uhe ) { System.out.println( "The host computer name you have specified, " + HOST_NAME + " does not match your real computer name." );
} catch ( RemoteException re ) { System.out.println( "Error starting service" ); System.out.println( "" + re ); } catch ( MalformedURLException mURLe ) { System.out.println( "Internal error" + mURLe ); }
} // main
public void registerTimeMonitor( TimeMonitor tm ) { System.out.println( "Client requesting a connection" );
TimeTicker tt;
// Insert two lines of code that complete the implementation of this method. // 1. Create a new TimeTicker object // 2. Start this new object running. tt = new TimeTicker( tm ); tt.start();
System.out.println( "Timer Started" ); }
} // class RMIServer
class TimeTicker extends Thread {
private TimeMonitor tm;
TimeTicker( TimeMonitor tm ) { this.tm = tm; }
public void run() { while ( true ) { try { sleep( 2000 );
// Insert a single line of code that calls the tellMeTheTime // method of the TimeMonitor interface. // Pass it a new instance of the Date class. tm.tellMeTheTime( new Date() ); } catch ( Exception e ) { stop(); } } } }
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-mml2/magercises/RMICallback/Solution/TimeMonitor.java#2 $ import java.rmi.*; import java.util.Date;
// Create the interface TimeMonitor. // It contains one method tellMeTheTime. // This method accepts a parameter of type Date // and returns void. public interface TimeMonitor extends java.rmi.Remote { public void tellMeTheTime( Date d ) throws RemoteException; }
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-mml2/magercises/RMICallback/Solution/TimeServer.java#2 $ import interfaces.TimeMonitor; import java.rmi.*;
// Create the interface TimeServer. // It contains one method registerTimeMonitor. // This method accepts a parameter of type TimeMonitor // and returns void.
public interface TimeServer extends java.rmi.Remote { public void registerTimeMonitor( TimeMonitor tm) throws RemoteException;
}
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/RMI-mml2/magercises/RMICallback/Solution/Applet1.java#2 $ import interfaces.TimeMonitor; import interfaces.TimeServer; import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.Date; import java.net.URL; import java.rmi.*; import java.rmi.server.*;
public class Applet1 extends Applet
implements TimeMonitor { // Change this to your computer's name private final static String HOST_NAME = "ROSA";
private TimeServer ts;
public void init() { super.init();
uiInit();
try { System.out.println( "Exporting the Applet" ); UnicastRemoteObject.exportObject( this ); URL base = getDocumentBase();
String hostName = base.getHost();
if ( 0 == hostName.length() ) { hostName = HOST_NAME; }
String serverName = "rmi://" + hostName + ":" + getParameter( "registryPort" ) + "/TimeServer" ;
System.out.println( "Looking up TimeService at: " + serverName );
try { ts = (TimeServer)Naming.lookup( serverName ); } catch ( Exception e ) { System.out.println( "" + e ); }
ts.registerTimeMonitor( this );
System.out.println( "We have been registered!" );
} catch ( RemoteException re ) { System.out.println( "" + re ); } }
public void tellMeTheTime( Date d ) { textArea1.appendText( d.toString() + "\n" ); }
public void uiInit() { setLayout(null); resize(456,266); textArea1 = new java.awt.TextArea(); textArea1.reshape(36,24,252,170); add(textArea1);
button1 = new java.awt.Button("Clear"); button1.reshape(324,36,72,24); add(button1);
button1.addActionListener ( new ActionListener() {
public void actionPerformed(ActionEvent event) { textArea1.setText(""); } } );
}
java.awt.TextArea textArea1; java.awt.Button button1; }
|
этот пример я взял с сановского сервера.... http://java.sun.com/developer/onlineTraini...back/index.htmlЭто сообщение отредактировал(а) cube - 9.6.2009, 14:47
|