Java Spaces - PowerPoint PPT Presentation

About This Presentation
Title:

Java Spaces

Description:

... technology acts as a virtual space between providers and ... The most common notion people have from Jini is that ... can I create a JavaSpace ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 25
Provided by: vicg9
Category:

less

Transcript and Presenter's Notes

Title: Java Spaces


1
Java Spaces
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
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
Example
?
5
Services provided by Jini
Lookup Services (reggie)
rmid
JavaSpace (outriggger)
Fiddler Mercury Norm
Transaction Services (mahalo.jar)
HTTP-Server (tools.jar)
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
The write and read
write
Space
read
A copy
8
The take
write
Space
X
take
Exists only here
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
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
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
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
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
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_V
    ALUE)
  • System.out.println("Got
    "result.toString())
  • catch (Exception e) e.printStackTrace()

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
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
Synchronizing 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
18
And 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
19
Distributed 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
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
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
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
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
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
Write a Comment
User Comments (0)
About PowerShow.com