Title: Java Spaces
1Java Spaces
2Definition 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.
3JavaSpaces 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
4Example
?
5Services provided by Jini
Lookup Services (reggie)
rmid
JavaSpace (outriggger)
Fiddler Mercury Norm
Transaction Services (mahalo.jar)
HTTP-Server (tools.jar)
6What 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
7The write and read
write
Space
read
A copy
8The take
write
Space
X
take
Exists only here
9How 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
10How 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)
11Example 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) -
12Storing 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()
-
13The 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).
14Retrieving 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_V
ALUE) - System.out.println("Got
"result.toString()) - catch (Exception e) e.printStackTrace()
-
15Rules 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
16Taking 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
17Synchronizing clients
- 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
Ticket Dispenser
number
Service Number
number
See TicketInit.java Customer.java
18And of courseChatting
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
Channel
position
See Tail.java, MessageChannel.java,
CreateChannel.java, ChatSpace.java
19Distributed Events
3- an object matching the template enters the
space
4- the listener is nofied
2- Notify space about interest With a template
1- Create a Listener object
20How 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()
-
21A 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()
-
-
22Calling 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)
23Transactions
- 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
24An 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