Java RMI distributed program development example

Source: Internet
Author: User

Java RMI distributed program development example

 

Author: javaboy2012
Email: yanek@163.com
QQ: 1046011462

 

I. Server Side

Interface Definition: note that the remote interface must be inherited.

Package com. Yanek. RMI. server;

Import java. RMI. Remote;
Import java. RMI. RemoteException;
Import java. util. List;

Public interface channelmanager extends remote {
 
Public list <channel> getchannels () throws RemoteException;

}

Objects transmitted by the interface: note that the serializable interface must be implemented.

Package com. Yanek. RMI. server;

Import java. Io. serializable;

Public class channel implements serializable {
 
 
Private int ID;
Private string name;
Public int getId (){
Return id;
}
Public void setId (int id ){
This. id = id;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public Channel (int id, String name ){
Super ();
This. id = id;
This. name = name;
}
 

}

 

Interface implementation: Pay attention to inherit the UnicastRemoteObject class

Package com. yanek. rmi. server;

Import java. rmi. RemoteException;
Import java. rmi. server. UnicastRemoteObject;
Import java. util. arraylist;
Import java. util. date;
Import java. util. List;

Public class channelmanagerimpl extends unicastremoteobject implements channelmanager {

/**
*
*/
Private Static final long serialversionuid = 1l;

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){



Try {
Channelmanager CM = channelmanagerimpl. getinstance ();

Channelmanager cm1 = channelmanagerimpl. getinstance ();
Channelmanager cm2 = channelmanagerimpl. getinstance ();
System. Out. println (cm1 );
System. Out. println (cm2 );
System. Out. println (CM );

List <channel> channels = cm. getchannels ();

For (INT I = 0; I <channels. Size (); I ++)
{
Channel c = channels. Get (I );

System. Out. println (C. GETID () + "-" + C. getname ());
}

} Catch (RemoteException e ){
E. printstacktrace ();
}

}
 
Private Static final object lock = new object ();
Private Static channelmanager instance;
 
Public static channelmanager getinstance () throws RemoteException {
If (instance = NULL ){
Synchronized (LOCK ){
If (instance = NULL)
Instance = new channelmanagerimpl ();
}
}
Return instance;
}
 
Private channelmanagerimpl () throws RemoteException {
}

@ Override
Public list <channel> getchannels () throws RemoteException {
List <channel> channels = new arraylist <channel> ();
Channels. Add (new channel (1, "Java "));
Channels. add (new Channel (2, "php "));
Channels. add (new Channel (3, "C "));
Channels. add (new Channel (4, "ASP "));
System. out. println (new Date () + "getChannels method called! ");
Return channels;
}

}

Service Startup class:

Package com. yanek. rmi. server;

Import java.net. MalformedURLException;
Import java. rmi. Naming;
Import java. rmi. RMISecurityManager;
Import java. rmi. RemoteException;
Import java. rmi. registry. LocateRegistry;

Public class RMIServer {

/**
* @ Param args
*/
Public static void main (String [] args ){


System. Out. println ("RMI server starting ...");


If (system. getsecuritymanager () = NULL ){
// System. setsecuritymanager (New rmisecuritymanager ());
}


Try {
Locateregistry. createregistry (1099 );
Try {
Naming. rebind ("channelmanager", channelmanagerimpl. getinstance ());
} Catch (malformedurlexception e ){
E. printstacktrace ();
}
} Catch (RemoteException e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

System. out. println ("RMI Server ready ...");

}

}

Pack the above four classes as server. jar

Package the ChannelManager and Channel classes as server_4client.jar.

Ii. Client

Package com. yanek. rmi. client;

Import java. rmi. Naming;
Import java. rmi. RMISecurityManager;
Import java. rmi. Remote;
Import java. rmi. RemoteException;
Import java. util. List;

Import com. yanek. rmi. server. Channel;
Import com. yanek. rmi. server. ChannelManager;

Public class ClientUtil {

/**
* @ Param args
*/
Public static void main (String [] args ){

ChannelManager cm = (ChannelManager) ClientUtil. renewRMI ("ChannelManager ");

List <channel> channels;
Try {
Channels = cm. getchannels ();

For (INT I = 0; I <channels. Size (); I ++ ){
Channel c = channels. Get (I );

System. Out. println (C. GETID () + "-" + C. getname ());
}

} Catch (RemoteException e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

 

}

Public static remote renewrmi (string name ){
Try {

If (system. getsecuritymanager () = NULL ){
// System. setsecuritymanager (New rmisecuritymanager ());
}
Return naming. Lookup ("RMI: // 192.168.136.1: 1099/" + name );
} Catch (exception re ){
Throw new illegalstateexception ("bind" + name + "failure .");
}

}

}

 

Package com. Yanek. RMI. client;

Import java. RMI. RemoteException;
Import java. util. arraylist;
Import java. util. List;

Import com. Yanek. RMI. server. channel;
Import com. Yanek. RMI. server. channelmanager;

Public class clienttest {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){


ChannelManager cm = (ChannelManager) ClientUtil. renewRMI ("ChannelManager ");

List <Channel> channels = new ArrayList <Channel> ();
Try {
Channels = cm. getChannels ();

For (int I = 0; I <channels. size (); I ++ ){
Channel c = channels. get (I );

System. out. println (c. getId () + "-" + c. getName ());
}

} Catch (RemoteException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}

}

}

Package the above two classes as client. jar

Iii. Release and deployment

Server Release: reference the server. jar package and execute the main method of the rmiserver class.

Client call: Reference server_4client.jar and client. Jar

 

The following code calls the batch processing script in bat mode in Windows:

Client. bat

@ Echo off

Set CP = config

For % I in (Lib \ *. Jar) Do call A. Bat % I

Set CP = % CP %;

Echo % CP %;

Java-CP % com. Yanek. RMI. Client. clienttest

Pause

Server. bat

@ Echo off

Set CP = config

For % I in (Lib \ *. Jar) Do call A. Bat % I

Set CP = % CP %;

Echo % CP %;

Java-CP % com. Yanek. RMI. server. rmiserver

Pause

 

Auxiliary File A. bat

Set CP = % CP %; % 1

Server startup

 

 

Client call:

 

 

 

 

Code and deployment file: http://download.csdn.net/detail/5iasp/5093357 (points are not required)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.