The Artima Developer Community
Sponsored Link

Jini Seminar by Bill Venners
JavaSpaces
Lecture Handout

Agenda


JavaSpace Service


The JavaSpace Interface



Four Operations


Entries


A ChatMessage Entry

   1 // In file
   2 // JavaSpaces/ex1/com/artima/chat/entry/ChatMessage.java
   3 package com.artima.chat.entry;
   4
   5 import net.jini.entry.AbstractEntry;
   6
   7 public class ChatMessage extends AbstractEntry {
   8
   9     public String chatRoom;
  10     public String author;
  11     public Long position;
  12     public String content;
  13
  14     public ChatMessage() {
  15     }
  16
  17     public ChatMessage(String chatRoom, String author,
  18         Long position, String content) {
  19
  20      this.chatRoom = chatRoom;
  21      this.author = author;
  22      this.position = position;
  23      this.content = content;
  24     }
  25 }

Write

    public Lease write(Entry e, Transaction txn, long lease)
        throws TransactionException, SecurityException,
        java.rmi.RemoteException;

Matching


Read

  public Entry read(Entry tmpl, Transaction txn, long timeout)
      throws UnusableEntryException, TransactionException,
      SecurityException, InterruptedException,
      java.rmi.RemoteException;

ReadIfExists

  public Entry readIfExists(Entry tmpl, Transaction txn,
      long timeout) throws UnusableEntryException,
      TransactionException, SecurityException,
      InterruptedException, java.rmi.RemoteException;

Take and TakeIfExists

  public Entry take(Entry tmpl, Transaction txn, long timeout)
      throws UnusableEntryException, TransactionException,
      SecurityException, InterruptedException,
      java.rmi.RemoteException;

  public Entry takeIfExists(Entry tmpl, Transaction txn,
      long timeout) throws UnusableEntryException,
      TransactionException, SecurityException,
      InterruptedException, java.rmi.RemoteException;

Notify

  public EventRegistration notify(Entry tmpl, Transaction txn,
      RemoteEventListener listener, long lease,
      java.rmi.MarshalledObject handback)
      throws TransactionException, SecurityException,
      java.rmi.RemoteException;

How Matching Works


Code Downloading


JavaSpaces Uses


JavaRing and Fractal Points


An Event Mailbox


Distributed Arrays


A NextMessageNumber Entry

   1 // In file JavaSpaces/ex1/com/artima/...
   2 //     ...chat/entry/NextMessageNumber.java
   3 package com.artima.chat.entry;
   4
   5 import net.jini.entry.AbstractEntry;
   6
   7 public class NextMessageNumber extends AbstractEntry {
   8
   9     public String chatRoom;
  10     public Long nextMessageNum;
  11
  12     public NextMessageNumber() {
  13     }
  14
  15     public NextMessageNumber(String chatRoom,
  16         Long nextMessageNum) {
  17
  18         this.chatRoom = chatRoom;
  19         this.nextMessageNum = nextMessageNum;
  20     }
  21
  22     public void increment() {
  23         nextMessageNum = new Long(
  24             nextMessageNum.longValue() + 1);
  25     }
  26 }

Writing a Chat Message

 1 // Create the template for NextMessageNumber
 2 NextMessageNumber template = new NextMessageNumber(
 3     currentChatRoom, null);
 4
 5 // Create a transaction
 6 Transaction.Created trans = TransactionFactory.create(
 7     transMan, TIME_OUT);
 8
 9 // Renew lease on transaction
10 long expireTime = trans.lease.getExpiration();
11 Date date = new Date();
12 long currentTime = date.getTime();
13 long leaseLen = expireTime - currentTime;
14 if (leaseLen < TIME_OUT) {
15     leaseMan.renewUntil(trans.lease,
16         currentTime + leaseLen, null);
17 }
18
19 // Take the NextMessageNumber entry
20 NextMessageNumber nextMsgNumEntry =
21     (NextMessageNumber) space.take(template,
22     trans.transaction, TIME_OUT);
23
24 // Grab the next message number
25 myMessageNum = nextMsgNumEntry.nextMessageNum;
26
27 // Increment the number in the entry
28 nextMsgNumEntry.increment();
29
30 // Write the incremented number back to the space
31 write(nextMsgNumEntry, trans.transaction, Lease.FOREVER);
32
33 // Create the ChatMessage entry using the message number
34 ChatMessage msgEntry = new ChatMessage(currentChatRoom,
35     userName, myMessageNum, message);
36
37 // Write the chat message to the space
38 space.write(msgEntry, null, MESSAGE_LEASE_LENGTH);
39
40 // Commit the transaction
41 trans.transaction.commit();

Project: Designing Entries for a Chat Service

Design Entry classes for a chat service. The chat service should have the following features:

  1. The JavaSpace can contain multiple named chat rooms. Each chat room should have a unique name.
  2. Each room can contain multiple users. Each user should be in only one room at any one time.
  3. Every user's name is unique across the entire JavaSpace.
  4. Users can post a message to the current room.
  5. Users can change rooms.
  6. Clients can get a list of all current chat rooms in existence.
  7. Clients can get a list of all users in a particular chat room.
  8. Clients should have some way of finding out when a new user arrives in the current room, or a user already in the current room leaves the room.
  9. Users that disappear from the network should eventually be automatically removed from their current room.
  10. Messages should be automatically removed from the space after a period of time.
  11. Rooms that have been empty (no users) for a while should be automatically removed from the space.

What you will be doing here is basically data model design, not object-oriented design. Even though JavaSpace entries are objects, because their fields are public, entries feel more like C structs or XML documents than objects in a typical object-oriented system. What you will be designing is the implementation of a Jini chat service object, not the interface. When a client invokes methods on the interface of chat service object, it will read, write, and take entries from the JavaSpace.


Sponsored Links

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us