l modello Java RMI - Università di Padova

annuncio pubblicitario
Sistemi distribuiti: il modello Java RMI
Architettura del modello - 1
*))
+
SCD
SCD
!""#$%
))
,
)
≠ ))
*))
&
'
(
)
$!
→
[email protected]
!
Architettura del modello - 2
≠ ))
*))
!$!
Architettura del modello - 3
*))
))
)
synchronized
+ ))
→ serializable
#
--
!"
-
$
0/
'
&
%%
. /
.
+ ))
→
→
-
'
#
%
Architettura del modello - 4
Architettura del modello - 5
!
235
(
!
--
,
(
0
.
/
))
67
)
(
23
6
-- -
23
*))
→
)
-
"( *
(
(
+
&
) -
23
)
!
(
6
!
1
UniPD - SCD 2003 - Corso di Sistemi Concorrenti e Distribuiti
4
1
Sistemi distribuiti: il modello Java RMI
Utilizzo del modello - 1
+ ))
Utilizzo del modello - 2
+ ))
java.rmi.Remote
6
*)
,
java.rmi.RemoteException
*)
+ ))
6
java.rmi.UnicastRemoteObject
:
import java.rmi.*;
public interface Echo extends Remote {
String call (String message) throws RemoteException;}
+ ))
&
-
-
+ ))
6
java.rmi.RemoteException
import java.rmi.*;
import java.rmi.server.*
public class EchoServer extends UnicastRemoteObject implements Echo{
public EchoServer( String name ) throws RemoteException {
Naming.rebind (name, this); }
public String call (String message) throws RemoteException {
return "From EchoServer:- message: [" + message + "]"; }
public static void main (String args[]) {
// il main è nel servente, che può anche essere separato dalla
// classe che implementa l’oggetto remoto }
)
+
8
9
Utilizzo del modello - 3
)
-
main
5
Utilizzo del modello - 4
6
+ ))
*)
))
-
)
6
.
rmiregistry/
Naming.bind
)
)
.
)
/
/
.
Naming.rebind
<
6
)
)
)
.
.
)
← = #!
←
2
>
+ ))
))
.
start rmiregistry [portnumber]
rmiregistry [portnumber] &
/
/
→ javac
/
";;/
.
)
rmic
))
!
;
"
Utilizzo del modello - 5
rmic .
+ ))
--
0?
.
Utilizzo del modello - 6
/
/
0
A
)
))
)
6
Naming.lookup (String name)
# +
, '!
((
6
&+
6
-- @
+ ))
)
+
-
-
0) 6
,
>
$!
)
+
5
:6
B
C
javac
:6
))
B
:6
D0 B
:6
D
rmic
B
!
UniPD - SCD 2003 - Corso di Sistemi Concorrenti e Distribuiti
2
Sistemi distribuiti: il modello Java RMI
Utilizzo del modello - 7
rmic .
/
0
Applicazione del modello - 1
(
!$!
5
E
6
6
6
--
<
--
+ ))
-
,
23
5
-
+ ))
)
-
!" !".
6
--
CLASSPATH
6
*
codebase
+
(
(
)
java -Djava.rmi.server.codebase=file://<path>/
&
#
%
Applicazione del modello - 2
+
)
package echo;
public interface Echo extends java.rmi.Remote {
String call (String message) throws java.rmi.RemoteException;
}
,
--
)
package echo; import java.rmi.*; import java.rmi.server.*;
public class EchoServer extends UnicastRemoteObject implements Echo {
public EchoServer( String name ) throws RemoteException {
try { Naming.rebind (name,this); } catch (Exception e) {
System.out.println (“Exception in EchoServer: " + e.getMessage());
e.printStackTrace();} }
public String call (String message) throws RemoteException {
System.out.println("Echo's method call invoked: [" + message + "]");
return "From EchoServer:- Thanks for your message: [" + message + "]"; }
public static void main (String args[]) throws Exception {
if (System.getSecurityManager() == null)
System.setSecurityManager ( new RMISecurityManager() );
String url = "rmi://" + args[0] + "/Echo";
EchoServer echo = new EchoServer (url);
System.out.println("EchoServer ready!"); }
}
→
→
+
)
)
java -Djava.security.policy=<policy_file>
grant {
permission
permission
permission
permission
Esempio - 1
java.io.FilePermission "<<ALL FILES>>", "read";
java.net.SocketPermission "*:1234", "accept, connect, listen, resolve";
java.lang.RuntimePermission "accessClassInPackage.sun.jdbc.odbc";
java.util.PropertyPermission "file.encoding", "read"; };
1
4
Esempio - 2
package echo; import java.rmi.*; import java.rmi.server.*;
public class EchoClient {
public static void main (String args[]) {
int i;
if (System.getSecurityManager() == null)
System.setSecurityManager ( new RMISecurityManager() );
try {
System.out.println ("EchoClient ready!");
String url = "rmi://" + args[0] + "/Echo";
System.out.println ("Looking up remote object " + url + " ...");
Echo echo = (Echo) Naming.lookup (url);
String toMsg = (String) args[1];
for (i = 1; i<6; i++) {
toMsg = toMsg + "-" + i;
System.out.println ("Message " + i + " to Echo: [" + toMsg + "]");
String fromMsg = echo.call (toMsg);
Thread.sleep (2000);
System.out.println ("Message from Echo: \n\t" + fromMsg + "\n"); }
} catch (Exception e) {
System.out.println ("Exception in EchoClient: " + e.getMessage());
e.printStackTrace(); } }
}
Esempio - 3
javac
javac
javac
rmic
-d
-d
-d
-d
.
.
.
.
Echo.java
EchoServer.java
EchoClient.java
echo.EchoServer
F
0)
6
EchoServer_Skel.class
EchoServer_Stub.class
localhost
-
!#%
rmiregistry 1234 &
-
+
java -classpath .. -Djava.security.policy=pol.policy
echo.EchoServer localhost:1234
-
+
java -classpath .. -Djava.security.policy=pol.policy
echo.EchoClient localhost:1234 Initial_Message
8
UniPD - SCD 2003 - Corso di Sistemi Concorrenti e Distribuiti
9
3
Scarica