La descarga está en progreso. Por favor, espere

La descarga está en progreso. Por favor, espere

Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Módulo 9: Desarrollo de Aplicaciones.

Presentaciones similares


Presentación del tema: "Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Módulo 9: Desarrollo de Aplicaciones."— Transcripción de la presentación:

1 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 1 Java Spaces

2 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 2 Definition according to sun  JavaSpacesTM technology is a simple unified mechanism for dynamic communication, coordination, and sharing of objects between JavaTM technology-based network resources like clients and servers. In a distributed application, JavaSpaces technology acts as a virtual space between providers and requesters of network resources or objects. This allows participants in a distributed solution to exchange tasks, requests and information in the form of Java technology-based objects. JavaSpaces technology provides developers with the ability to create and store objects with persistence, which allows for process integrity.

3 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 3 JavaSpaces is part of the Jini spec.  Jini is a collection of service specifications  They help services to find each other on the network  The most common notion people have from Jini is that enables jini powered machines to be attached to the network and automatically find out which services are available, as well as post its own services to the rest  Jini based machines are servers and client at the same time

4 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 4 Example ? ?

5 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 5 Services provided by Jini Lookup Services (reggie) rmid HTTP-Server (tools.jar) Transaction Services (mahalo.jar) JavaSpace (outriggger) Fiddler Mercury Norm

6 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 6 What does JavaSpaces provides  A space in which objects can be stored in, retrieved from, copied from.  A set of methods: write, read, take, (readIfExists, takeIfExists)  A mechanism to ensure completeness of transactions  An event notify mechanism

7 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 7 The write and read write Space read A copy

8 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 8 The take X write Space take Exists only here

9 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 9 How can I create a JavaSpace in a host  A JavaSpace can be created by starting the outrigger service of Jini with a certain name as parameter. This will be the name of the space.  Before starting the outrigger it is necessary to start:  The http server tools.jar  An rmid  The lookup server rigger  The transaction server mahalo

10 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 10 How can I write objects into a Space  User defined object classes must declare they implement the interface Entry.  This does not mean it has to implement any methods. It is just a tag to tell the system an object of this class can be written in a JavaSpace  Fields (variables) in this class should be objects. If primitives are needed (such as integers) use wrappers  Integer i = new Integer(4)

11 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 11 Example of an Entry class import net.jini.core.entry.Entry; public class Message implements Entry { public String content; public Integer counter; public Message() { //this is mandatory !!!!! } public Message(String content, int initVal) { this.content = content; counter = new Integer(initVal); } public String toString() { return content + counter + " times."; } public void increment() { counter = new Integer(counter.intValue() + 1); }

12 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 12 Storing objects in a space import net.jini.core.lease.Lease; import net.jini.space.JavaSpace; import java.io.*; public class WriteMessages { public static void main(String[] args) { try { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); JavaSpace space = SpaceAccessor.getSpace(); System.out.print("Message ? "); String m = in.readLine(); Message2 msg = new Message2(m,0); space.write(msg, null, Lease.FOREVER); } catch (Exception e) {e.printStackTrace(); } }

13 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 13 The write sentence space.write(msg, null, Lease.FOREVER);  The Parameters: an Entry object (the Message), a transaction object (null for now) and a long (leasing time in milliseconds, Lease.FOREVER is the value to make it permanent).

14 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 14 Retrieving objects in a space import java.io.*; import net.jini.core.lease.Lease; import net.jini.space.JavaSpace; public class ReadMessage { public static void main(String[] args) { try { Message2 template; JavaSpace space = SpaceAccessor.getSpace(); BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Message (**=null)? "); String m = in.readLine(); template = new Message2(m); Message2 result = (Message2)space.read(template,null,Long.MAX_VALUE); System.out.println("Got "+result.toString()); } catch (Exception e) {e.printStackTrace(); }

15 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 15 Rules for retrieving objects  The object must match the template according to the class and the content  Null references in the template act as wildcards  There is no rule for deciding which of all the matching objects in the space matching the template will be retrieved  The parameters: a template object, a transaction (null) and a waiting time before the read sentence gives up if it does not find an object which matches the template. Long.MAX_VALUE is for waiting forever

16 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 16 Taking objects  It works just like read but the object is deleted from the space  In order to avoid deadlocks or waiting, it is possible to use readIfExists and takeIfExists  Their structure is the same as read and take, but they will return immediately if they do not find any matching object in the space  The SpaceAccessor class with the getSpace method is not standard, see SpaceAccessor.java

17 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 17 Synchronizing clients Ticket Dispenser number Customer 1- takes a number, 2- increments Ticket Disp. 3- takes the service object when it has this number 4- waits for being served 5-Increments service number A client will be served Only if it has the service number Service Number number See: TicketInit.java Customer.java

18 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 18 And of course…Chatting position A Tail object indicates which is the number of the last message Every message has a content And a number The channel identifies a „chat.-thred“ Tail MessageChat number Channel MessageChat number Channel MessageChat number Channel See: Tail.java, MessageChannel.java, CreateChannel.java, ChatSpace.java

19 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 19 Distributed Events 1- Create a Listener object 2- Notify space about interest With a template 3- an object matching the template enters the space 4- the listener is nofied

20 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 20 How to write a listener import java.rmi.server.*; import java.rmi.RemoteException; import net.jini.core.event.*; import net.jini.space.JavaSpace; public class Listener implements RemoteEventListener { private JavaSpace space; public Listener(JavaSpace space) throws RemoteException { this.space = space; UnicastRemoteObject.exportObject(this); } public void notify(RemoteEvent ev) { Message template = new Message(); try { Message result = (Message)space.read(template, null, Long.MAX_VALUE); System.out.println(result.content); } catch (Exception e) { e.printStackTrace(); }

21 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 21 A program that listens import net.jini.core.lease.Lease; import net.jini.space.JavaSpace; public class HelloWorldNotify { public static void main(String[] args) { JavaSpace space = SpaceAccessor.getSpace(); try { Listener listener = new Listener(space); Message template = new Message(); space.notify(template, null, listener, Lease.FOREVER, null); } catch (Exception e) { e.printStackTrace(); }

22 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 22 Calling the notify method  After this, if any program writes a message (no matter the content) the Listener object of the HelloWorldNotify program will be „notified“, this means, the notify method will be called Message msg = new Message(); msg.content = "Hello World"; space.write(msg, null, Lease.FOREVER);

23 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 23 Transactions  A transaction is a set of instructions which should be all executed atomically or none at all.  In JavaSpaces this refers to write, read, and take instructions that can be scheduled to be performed in this way.  For this, a transaction object should be created and passed as an argument in every instruction which belongs to the transaction  After all write, read, take, etc.instructions with (the same) transaction are “executed”, a commit statement will either perform all the operations or no one.  In the latter case, an exception is thrown

24 Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Email: victoria.gaete @die.uchile.cl2000/1 Módulo 9: Desarrollo de Aplicaciones en Redes de Computadores 24 An example JavaSpace space = SpaceAccessor.getSpace(); TransactionManager mgr = TransactionManagerAccessor.getManager(); //get a reference to the transaction manager service Try { Transaction.Created trc = TransactionFactory.create(mgr, 3000); Transaction txn = trc.transaction; SharedVar template = new SharedVar(url); SharedVar counter = (SharedVar) space.take(template, txn, Long.MAX_VALUE); counter.increment(); space.write(counter, txn, Lease.FOREVER); txn.commit(); } catch (Exception e) { System.err.println("Transaction failed"); return; }


Descargar ppt "Universidad de Chile - Tupper 2007, Santiago - Fono: 678 4888 - Fax: 698 8427 - Módulo 9: Desarrollo de Aplicaciones."

Presentaciones similares


Anuncios Google