Sponsored Link •
|
JavaSpace
interface
JavaSpace
InterfaceJavaSpace
interface
JavaSpace
object from a Jini lookup service,
RMI registry, ...
net.jini.core.entry.Entry
(which extends
java.io.Serializable
)
ChatMessage
Entry1 // 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 }
public Lease write(Entry e, Transaction txn, long lease) throws TransactionException, SecurityException, java.rmi.RemoteException;
null
fields act as wildcards
ChatMessage m = new ChatMessage(); m.chatRoom = "AutumnLeafCafe"; m.position = new Long("19");
public Entry read(Entry tmpl, Transaction txn, long timeout) throws UnusableEntryException, TransactionException, SecurityException, InterruptedException, java.rmi.RemoteException;
JavaSpace.NO_WAIT
: don't wait at all
public Entry readIfExists(Entry tmpl, Transaction txn, long timeout) throws UnusableEntryException, TransactionException, SecurityException, InterruptedException, java.rmi.RemoteException;
readIfExists()
also reads, but only blocks if
waiting for a transactional state to settle
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;
public EventRegistration notify(Entry tmpl, Transaction txn, RemoteEventListener listener, long lease, java.rmi.MarshalledObject handback) throws TransactionException, SecurityException, java.rmi.RemoteException;
MarshalledObject.equals()
)
equals()
not used in matching
null
is wildcard
equals()
not used in matching
Animal
matches a Dog
, client will actually get
a Dog
- no type truncation
equals()
not used in matching
Point
jobs
Point
PointColor
entry to the space
PointColor
s, and records them
position
field
NextMessageNumber
, increment, write back
NextMessageNumber
Entry1 // 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 }
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();
Design Entry
classes for a chat service. The chat service should
have the following features:
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
|