AiLeVaDiSi FoRuM  

Go Back   AiLeVaDiSi FoRuM > BILGISAYAR > Tutorials

Tutorials Bilgisayar İngilizce Eğitim Setleri

 

Cevapla
 
LinkBack Seçenekler Stil
Alt 08-03-2012, 04:48 PM   #1 (permalink)
HaYaTıN iÇiNDeN
JQuLK - ait Kullanıcı Resmi (Avatar)
AileVadisi.NeT
Üyelik tarihi: Dec 2009
Bulunduğu yer: -
Konular :
Mesajlar: 35,260
Rep Puanı : 1001
Rep Derecesi : JQuLK has much to be proud ofJQuLK has much to be proud ofJQuLK has much to be proud ofJQuLK has much to be proud ofJQuLK has much to be proud ofJQuLK has much to be proud ofJQuLK has much to be proud ofJQuLK has much to be proud of
İletisim :
Standart The 10 minutes “Getting started with RMI” tutorial


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:
  1. It is your responsibility to make the remote objects thread safe
  2. This example is not production code. For example there is no exception handling
  3. It is possible to download the shared code in api.jar at runtime from an HTTP server but this is beyond the purpose of this short tutorial






imza
Ö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.
JQuLK isimli Üye şimdilik offline konumundadır   Alıntı ile Cevapla
Reklamlar Bağışlanmaktadır
Cevapla

Bookmarks

Etiketler
“getting, minutes, rmi”, started, tutorial

Seçenekler
Stil

Yetkileriniz
Konu Acma Yetkiniz Yok
Cevap Yazma Yetkiniz Yok
Eklenti Yükleme Yetkiniz Yok
Mesajınızı Değiştirme Yetkiniz Yok

BB code is Açık
Smileler Açık
[IMG] Kodları Açık
HTML-Kodu Kapalı
Trackbacks are Açık
Pingbacks are Açık
Refbacks are Açık

Forum Şartları


Tüm Zamanlar GMT +3 Olarak Ayarlanmış. Şuanki Zaman: 10:16 AM.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.5.2
aBSHeLL
Protected by CBACK.de CrackerTracker
Abshell-AileVadisi

Linkler

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305