Tutorials to .com

Tutorials to .com » Jsp » Skills » Getting Started javaRMI procedures and configuration

Getting Started javaRMI procedures and configuration

Print View , by: iSee ,Total views: 15 ,Word Count: 911 ,Date: Fri, 21 Aug 2009 Time: 4:05 AM

This example contains a total of five documents, namely HelloClient, Hello, HelloServer, Configur, config.properties




-------------------------------------------------- ------------------------------


/ * Hello.java * /
/ *
* Created on 2004-11-10
*
* /
package test.rmi;

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

/ **
* @ Author dogcome
*
* <p> Remote method invocation interface, the definition of a remote method call </ p>
* /
public interface Hello extends Remote (

String sayHello () throws RemoteException;

)




-------------------------------------------------- ------------------------------


/ * HelloServer * /
/ *
* Created on 2004-11-10
*
* /
package test.rmi;

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

/ **
* @ Author dogcome
*
* <p> server-side, the realization of the Hello interface method for remote method calls the specific business logic </ p>
* /
public class HelloServer extends UnicastRemoteObject implements Hello (
String name;
public HelloServer (String s) throws RemoteException (
super ();
name = s;
)

public String sayHello () throws RemoteException (
/ / TODO Auto-generated method stub
return "Hello world!";
)

public static void main (String [] args) (
/ ** To add the following sentence, you need to carry out the certification authority, an increase of. Policy document
* The command line and use the following format
* Java-Djava.security.policy = java.policy test.rmi.HelloServer
* /
/ * System.setSecurityManager (new RMISecurityManager ());*/
Registry registry = null;
try (
/ ** Start the registration server, the use of this statement is no longer required on the command line environment
* Start the registry service
* /
registry = LocateRegistry.getRegistry ();
/ * If there is no access to connectivity, the sentence will be thrown back in the associated post-harvest handling * /
registry.list ();
System.out.println ( "Register the exist server!"); / / $ NON-NLS-1 $
) Catch (RemoteException re) (
try (
int port = Integer.parseInt (Configur
. getString ( "HelloServer.RegistryServerPort")); / / $ NON-NLS-1 $
registry = LocateRegistry.createRegistry (port);
System.out.println ( "Create Registry Server!"); / / $ NON-NLS-1 $
) Catch (Exception e) (
e.printStackTrace ();
)
)
try (
HelloServer helloServer = new HelloServer ( "Hello");
registry
. rebind (
Configur.getString ( "HelloServer.HelloServerName"), helloServer); / / $ NON-NLS-1 $
System.out.println ( "HelloServer server start!"); / / $ NON-NLS-1 $
) Catch (Exception e) (
e.printStackTrace ();
)
)
)




-------------------------------------------------- ------------------------------


/ * HelloClient * /
/ *
* Created on 2004-11-10
*
* /
package test.rmi;

import java.rmi .*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

/ **
* @ Author dogcome
*
* <p> Client server need to register and use the jndi </ p>
* /
public class HelloClient (

public static void main (String [] args) (
/ ** To add the following sentence, you need to carry out the certification authority, an increase of. Policy document
* The command line and use the following format
* Java-Djava.security.policy = java.policy test.rmi.HelloServer
* /
/ * System.setSecurityManager (new RMISecurityManager ());*/
try (
/ * Register server * /
String hostName = Configur.getString ( "HelloServer.RegistryServerName");
int port = Integer.parseInt (Configur.getString ( "HelloServer.RegistryServerPort"));
Registry registry = LocateRegistry.getRegistry (hostName, port);

Hello hello = (Hello) registry.lookup (Configur.getString ( "HelloServer.HelloServerName"));
String message = hello.sayHello ();
System.out.println (message);

) catch (Exception e) (
e.printStackTrace ();
)
)
)




-------------------------------------------------- ------------------------------


/ *
* Created on 2004-11-12
*
* /
package test.rmi;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

/ **
* @ Author dogcome
*
* <p> Required to obtain system configuration information, corresponding to the configuration file name config.properties </ p>
* /
public class Configur (
private static final String BUNDLE_NAME = "test.rmi.config";

private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
. getBundle (BUNDLE_NAME);

private Configur () (
)

/ **
Name * <p> through access to key information about configuration file </ p>
* @ Param key key name
* @ Return String Profile Information
* /
public static String getString (String key) (
try (
return RESOURCE_BUNDLE.getString (key);
) Catch (MissingResourceException e) (
return '!' + key + '!';
)
)
)



-------------------------------------------------- ------------------------------
# config.properties
HelloServer.RegistryServerPort = 1111
HelloServer.HelloServerName = HelloServer
HelloServer.RegistryServerName = 127.0.0.1



-------------------------------------------------- ------------------------------


Rmic test.rmi.HelloServer the first to use a command to generate the stub and skel need two types of documents
HelloServer server and then run java test.rmi.HelloServer
Finally, run java test.rmi.HelloClient client HelloClient
Screen output HelloWorld, so that one of the most simple remoting rmi successful




Java skills and code Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.