CO 4307 – Tutorial Slides

Download Report

Transcript CO 4307 – Tutorial Slides

Outline
•
Week 1: Survey on various distributed Systems
•
Week 2: Web Services Using JEE 5 or Glassfish
•
Week 3: Multicast
•
Week 3: RMI Callback, Security Manager, Stub Download
•
Week 4: CORBA Using JavaIDL, CORBA Callback
•
Week 5: Serialize and Secure Sockets (Digital signature)
•
Week 6: JSSE, SSL, HTTP, HTTPS
•
Week 7: Java Message Service (JMS)
•
Week 8: Jini, JavaSpaces (TupleSpace, Object Space)
•
Week 9: Peer-to-Peer (JXTA)
•
Week 10: Mobile Agent: No Voyager or Aglet, Use JADE
− Implement our own agent platform using RMI
•
Week 11: Globus Toolkit
•
Week 12: MIDlet, JMF, SIP (Audio-Video Streaming)
•
Exercises: Week 1 survey, Multicast, RMI, CORBA, Digital Signature, UBD Browser, HTTPS
CO 4307 – Tutorial Slides
1
Jini
• Jini is not an acronym. Jini is a set of APIs and network protocols that
help programmers build and deploy distributed systems that are
organized as federations of services.
• Jini has three protocols
− Allows service provider to register its service
− Allows Client to search for a service
− Allows client and service provider to discover the Jini lookup server.
• Jini lookup server
− Coordinates the discovery and registration of Jini objects
− Discovery is centralized through lookup service but communication
is direct between client and server, and thus jini runs as a brokered
architecture.
• Jini provides programmers easy option to get their devices talk to each
other.
• Jini can work on XML-based message passing; But generically, Jini
requires Java SDK (Java RMI and Serialization).
CO 4307 – Tutorial Slides
2
Week 8 - Jini
• Jini Architecture does not expect a centralized controlling authority.
• Lookup Service (LUS) stores a directory of currently available services.
• Jini is independent on network protocols, but currently only TCP/IP
implementation exists.
• Jini can also form communities, called as groups.
• Allows services to find each other without any knowledge about their names
or locations.
• Program can provide a service without knowing who is managing the service
offers and where this takes place.
• A service user can look for a service and only needs to know which Java
interface the service should implement.
• Services which are both hardware and software could be identified in Jini.
Such services can be dynamically added to and removed from a system
without having to be managed by users or administrators as in the old days.
CO 4307 – Tutorial Slides
3
Week 8 - Jini
• Lookup service
− Services are registered here with a lease period, which
could be extended.
− Users can search for services matching his requests.
• All services in Java are represented by objects, no matter
whether they are devices or programs. Jini uses RMI for
remote communication.
• Each service is assigned a unique identification. This is
assigned at registration time by the lookup server and
returned to a ServiceIDListener by calling the listener's
serviceIdNotify() method.
CO 4307 – Tutorial Slides
4
Week 8 - Jini
• Jini defines Discovery and Join protocols.
− Discovery protocol is used by the clients to identify
the lookup server. It is to be noted that the clients
don't know the IP address of the lookup server.
− Join protocol is used by the clients to register with the
lookup server. During registration, it provides
necessary information like the interface the service
implements and the service attributes which
additionally describe the service.
• Jini requires Java SDK. While referring the JVM, you need
to refer to java.exe in JRE folder.
CO 4307 – Tutorial Slides
5
Week 8 - Jini
• Jini works as below:
− Jini service uses discovery to locate the LUS and then registers its
service with LUS.
− Jini client uses discovery to locate the LUS and then uses the LUS
to find the particular Jini service it wishes to use. The lookup
service provides information to client so that client could contact
the service directly.
− Client and service talk to each other directly.
• Jini interactions:
− Similar to applet, the proxy provided by the LUS could do the job
locally at the client.
− The proxy could act as a STUB like in RMI and thus help client
contact the service to do the task.
− Proxy can act as a smart adapter. Proxy decides on how to
execute the functionality. It could select JXTA or sockets or
CORBA or others to do handle the task.
CO 4307 – Tutorial Slides
6
Week 8 - Jini
• Discovery Service is handled in three ways:
− New service sends UDP packets using Multicast
Request Protocol on 224.0.1.85:4160 and awaits an
answer. The lookup server listens at the specified
address and reports to the new service.
− Multicast Announcement Protocol announces the
availability of lookup server, using 224.0.1.84:4160.
− Identifying a lookup server using a unicast address
(Unicast Discovery Protocol). But, we need to know
the IP address in advance.
• Services can be grouped in logical manner.
CO 4307 – Tutorial Slides
7
Week 8 - Jini
• JoinManager(java.lang.Object obj, Entry[] attrSets,
ServiceIDListener callback, LeaseRenewalManager
leaseMgr)
• JoinManager(java.lang.Object obj, Entry[] attrSets,
java.lang.String[] groups, LookupLocator[] locators,
ServiceIDListener callback, LeaseRenewalManager
leaseMgr)
• The lookup server contains the collection of all existing
services and their descriptions, and can answer queries
regarding a specific type of service and, if possible,
return matching services.
CO 4307 – Tutorial Slides
8
Week 8 - Jini
• A service is an object that implements a service
API.
• A client looks up a service that supports an API.
• A discovered service object is sent to the client.
• The client and the service only have to agree on
the API, and not how requests and responses are
transmitted.
• Jini is to network services what universal plugand-play is to system devices: it allows
interoperability of clients and services regardless
of transport and “wire” protocols.
CO 4307 – Tutorial Slides
9
Week 8 - Jini
• Each federation of client and service(s):
− is independent of other federations and may co-exist with other
federations.
− is transient: the union lasts only for as long as the service(s)
is/are needed.
• There is no centralized control system:
− There is no centralized administration of available services
− There are no policies that restrict federation between client and
services.
• ServiceRegistrar: Defines the interface to the lookup service. The
interface is not a remote interface; each implementation of the
lookup service exports proxy objects that implement the
ServiceRegistrar interface local to the client, using an
implementation-specific protocol to communicate with the actual
remote server.
CO 4307 – Tutorial Slides
10
Week 8 - Jini
• LookupLocator: A utility class that performs unicast discovery, using version 1
of the unicast discovery protocol.
• ServiceItem: Items are stored in and retrieved from the lookup service using
instances of this class.
• getRegistrar(): Perform unicast discovery and return the ServiceRegistrar
object for the given lookup service.
• register(): Register a new service or re-register an existing service.
• ServiceTemplate: Items in the lookup service are matched using instance of
this class. A service item (item) matches a service template (tmpl) if:
item.serviceID equals tmpl.serviceID (or if tmpl.serviceID is null); and
item.service is an instance of every type in tmpl.serviceTypes; and
item.attributeSets contains at least one matching entry for each entry
template in tmpl.attributeSetTemplates.
• Entry: An entry is a typed set of objects, each of which may be tested for
exact match with a template. An entry is a typed group of object references
represented by a class that implements the marker interface
net.jini.core.entry.Entry.
CO 4307 – Tutorial Slides
11
Week 8 - Jini
• net.jini.core.discovery.LookupLocator
− A utility class that performs unicast discovery for a Lookup service
− Constructors
• LookupLocator(String url)
• LookupLocator(String host, int port)
• The URL type is jini;Default port for a lookup service is 4160;
Translate CAFE-BABE from hex to decimal to get 4160
• String getHost()
• int getPort()
• ServiceRegistrar getRegistrar()
• ServiceRegistrar getRegistrar(int timeout)
− Perform unicast discovery and return the ServiceRegistrar object for the
given lookup service.
CO 4307 – Tutorial Slides
12
Week 8 - Jini
•
Interface net.jini.core.lookup.ServiceRegistrar
− Methods: Information about available services
• Class[] getEntryClasses(ServiceTemplate tmpl) Object[]
• getFieldValues(ServiceTemplate, int setIndex, String field) Class[]
• getServiceTypes(ServiceTemplate, String prefix)
• Object lookup(ServiceTemplate tmpl) ServiceMatches
• lookup(ServiceTemplate tmpl, int maxMatches)
− Returns the service object from an item matching the template
− Information about Lookup service: String[] getGroups()
− Returns the groups that this lookup service is a member of.
− LookupLocator getLocator()
− LookupLocator for unicast discovery of lookup service.
− ServiceID getServiceID()
− Notifcation, Registration
− EventRegistration notify(ServiceTemplate tmpl, int transitions,
RemoteEventListener listener, MarshalledObject handback, long leaseDuration)
ServiceRegistration register(ServiceItem item, long leaseDuration)
CO 4307 – Tutorial Slides
13
Week 8 - Jini
• Interface net.jini.core.entry.Entry
• Entry subclasses:
− Are used in identifying and matching services
− Contains public fields of non-primitive types
− Must have no-argument constructor
− All fields must be serializable
− Each field is serialized separately, so references between two fields of an
entry will not be reconstituted to be shared references, but instead to
separate copies of the original object
− If a field of an entry being fetched is cannot be deserialized for any
reason then net.jini.core.entry.UnusableEntryException will be thrown
when
• Entry is a marker interface, it has no methods
CO 4307 – Tutorial Slides
14
Week 8 - Jini
• net.jini.core.lookup.ServiceID
− Each service has a unique service id. The id is unique over time and space with
respect to all other service ids generated by all lookup services. The id is a 128bit value, to insure this uniqueness.
− It is to be set by the lookup service. To re-register an existing service, or to
register the service in any other lookup service, item.serviceID should be set to
the same service id that was returned by the initial registration.
• com.sun.jini.lookup.ServiceIDListener
− Contains one method, serviceIDNotify. The lookup service calls this method to
provide the service its service ID.
• com.sun.jini.lookup.JoinManager
− This class manages the join protocol for a service. It discovers and keeps track of
which lookup services to join, registers with them, keeps the registration leases
renewed, and keeps the attributes up to date.
− JoinManager(Object service, Entry[] attrSets,ServiceIDListener callback,
LeaseRenewalManager leaseMgr)
• JoinManager Methods
− addAttributes(Entry[] attrSets)
addAttributes(Entry[] attrSets, boolean checkSC)
addGroups(String[] groups)
addLocators(LookupLocator[] locators)
Entry[] getAttributes()
String[] getGroups()
ServiceRegistrar[] getJoinSet()
CO 4307 – Tutorial Slides
15
Week 8 - Jini
• Get the list of lookup services that have currently been joined.
− LookupLocator[] getLocators()
• Get the list of locators of specific lookup services to join.
− modifyAttributes(Entry[] attrSetTemplates, Entry[] attrSets)
− modifyAttributes(Entry[] attrSetTemplates, Entry[] attrSets, boolean
checkSC)
− removeGroups(String[] groups)
− removeLocators(LookupLocator[] locators)
− setAttributes(Entry[] attrSets)
− setGroups(String[] groups)
− setLocators(LookupLocator[] locators)
− terminate()
CO 4307 – Tutorial Slides
16
Week 8 – Jini Operation
• Jini Service uses discovery to locate the LUS, either by using
multicast or unicast discovery
• LUS returns a ServiceRegistrar object
• Jini Service can now use the ServiceRegistrar’s register() function to
register its service with LUS
• Jini client uses discovery to locate the LUS, either by using multicast
or unicast discovery
• LUS returns a ServiceRegistrar object
• Client uses the ServiceRegistrar’s lookup() function to search for
specific service required
• Client receives the Java Proxy (RequiredService Interface)
• Client executes the RequiredService through RMI.
CO 4307 – Tutorial Slides
17
Week 8 – Jini – ServiceInterface and Server
• MyServiceInterface.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface MyServerInterface extends Remote {
public String sayHello () throws RemoteException;
}
• MyService.java
import net.jini.lookup.entry.Name;
import net.jini.lookup.ServiceIDListener;
import net.jini.core.lookup.ServiceID;
import net.jini.core.lookup.ServiceRegistrar;
import net.jini.core.lookup.ServiceItem;
import net.jini.core.discovery.LookupLocator;
import net.jini.core.entry.Entry; import net.jini.core.lease.Lease;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
CO 4307 – Tutorial Slides
18
Week 8 – Jini - Server
import java.rmi.RMISecurityManager;
public class MyService extends UnicastRemoteObject implements
MyServerInterface {
public MyService () throws RemoteException { super (); }
public String sayHello () throws RemoteException { // MyServiceInterface
System.out.println ("server: The method sayHello() in MyService was " +
"called");
return ("Hello World from MyService!"); }
public static void main (String[] args) {
MyServerInterface myServer;
LookupLocator lookup;
ServiceRegistrar registrar;
ServiceItem serviceItem;
try {
/*
Set the security manager to allow the RMI class loader
to go to the codebase for classes that are not available locally. */
System.setSecurityManager (new RMISecurityManager ());
CO 4307 – Tutorial Slides
19
Week 8 – Jini - Server
/*
Create the attributes (an array of entry objects) that describe this
server and use it to register this server with the lookup service.
JoinManager finds and registers with the lookup service */
Entry[] attr = new Entry[1];
attr[0] = new Name("HelloWorldServer");
myServer = new MyService ();
// ServiceItem needs ServiceID, object of service and Attributes as parameters
serviceItem = new ServiceItem(null, myServer, attr);
lookup = new LookupLocator ("jini://localhost");
registrar = lookup.getRegistrar();
// register needs the ServiceItem and Lease (Lease.ANY [asks LUS to decide the time] or
Lease.FOREVER [lease never expires])
// the copy of the ServiceItem object is send to LUS using RMI service and is stored there
registrar.register(serviceItem, Lease.FOREVER);
System.out.println("Service Ready ...");
} catch (Exception e)
{ System.out.println("server: MyService.main(): Exception " + e); }
} }
CO 4307 – Tutorial Slides
20
Week 8 – Jini - Client
import net.jini.core.entry.Entry;
import net.jini.core.discovery.LookupLocator;
import net.jini.core.lookup.ServiceRegistrar;
import net.jini.core.lookup.ServiceTemplate;
import net.jini.lookup.entry.Name;
import java.rmi.RMISecurityManager;
public class MyClient {
public static void main (String[] args) {
Entry[] aeAttributes;
LookupLocator lookup;
ServiceRegistrar registrar; ServiceTemplate template;
MyServerInterface myServerInterface;
try {
/*
Set the security manager to allow the RMI class loader to access
the codebase for classes that are not available locally */
System.setSecurityManager (new RMISecurityManager ());
/*
Find the Jini lookup service (reggie) and print its location */
lookup = new LookupLocator ("jini://localhost");
CO 4307 – Tutorial Slides
21
Week 8 – Jini - Client
/*
Get the lookup service's ServiceRegistrar and perform a search to find
the service that has the name attribute name of "HelloWorldServer".
The lookup service returns an Proxy object to the service */
registrar = lookup.getRegistrar();
aeAttributes = new Entry[1];
aeAttributes[0] = new Name ("HelloWorldServer");
// ServiceTemplate contains the search criteria. Wildcards can be used.
// ServiceTemplate might include a ServiceID or attributes
template = new ServiceTemplate (null, null, aeAttributes);
// lookup needs the ServiceTemplate
myServerInterface = (MyServerInterface) registrar.lookup (template);
System.out.println ("Calling sayHello()->" + myServerInterface.sayHello () + "<-");
} catch (Exception e) {
System.out.println ("client: MyClient.main() exception: " + e);
}
}
}
CO 4307 – Tutorial Slides
22
Week 8 - Jini
23
• Compiling and Running the Application
− Install the Jini Technology Starter Kit.
• jini2_1.exe for Windows or jini2_1.bin for Linux
• Double-click the exe file OR ./jini2_1.bin for Linux
• Provide folder for jini to install as C:\jini2_1 or /home/student/jini2_1
− If you face problems (libc.so.6 or libdl.so.2 not found) when installing jini
on Linux
• Install hexadecimal editor on Linux (ghex2 in OpenSuse using YaST)
• Open the jini2_1.bin file using ghex: ghex2 jini2_1.bin
• Click Edit and Find export LD_ASSUME_KERNEL=2.2.5 and replace it as
#xport LD_ASSUME_KERNEL=2.2.5
• Click File and Save. Then, run the jini2_1.bin file.
− You must as java SDK already installed for Jini to install
(/home/student/SDK/jdk/bin/java).
− To start the Jini Service, run Launch-All, which is in the installverify
directory of your Jini installation directory. This will start a service
browser and six contributed Jini network technology services.
• On Windows: If the services does not start, check the Registrar option
present in the Service Browser
CO 4307 – Tutorial Slides
Week 8 - Jini
• On Linux: If Launch-All complains libc.20.6 and other errors,
use ghex2 and change export LD_ASSUME_KERNEL to #xport
LD_ASSUME_KERNEL
• On Linux: If you find no service is being run on the Service
Browser, click on File and select Find By Address and type
127.0.0.1 or localhost. Then click on Registrar and click on
the machine name given. Also, if only one service is running
that means the firewall is blocking. Disable firewall so that all
the required services start to run.
• On the Service Browser, we should be able to see
− Lookup Service (Service Registrar): reggie
− Persistence Service (JavaSpaces): outrigger
− Transaction Service: mahalo
− Lookup Discovery Service: fiddler
− Lease Renewal Service: norm
− Event Mailbox Service: mercury
CO 4307 – Tutorial Slides
24
Week 8 - Jini
• Compiling and Running the Application
− MyServerInterface.java is needed by both the MyService.java and
MyClient.java
− Compile the MyServerInterface.java, MyService.java and
MyClient.java [which are present in c:\mibuhari\practice\jini or
/home/student/jini folder]
• javac MyServerInterface.java
• javac –classpath c:\jini2_1\classes;. MyService.java
• javac –classpath c:\jini2_1\classes;. MyClient.java
• javac –classpath /home/student/jini2_1/classes:.
MyService.java
• javac –classpath /home/student/jini2_1/classes:.
MyClient.java
− We need to generate the stub and skeleton files using
• rmic MyService
− Without the Service being enabled (using Launch-All), running
MyService will generate a Connection Exception.
CO 4307 – Tutorial Slides
25
Week 8 - Jini
−
•
•
•
Run MyService and MyClient (which in c:\mibuhari\practice\jini /home/student/jini folder) as
• java -classpath c:\jini2_1\lib\jini-ext.jar;c:\jini2_1\lib\reggie.jar;. MyService
• java -classpath ../jini2_1/lib/jini-ext.jar:../jini2_1/lib/reggie.jar:. MyService
OR
• java -classpath c:\jini2_1\classes;. MyService
• java -classpath c:\jini2_1\classes;. MyClient
• java -classpath ../jini2_1/classes:. MyService
• java -classpath ../jini2_1/classes:. MyClient
The policy file is needed if the RMISecurityManager is used in both the client and server programs.
This policy file should be needed for both the client and server.
Lets have a policy file (jini.policy in c:\mibuhari\practice\jini)
− grant { permission java.security.AllPermission; };
Run MyService and MyClient (which in c:\mibuhari\practice\jini folder) as
− java –Djava.security.policy=jini.policy -classpath c:\jini2_1\lib\jiniext.jar;c:\jini2_1\lib\reggie.jar;. MyService
− java –Djava.security.policy=jini.policy -classpath ../jini2_1/lib/jini-
ext.jar:../jini2_1/lib/reggie.jar:. MyService
OR
− java –Djava.security.policy=jini.policy -classpath c:\jini2_1\classes;. MyService
− java –Djava.security.policy=jini.policy -classpath c:\jini2_1\classes;. MyClient
− java –Djava.security.policy=jini.policy -classpath ../jini2_1/classes:. MyService
− java –Djava.security.policy=jini.policy -classpath ../jini2_1/classes:. MyClient
CO 4307 – Tutorial Slides
26
Week 8 - Jini
• Running the Server, adds a service to the Service Browser of Jini.
• Running client and server on a single machine: If the Client program is in
c:\mibuhari\practice\jini\client folder and Server program is in
c:\mibuhari\practice\jini\server
− Run MyService (which in c:\mibuhari\practice\jini\server or
/home/student/jini folder) as
• java –Djava.security.policy=jini.policy -classpath c:\jini2_1\classes;.
MyService
• java –Djava.security.policy=jini.policy -classpath ../jini2_1/classes:.
MyService
− Run MyClient (which in c:\mibuhari\practice\jini\client folder) as
• java –Djava.security.policy=jini.policy -classpath
c:\jini2_1\classes;..\server;. MyClient
• java –Djava.security.policy=jini.policy classpath ../jini2_1/classes:../server:. MyClient
• ..\server is a must to indicate the path to the Server. Otherwise
ClassNotFoundException: MyService will be reported.
CO 4307 – Tutorial Slides
27
Week 8 - Jini
• Running client and server at different machines: Client requires the
Implementation and the stub be available for usage through HTTP
(We use J2EE here).
− Copy the MyService.class and MyService_Stub.class into the
C:\Sun\SDK\domains\domain1\docroot or /src/www/htdocs folder.
− Then run the server and client as:
• java –Djava.security.policy=jini.policy -classpath
c:\jini2_1\classes;. MyService
• java –Djava.rmi.server.codebase=http://202.160.5.155:8080/
–Djava.security.policy=jini.policy -classpath c:\jini2_1\classes;.
MyClient
• java –Djava.security.policy=jini.policy classpath ../jini2_1/classes:. MyService
• java –Djava.rmi.server.codebase=http://202.160.5.155:8080/
–Djava.security.policy=jini.policy -classpath ../jini2_1/classes:.
MyClient
• Where, 202.160.0.155 is the IP address of the server.
CO 4307 – Tutorial Slides
28
Week 8 - Jini
• Jini Starter Kit is made up of many jar files. Here are few important
ones:
− jini-core.jar, core Jini functionality
− jini-ext.jar, Jini lookup functionality (from client)
− reggie.jar, Jini Lookup service
− reggie-dl.jar, Jini lookup service
− tools.jar, HTTP server
• Jini Starts three services
− An HTTP server: for accessing the remote class files
− RMID Daemon: which starts on machines where you want to host
the remote Java objects
− Reggie: the default Jini lookup server
CO 4307 – Tutorial Slides
29
Week 8 –JavaSpaces
• The JavaSpaces technology is a simple and powerful high-level tool
for building distributed and collaborative applications.
• Based on the concept of shared network-based space that serves as
both object storage and exchange area, it provides a simple API that
is easy to learn and yet expressive for building sophisticated
distributed applications.
• The JavaSpaces technology model is different in that it provides
persistent object exchange areas (or spaces) through which remote
Java technology processes coordinate actions and exchange data.
• A marked departure from classic distributed models that rely on
message passing or RMI, the JavaSpaces model views a distributed
application as a collection of processes that cooperate through the
flow of objects into and out of one or more spaces. This programming
model has its roots in Linda, a coordination language developed by Dr.
David Gelernter at Yale University.
CO 4307 – Tutorial Slides
30
Week 8 - JavaSpaces
• Relational databases understand the data they store and
manipulate it directly through query languages such as
SQL. JavaSpaces services, on the other hand, store
entries that they understand only by type and the
serialized form of each field.
• As a result, IN JavaSpaces:
− there are no general queries
− Only "exact match" or "don't care" for a given field is allowed.
• Object databases provide an object-oriented image of
stored data that can be modified and used, almost as if it
were transient memory. JavaSpaces systems do not
provide a nearly transparent persistent or transient layer,
and they work only on copies of entries.
CO 4307 – Tutorial Slides
31
Week 8 - JavaSpaces
• Operations used in JavaSpaces are:
− write(): Writes new objects into a space
− take(): Retrieves objects from a space
− read(): Makes a copy of objects in a space
− notify: Notifies a specified object when entries that match the
given template are written into a space
• Both the read() and take() methods have variants: readIfExists() and
takeIfExists(). If they are called with a zero timeout, then they are
equivalent to their counterpart. The timeout parameter comes into
effect only when a transaction is used.
• Unlike conventional object stores, objects are passive data.
Therefore, processes do not modify objects in the space or invoke
their methods directly. In order to modify an object, a process must
explicitly remove, update, and reinsert it into the space.
CO 4307 – Tutorial Slides
32
Week 8 - JavaSpaces
• Once an entry is written into a JavaSpaces service, it can
be used in future look-up operations. Looking up entries
is performed using templates, which are entry objects
that have some or all of their fields set to specified
values that must be matched exactly. All remaining fields,
which are not used in the lookup, are left as wildcards.
• The read() method returns either an entry that matches
the template or an indication that no match was found.
The take() method operates like read(), but if a match is
found, the entry is removed from the space.
CO 4307 – Tutorial Slides
33
Week 8 - JavaSpaces
• A client can interact with as many JavaSpaces services as needed. Clients
perform operations that map entries to templates onto JavaSpaces services.
• In a Chat example, participants write message objects into the space, while
other members wait for new message objects to appear, then read them out
and display their contents. The list of participants can be kept in the space
and updated whenever someone joins or leaves the discussion. Because the
space is persistent, a new member can read and view the entire discussion
Typical JavaSpaces Technology Application
CO 4307 – Tutorial Slides
34
Week 8 - JavaSpaces
• JavaSpaces is part of Jini and is used for management and retrieval
of matching services in the lookup service, so that services and
service attributes are described in the JavaSpaces way in the first
place.
• public ServiceTemplate(ServiceID serviceID, Class[] serviceTypes,
Entry[] attributeSetTemplate)
• Proxy of the requested service will be transmitted, which in this case
corresponds to an RMI stub, and which can be used to issue remote
calls to the service via RMI
• Remaining lease time in milliseconds: getExpiration()
• Extending leases: renew(long duration)
• User of a lease can terminate a lease prematurely by calling cancel()
CO 4307 – Tutorial Slides
35
Week 8 - JavaSpaces
• A space is a shared, network-accessible repository for
objects. Instead of communicating with each other,
processes coordinate by exchanging objects through one
or more spaces.
• A space may be transient, or it may be persistent.
• An object may be identified using properties lookup; if a
desired object is not currently available, a process can
wait until one arrives.
• Objects deposited in a space are passive. Unlike other
models, processes don’t modify objects in the space or
invoke an object’s methods directly.
• To modify an object, a process must explicitly remove it,
update it, and reinsert it into the space.
CO 4307 – Tutorial Slides
36
Week 8 - JavaSpaces
• To build applications based on object spaces, one
designs distributed data structures and
distributed protocols that operate over the
object spaces.
• A distributed data structure is composed of
multiple objects that are stored in one or more
spaces.
• A distributed protocol defines the way
participants share and modify these data
structures in a coordinated way.
• JavaSpaces API has an interface named Entry,
which can be used to write or read from a space.
CO 4307 – Tutorial Slides
37
Week 8 - JavaSpaces
JavaSpace runs over Jini.
 To run JavaSpace, you need an
infrastructure in place:

− An HTTP server, used to download code to
JavaSpaces clients.
− An RMI Activation Daemon, for managing the
states of Jini services.
− A Lookup service: for Jini service location.
− A JavaSpaces service.
− Optionally, a transaction manager.
CO 4307 – Tutorial Slides
38
Week 8 - JavaSpaces
• Simplicity.
• Expressive: “Using a small set of
operations, we can build a large class of
distributed applications without writing a
lot of code.”
• It supports loosely coupled protocols.
• It eases the burden of writing client/server
systems: the functionalities of the server
are provided for free, via the spaces; in
most cases only the client code needs to
be written.
CO 4307 – Tutorial Slides
39
Week 8 - JavaSpaces
• All operations are invoked on an object that implements
the net.jini.space.JavaSpace interface
−
−
−
−
JavaSpace space = getSpace();
MessageEntry msg = new MessageEntry();
msg.content = "Hello there";
space.write(msg, null, Lease.FOREVER);
• The null field indicates that no Transaction object is
managing the operation.
• The write() operation places a copy of an entry into the
given JavaSpace service, and the Entry passed is not
affected by the operation. Each write() operation places
a new Entry into the space even if the same Entry object
is used in more than one write().
CO 4307 – Tutorial Slides
40
Week 8 - JavaSpaces
• Entries written in a space are governed by a renewable lease. You
can change the lease (when the write() operation is invoked) to one
hour as follows
− space.write(msg, null, 60 * 60 * 1000);
• A write() operation returns a Lease object that is milliseconds long. If
the requested time is longer than the space is willing to grant, you
will get a lease with a reduced time. In addition, when the lease
expires, the entry is removed from the space.
• To read an entry, a template is used, which is an entry that may have
one or more of its fields set to null. An entry matches a template if
(a) the entry has the same type as or is a subtype of the template
and (b) if for every specified non-null field in the template, their
fields match exactly. The null fields act as wildcards and match any
value.
CO 4307 – Tutorial Slides
41
Week 8 - JavaSpaces
− MessageEntry template = new MessageEntry();
− MessageEntry output = (MessageEntry)
space.read(template, null, Long.MAX_VALUE);
• The Long.MAX_VALUE is a timeout value that
specifies how long the client is willing to wait for
a matching entry to appear, even if there is no
hint of an entry appearing. The read() and take()
operations would block on a completely empty
space. But if the timeout value was readIfExists(),
then it would be how long the client was willing
to wait for interfering transactions to settle.
CO 4307 – Tutorial Slides
42
Week 8 - JavaSpaces
• Transactions
− The JavaSpaces API uses the package
net.jini.core.transaction to provide basic atomic
transactions that group multiple operations across
multiple JavaSpaces services into a bundle that acts as
a single atomic operation. Either all modifications
within the transactions will be applied or none will,
regardless of whether the transaction spans one or
more operations or one or more JavaSpaces services.
− Transactions can span multiple spaces and participants
in general.
CO 4307 – Tutorial Slides
43
Week 8 - JavaSpaces
• The Jini Technology Starter Kit comes with the package
com.sun.jini.outrigger, which provides an implementation
of a JavaSpaces technology-enabled service. You can run
it two ways:
• As a transient space that loses its state between
executions: Use
com.sun.jini.outrigger.TransientOutriggerImpl.
• As a persistent space that maintains state between
executions: Use
com.sun.jini.outrigger.PersistentOutriggerImpl.
• The TransientOutriggerImpl can be run only as a
nonactivatable server, but the PersistentOutriggerImpl
can be run as either an activatable or nonactivatable
server
CO 4307 – Tutorial Slides
44
Week 8 - JavaSpaces
• Compiling and Running the Application
− Install the Jini Technology Starter Kit.
− Compile the MessageEntry.java [which is present in
c:\mibuhari\javaspaces or /home/student/javaspaces folder]
• javac –classpath c:\jini2_1\classes MessageEntry.java
• javac –classpath /home/student/jini2_1/classes MessageEntry.java
− To start the JavaSpace Service, run Launch-All, which is in the
installverify directory of your Jini installation directory. This will start a
service browser and six contributed Jini network technology services.
• Check if the firewall is disabled or not.
• If the services does not start, check the Registrar option present in
the Service Browser. You might need to use File  Find by Address
and type 127.0.0.1 and then select the Registrar option.
− Compile the Lookup.java
• javac –classpath c:\jini2_1\classes Lookup.java
• javac –classpath /home/student/jini2_1/classes Lookup.java
• Note that Lookup.java and MessageEntry.java don’t have main
method and both are needed for SpaceClient.java program
CO 4307 – Tutorial Slides
45
Week 8 - JavaSpaces
• Compiling and Running the Application
− Compile the SpaceClient.java (. Indicates current folder)
• javac –classpath c:\jini2_1\classes;. SpaceClient.java
• javac –classpath /home/student/jini2_1/classes:.
SpaceClient.java
− Run the SpaceClient (which in c:\mibuhari\javaspaces or
/home/student/javaspaces folder) as
• java -classpath c:\jini2_1\lib\jiniext.jar;c:\jini2_1\lib\reggie.jar;c:\jini2_1\lib\outrigger.jar;c:\
mibuhari\javaspaces SpaceClient
• java -classpath /home/student/jini2_1/lib/jiniext.jar:/home/student/jini2_1/lib/reggie.jar:/home/student
/jini2_1/lib/outrigger.jar:. SpaceClient
• If the SpaceClient did not respond, make sure the firewall is
disabled.
CO 4307 – Tutorial Slides
46