![]() |
|
|||||||
| Tutorials Bilgisayar İngilizce Eğitim Setleri |
![]() |
|
|
LinkBack | Seçenekler | Stil |
|
|
#1 (permalink) |
![]() Üyelik tarihi: Dec 2009
Bulunduğu yer: -
Konular :
Mesajlar: 35,260
Rep Puanı : 1001
Rep Derecesi :
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() İletisim :
|
http://www.ailevadisi.net Since Java 5 working with RMI (Remote Method Invocation) is very easy. You don’t need the rmic compiler unless you work with legacy RMI clients. Now stubs are generated automatically at runtime.
As a result, writing distributed or client-server applications becomes trivial. Let’s see a very minimalistic example. Our scenario will have a server sharing an object via RMI and a client calling the shared instance. First we need to define the interface for our remote object. In our case it is a simple interface for incrementing a shared counter. Api.java 1.package com.littletutorials.rmi.api; 2. 3.import java.rmi.*; 4. 5.public interface Api extends Remote { 6.public Data incrementCounter(Data value) throws RemoteException; 7.} Notice the fact that our interface implements java.rmi.Remote to mark the interface as available remotely. Also the interface uses a Data class to hold data. The purpose of this class in this tutorial is only to show how to send your own objects across the wire. In itself it doesn’t add any value over an int. Data.java 01.package com.littletutorials.rmi.api; 02. 03.import java.io.*; 04. 05.public class Data implements Serializable { 06.private static final long serialVersionUID = 1L; 07.private int value; 08. 09.public Data(int value) { 10.this.value = value; 11.} 12. 13.public int getValue() { 14.return value; 15.} 16. 17.public void setValue(int value) { 18.this.value = value; 19.} 20.} The Data class implements java.io.Serializable to specify that its instances can be sent across the network. And here is the implementation for our shared counter: ApiImpl.java 01.package com.littletutorials.rmi.server; 02. 03.import java.rmi.*; 04.import java.rmi.server.*; 05.import com.littletutorials.rmi.api.*; 06. 07.public class ApiImpl extends UnicastRemoteObject implements Api { 08.private static final long serialVersionUID = 1L; 09.private int counter = 0; 10. 11.public ApiImpl() throws RemoteException { 12.super(); 13.} 14. 15.@Override 16.public synchronized Data incrementCounter(Data value) throws RemoteException { 17.counter += value.getValue(); 18.return new Data(counter); 19.} 20.} Notice that I decided to extend UnicastRemoteObject to have my object automatically exported for RMI access with JRMP (Java Remote Method Protocol). If you don’t want to extend this class or you cannot because you need to extend some other class you could call: 1.UnicastRemoteObject.exportObject(this); But keep in mind, in this case you have to generate your stub using rmic! Now it is time to write the server application Server.java 01.package com.littletutorials.rmi.server; 02. 03.import java.rmi.*; 04.import java.rmi.registry.*; 05. 06.import com.littletutorials.rmi.api.*; 07. 08.public class Server { 09.private static final int PORT = 1099; 10.private static Registry registry; 11. 12.public static void startRegistry() throws RemoteException { 13.// create in server registry 14.registry = java.rmi.registry.LocateRegistry.createRegistry(PO RT); 15.} 16. 17.public static void registerObject(String name, Remote remoteObj) 18.throws RemoteException, AlreadyBoundException { 19.registry.bind(name, remoteObj); 20.System.out.println("Registered: " + name + " -> " + 21.remoteObj.getClass().getName() + "[" + remoteObj + "]"); 22.} 23. 24.public static void main(String[] args) throws Exception { 25.startRegistry(); 26.registerObject(Api.class.getSimpleName(), new ApiImpl()); 27.Thread.sleep(5 * 60 * 1000); 28.} 29.} Usually most of the examples require the rmiregistry to be started as a separate process. But, since this tutorial is about simpler ways, we create the RMI registry “in-process“. Then we register an instance of the API implementation with our registry. As instance name we use the API interface class name. The only piece of the puzzle remains the client code: Client.java view sourceprint? 01.package com.littletutorials.rmi.client; 02. 03.import java.rmi.registry.*; 04.import com.littletutorials.rmi.api.*; 05. 06.public class Client { 07.private static final String HOST = "localhost"; 08.private static final int PORT = 1099; 09.private static Registry registry; 10. 11.public static void main(String[] args) throws Exception { 12.registry = LocateRegistry.getRegistry(HOST, PORT); 13.Api remoteApi = (Api) registry.lookup(Api.class.getSimpleName()); 14.for (int i = 1; i <= 100; i++) { 15.System.out.println("counter = " + 16.remoteApi.incrementCounter(new Data(1)).getValue()); 17.Thread.sleep(100); 18.} 19.} 20.} An this is it. You can now start the server and the clients! To deploy this code on different machines you need to create 3 jar files: api.jar: Api.java, Data.java server.jar: Server.java, ApiImpl.java client.jar: Client.java On the server machine the class path will contain api.jar and server.jar On the client machine the class path will contain api.jar and client.jar Notes:
Ölüm elbet herkes için gelecektir... Ancak sadece bazıları o an geldiğinde gerçekten yaşamış olarak ölecektir... İşte ölüm ve yaşam arasındaki fark budur. Herkes ölür, ama herkes gerçekten yaşayamaz... -j- JQuLK : Bir savaş sırasında omuzunu dayaya bileceğin güvenilir adamın. |
|
|
|
| Reklamlar Bağışlanmaktadır |
![]() |
| Bookmarks |
| Etiketler |
| “getting, minutes, rmi”, started, tutorial |
| Seçenekler | |
| Stil | |
|
|