1. Trang chủ
  2. » Công Nghệ Thông Tin

Advanced Java 2 Platform HOW TO PROGRAM phần 8 pps

187 464 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 187
Dung lượng 1,27 MB

Nội dung

Chapter 22 Jini 1247 In method discovered, lines 69–70 register the SeminarInfo service’s Servi- ceItem with the discovered ServiceRegistrar. Lines 73–74 create a new Lease- RenewalManager, which class SeminarInfoLeaseService uses to manage the SeminarInfo service’s lease. Lines 77–78 invoke method renewUntil of class LeaseRenewalManager. Method renewUntil takes as its first argument the Lease object to be renewed. In this example, we obtain the Lease object by invoking method getLease of class ServiceRegistration. The second argument specifies the desired expiration time (in milliseconds) for renewed Leases. Line 78 specifies the con- stant Lease.FOREVER to request a Lease that never expires. This does not guarantee that the lookup service will provide an everlasting Lease—the lookup service is free to grant a lease that is shorter than the requested length. The third argument to method renewUntil is the LeaseListener, to notify of problems encountered when renewing the Lease. We pass null as the third argument to disregard such notifications. To run the SeminarInfo Jini service with lease management, create a new JAR file named SeminarServiceWithLeasing.jar. Figure 22.36 shows the contents of SeminarServiceWithLeasing.jar. Note that this JAR file replaces Seminar- InfoService.class with SeminarInfoLeaseService.class. Note also that 113 // launch SeminarInfoLeaseService 114 public static void main( String args[] ) 115 { 116 // set SecurityManager 117 if ( System.getSecurityManager() == null ) { 118 System.setSecurityManager( new RMISecurityManager() ); 119 } 120 121 SeminarInfoLeaseService service = 122 new SeminarInfoLeaseService(); 123 124 Object keepAlive = new Object(); 125 126 // wait on keepAlive Object to keep service running 127 synchronized ( keepAlive ) { 128 129 // keep application alive 130 try { 131 keepAlive.wait(); 132 } 133 134 // handle exception if wait interrupted 135 catch ( InterruptedException exception ) { 136 exception.printStackTrace(); 137 } 138 139 } // end synchronized block 140 141 } // end method main 142 } Fig. 22.35 Fig. 22.35Fig. 22.35 Fig. 22.35 SeminarInfoLeaseService uses class LeaseRenewalManager to manage SeminarInfo service leasing (part 4 of 4). 1248 Jini Chapter 22 this JAR file includes SeminarProvider.class, which is our custom Entry object for the SeminarInfo service. After packaging the classes in SeminarServiceWithLeasing.jar, run the new version of the service by typing the following command at a command prompt: java -classpath %CLASSPATH%;SeminarServiceWithLeasing.jar -Djava.security.policy=policy -Djava.rmi.server.codebase=http://hostname:9090/ SeminarServiceDownload.jar com.deitel.advjhtp1.jini.utilities.leasing. SeminarInfoLeaseService where policy is an appropriate security policy and hostname is the hostname where the Web server for downloading the SeminarInfo service proxy is running. The Lease- RenewalManager will renew the SeminarInfo service’s lease to maintain the ser- vice’s lookup service registrations. 22.8.4 JoinManager Utility As we have seen, making a Jini service available in a Jini community requires several steps. The service must discover lookup services, register with discovered lookup services and maintain registration leases. Class JoinManager is a utility class that facilitates the pro- cess of deploying a Jini service by performing lookup discovery, service registration and lease management in a single class. Class File Directory in SeminarServiceWithLeasing.jar Seminar.class com\deitel\advjhtp1\jini\seminar\ SeminarInterface.class com\deitel\advjhtp1\jini\seminar\service\ SeminarProxy.class com\deitel\advjhtp1\jini\seminar\service\ BackendInterface.class com\deitel\advjhtp1\jini\seminar\service\ SeminarInfo.class com\deitel\advjhtp1\jini\seminar\service\ SeminarInfo_Stub.class com\deitel\advjhtp1\jini\seminar\service\ SeminarProvider.class com\deitel\advjhtp1\jini\utilities\entry\ SeminarInfoLeaseService.class com\deitel\advjhtp1\jini\utilities\leasing\ Fig. 22.36 Fig. 22.36Fig. 22.36 Fig. 22.36 SeminarServiceWithLeasing.jar contents. Chapter 22 Jini 1249 Class SeminarInfoJoinService (Fig. 22.37) uses class JoinManager to deploy the SeminarInfo service. Lines 38–40 create a LookupDiscoveryManger that the JoinManager will use to discover lookup services. We pass as the first argument to the LookupDiscoveryManager constructor a String array with a single element, which is an empty String. This argument specifies that the LookupDiscoveryMan- ager should perform multicast discovery for lookup services that support the “public” group. For the second and third arguments, line 40 passes the value null. These arguments disable unicast discovery and specify a null DiscoveryListener, respectively. The JoinManager handles the discovery process, so class SeminarInfoJoinService need not handle DiscoveryEvents. Lines 43–44 create a new Entry array with a single SeminarProvider element that specifies the provider of Seminars for the SeminarInfo service. Lines 47–49 create a new instance of class JoinManager to discover lookup services, register the ser- vice and maintain the service’s registration leases. Line 47 invokes method cre- ateProxy to create a SeminarProxy for the SeminarInfo service. The second argument to the JoinManager constructor is the array of Entry attributes that describe the service. The third argument is a reference to a ServiceIDListener. When the JoinManager registers the Jini service with a lookup service, the JoinManager noti- fies the ServiceIDListener of the service ID that the lookup service assigns to the Jini service. The fourth argument is a DiscoveryManagement object for discovering lookup services. For this example, we pass the LookupDiscoveryManager created on lines 38–40. The final argument to the JoinManager constructor is a LeaseRenewal- Manager for maintaining the service’s registration leases. 1 // SeminarInfoJoinService.java 2 // SeminarInfoJoinService uses a JoinManager to find lookup 3 // services, register the Seminar service with the lookup 4 // services and manage lease renewal. 5 package com.deitel.advjhtp1.jini.utilities.join; 6 7 // Java core packages 8 import java.rmi.RMISecurityManager; 9 import java.rmi.RemoteException; 10 import java.io.IOException; 11 12 // Jini core packages 13 import net.jini.core.lookup.ServiceID; 14 import net.jini.core.entry.Entry; 15 16 // Jini extension packages 17 import net.jini.lookup.entry.Name; 18 import net.jini.lease.LeaseRenewalManager; 19 import net.jini.lookup.JoinManager; 20 import net.jini.discovery.LookupDiscoveryManager; 21 import net.jini.lookup.ServiceIDListener; 22 Fig. 22.37 Fig. 22.37Fig. 22.37 Fig. 22.37 SeminarInfoJoinService uses class JoinManager to facilitate registering the SeminarInfo service and manage its leasing (part 1 of 3). 1250 Jini Chapter 22 23 // Deitel packages 24 import com.deitel.advjhtp1.jini.seminar.service.*; 25 import com.deitel.advjhtp1.jini.utilities.entry.*; 26 27 public class SeminarInfoJoinService implements ServiceIDListener { 28 29 // SeminarInfoJoinService constructor 30 public SeminarInfoJoinService() 31 { 32 // use JoinManager to register SeminarInfo service 33 // and manage lease 34 try { 35 36 // create LookupDiscoveryManager for discovering 37 // lookup services 38 LookupDiscoveryManager lookupManager = 39 new LookupDiscoveryManager( new String[] { "" }, 40 null, null ); 41 42 // create and set Entry name for service 43 Entry[] entries = new Entry[ 1 ]; 44 entries[ 0 ] = new SeminarProvider( "Deitel" ); 45 46 // create JoinManager 47 JoinManager manager = new JoinManager( createProxy(), 48 entries, this, lookupManager, 49 new LeaseRenewalManager() ); 50 } 51 52 // handle exception creating JoinManager 53 catch ( IOException exception ) { 54 exception.printStackTrace(); 55 } 56 57 } // end SeminarInfoJoinService constructor 58 59 // create seminar service proxy 60 private SeminarInterface createProxy() 61 { 62 // get SeminarProxy for SeminarInfo service 63 try { 64 return new SeminarProxy( new SeminarInfo() ); 65 } 66 67 // handle exception creating SeminarProxy 68 catch ( RemoteException exception ) { 69 exception.printStackTrace(); 70 } 71 72 return null; 73 74 } // end method createProxy Fig. 22.37 Fig. 22.37Fig. 22.37 Fig. 22.37 SeminarInfoJoinService uses class JoinManager to facilitate registering the SeminarInfo service and manage its leasing (part 2 of 3). Chapter 22 Jini 1251 Method serviceIDNotify (lines 77–80) is required by interface Service- IDListener. The JoinManager invokes method serviceIDNotify to notify a ServiceIDListener that a lookup service has assigned a service ID to the Jini service. Line 79 simply prints out the service ID. Method main (lines 83–92) sets the RMISecurityManager and launches the SeminarInfoJoinService application. Note that method main does not use a keepAlive Object to keep the application running, as was required in previous exam- ples. The JoinManager keeps the application alive. To run the SeminarInfo service using the JoinManager, create the JAR file SeminarServiceJoinManager.jar with the contents listed in Figure 22.38. 75 76 // receive notification of ServiceID assignment 77 public void serviceIDNotify( ServiceID serviceID ) 78 { 79 System.err.println( "Service ID: " + serviceID ); 80 } 81 82 // launch SeminarInfoJoinService 83 public static void main( String args[] ) 84 { 85 // set SecurityManager 86 if ( System.getSecurityManager() == null ) { 87 System.setSecurityManager( new RMISecurityManager() ); 88 } 89 90 // create SeminarInfoJoinService 91 new SeminarInfoJoinService(); 92 } 93 } Service ID: 0084d3a0-4bbe-4b76-aa0b-f73294738fec Class File Directory in SeminarServiceJoinManager.jar Seminar.class com\deitel\advjhtp1\jini\seminar\ SeminarInterface.class com\deitel\advjhtp1\jini\seminar\service\ SeminarProxy.class com\deitel\advjhtp1\jini\seminar\service\ Fig. 22.38 Fig. 22.38Fig. 22.38 Fig. 22.38 SeminarServiceJoinManager.jar contents (part 1 of 2). Fig. 22.37 Fig. 22.37Fig. 22.37 Fig. 22.37 SeminarInfoJoinService uses class JoinManager to facilitate registering the SeminarInfo service and manage its leasing (part 3 of 3). 1252 Jini Chapter 22 After packaging the classes in SeminarServiceJoinManager.jar, run the new version of the service by typing the following at a command prompt: java -classpath %CLASSPATH%;SeminarServiceJoinManager.jar -Djava.security.policy=policy -Djava.rmi.server.codebase=http://hostname:9090/ SeminarServiceDownload.jar com.deitel.advjhtp1.jini.utilities.join. SeminarInfoJoinService where policy is an appropriate security policy and hostname is the hostname where the Web server for downloading the SeminarInfo service proxy is running. Figure 22.37 shows a sample service ID output from application SeminarInfoJoinService. 22.8.5 Service Discovery Utilities Complex Jini clients often have specific requirements for the Jini services they employ. To satisfy these requirements, the Jini client often must work with sets of Jini services. The client searches through these services to locate the particular service that can satisfy the cli- ent’s needs. For example, a Jini client that provides users with information about printers available in a given office needs a set of services for the available printers. This printer- monitoring program would need to be aware of the status of each printer to know when a new printer has been added. The service also should be able to search among the printers for particular features (color support, print quality, capacity, speed etc.). Class net.jini.lookup.ServiceDiscoveryManager provides Jini clients with a richer set of service and lookup-service management features than interface ServiceRegistrar provides. Class ServiceDiscoveryManager facilitates dis- covering available services and enables clients to perform finer-grained searches than are possible with the ServiceRegistrar interface. Jini clients also can use class Ser- viceDiscoveryManager to enhance application performance by maintaining a local cache of services. There are three primary ways in which Jini clients use class Service- DiscoveryManager—creating a local cache of services, receiving event notifications BackendInterface.class com\deitel\advjhtp1\jini\seminar\service\ SeminarInfo.class com\deitel\advjhtp1\jini\seminar\service\ SeminarInfo_Stub.class com\deitel\advjhtp1\jini\seminar\service\ SeminarProvider.class com\deitel\advjhtp1\jini\utilities\entry\ SeminarInfoJoinService.class com\deitel\advjhtp1\jini\utilities\join\ Class File Directory in SeminarServiceJoinManager.jar Fig. 22.38 Fig. 22.38Fig. 22.38 Fig. 22.38 SeminarServiceJoinManager.jar contents (part 2 of 2). Chapter 22 Jini 1253 when services become available or unavailable and performing detailed searches not pos- sible with simple ServiceTemplates. Class ServiceDiscoveryManager can enhance Jini-client performance by cre- ating a local cache of discovered services. This local cache—implemented as a net.jini.lookup.LookupCache—enables the client to perform additional service lookups without incurring the network overhead of a remote call to a lookup service. When the client needs a particular service that is in the LookupCache, the client simply invokes method lookup of interface LookupCache to retrieve the service from the local cache. Jini clients also can use a LookupCache retrieved from a ServiceDiscovery- Manager to receive notifications related to a set of services. By implementing interface ServiceDiscoveryListener and registering with a LookupCache, a Jini client can receive events indicating when a particular service has been discovered, when a ser- vice’s attributes have changed and when the service is removed from the LookupCache. This event notification is particularly useful for Jini clients that monitor available resources, such as our printer-monitoring example. Class ServiceDiscoveryManager also provides an enhanced interface that enables Jini clients to search for services using more specific search criteria. Jini clients can use class ServiceDiscoverManager with implementations of interface Service- ItemFilter to locate services whose attribute values fall within a particular range. For example, a Jini client could use a ServiceItemFilter to locate all automated teller machines in the area whose service charge is less than two dollars. Such a specific query is not possible using the standard ServiceTemplate matching available through interface ServiceRegistrar. For more information on class ServiceDiscoveryManager, please see the Jini API documentation included with the Jini Technology Core Platform. 22.9 Internet and World Wide Web Resources www.jini.org Home of the Jini community. www.sun.com/jini/specs/jini1.1html/coreTOC.html The site for Jini Technology Core Platform Specification. www.sun.com/jini/specs/jini1.1html/collectionTOC.html This site contains a collection of Jini Technology Helper Utilities and Services Specifications. www.sun.com/jini/specs/jini1.1html/jsTOC.html This site provides JavaSpaces Service Specification. developer.java.sun.com/developer/products/jini/installation.in- dex.html This site provides installation instructions for Jini technology. SUMMARY • Many network devices provide services to network clients. • Each service has a well-defined interface. • To use a service, a client must be able to discover that a service exists and must know the interface for interacting with the service. • Jini extends RMI to provide services to a network. 1254 Jini Chapter 22 • Jini services are plug-and-play—clients can discover services on the network dynamically, transpar- ently download classes required to use those services, then begin interacting with those services. • RMI’s dynamic class-downloading capability enables Jini clients to use services without installing special driver software for those services in advance. • For Jini clients to discover and use Jini services, standardized interfaces for common services must be developed. • The basic software requirements for Jini include the Java 2 Standard Edition (J2SE) and the Jini Technology Starter Kit. If you are going to write commercial Jini services and want to test their compatibility with the Jini platform, you also need to download the Jini Technology Core Platform Compatibility Kit (Jini TCK). • The Jini Starter Kit has three components—the Jini Technology Core Platform (JCP), the Jini Tech- nology Extended Platform (JXP) and the Jini Software Kit (JSK). The JCP contains the fundamental Jini interfaces and classes. The JXP provides helper utilities for implementing Jini services and cli- ents. The JSK contains an implementation of the services specified in the JCP and the JXP. • To compile and execute Jini services and clients the JAR files jini-core.jar, jini- ext.jar and sun-util.jar must be included in the CLASSPATH environment variable. These three JAR files are in the lib directory of the Jini Starter Kit—they correspond to the Jini Technol- ogy Core Platform, the Jini Technology Extended Platform and the Jini Software Kit, respectively. • The Jini distribution comes with three services that must be running correctly before executing Jini applications—a Web server to enable Jini clients to download class files through RMI, so the cli- ents can access Jini services dynamically; the RMI activation daemon (rmid) to enable the RMI infrastructure that allows Jini clients to communicate with Jini services; and a lookup service to maintain information about available Jini services, and to enable clients to discover and use those services. The Web server and rmid must be executing (they can be started in any order) before starting the lookup service. • The Jini Technology Core Platform implementation includes the StartService GUI tool for launching required services. • The Jini lookup service is the heart of a Jini community. The process of finding the lookup services and obtaining references to them is called discovery. • Discovery distinguishes Jini technology from RMI. In RMI, you must know in advance where to register an object. In Jini, you do not need to know where—just how. The discovery process de- termines where, but hides the details from the developer. • Discovery can be accomplished using either unicast discovery or multicast discovery. • Unicast discovery, or locator discovery, enables a Jini service or client to discover lookup services on a specific host. • Method getRegistrar of class LookupLocator performs unicast discovery. The method returns a ServiceRegistrar, which represents a lookup service. An overloaded version of method getRegistrar takes as an integer argument the maximum number of milliseconds to wait for the unicast discovery to locate a ServiceRegistrar before issuing a timeout. • Methods getHost and getPort of class LookupLocator retrieve the hostname and port number where a lookup service was discovered. • Multicast discovery, or group discovery, enables a Jini service or client to discover lookup services when the particular host running the lookup service is not known. A multicast discovery request uses network multicast to discover nearby lookup services. Lookup services periodically issue multicast announcements to notify interested Jini services and clients that the lookup services are available. •Class net.jini.discovery.LookupDiscovery performs multicast discovery. • Implementing interface DiscoveryListener enables an object of a class to receive Dis- coveryEvents—notifications of discovered lookup services. Chapter 22 Jini 1255 •Class LookupDiscovery invokes method discovered when LookupDiscovery locates new lookup services. • Method getRegistrars of DiscoveryEvent obtains an array of ServiceRegistrars. •Class LookupDiscovery invokes method discarded when a lookup service should be dis- carded because it is no longer available or because it no longer matches the set of groups in which the Jini service or client is interested. • A Jini service consists of several components, each of which contributes to the flexibility and porta- bility of the Jini architecture. A service proxy is an intermediary between a Jini service and its clients. The service proxy communicates with the actual service implementation through the service’s back- end interface, which defines methods in the service implementation. A separate application discovers lookup services and registers the Jini service, making the service available to Jini clients. • A Jini client uses the lookup service discovery techniques to discover lookup services. The Jini client then uses the discovered lookup services to locate the desired Jini service. When the lookup service locates the service requested by the Jini client, the lookup service serializes the service proxy and delivers the proxy to the Jini client. The client can then invoke methods defined in the service’s public interface directly on the service proxy, which implements that interface. The service proxy communicates with the service implementation through the back-end interface. •An Entry (package net.jini.core.entry) describes a service, which enables Jini clients to search for services of a particular description. • The lookup service requires a ServiceItem (package net.jini.core.lookup) to regis- ter a Jini service. • Jini helper utilities simplify the process of developing the Jini applications. These helper utilities provide high-level management capabilities. •Class LookupLocatorDiscovery enables a Jini service or client to discover lookup services on multiple known hosts. Class LookupLocatorDiscovery uses DiscoveryEvents to notify the Jini service or client of discovered lookup services. •Class LookupDiscoveryManager provides flexible lookup service discovery by enabling Jini applications and clients to perform both unicast and multicast lookup service discovery using a single class. • Entry attributes specify characteristics of Jini services. By attaching attributes to services, ser- vice providers can publish services with detailed information, such as the service location and the functionality of the service. Developers also can create custom attributes for Jini services. Class AbstractEntry provides a basic implementation of interface Entry. •An Entry class must supply a no-argument constructor. Also, instance variables must be pub- lic references to Serializable objects. • One goal of Jini technology is to make Jini communities “self-healing” and able to recover from common problems, such as network outages, hardware failures and software failures. Therefore, when a Jini service registers with a lookup service, the registration is not permanent. The registra- tion is leased for a specific amount of time, after which the lookup service revokes the registration. This prevents problematic services from disrupting the entire Jini community. • The leasing strategy that Jini employs is strict—if a Jini service does not renew its lease, the lookup service terminates the registration when the lease expires, making the service unavailable to clients. •Class LeaseRenewalManager is a Jini utility class that enables services to manage their leases to ensure that the service’s leases do not expire prematurely. •Class JoinManager is a utility class that facilitates the process of deploying a Jini service by performing lookup discovery, service registration and lease management in a single class. • Complex Jini clients often have specific requirements for the Jini services they employ. To satisfy these requirements, the Jini client often must work with sets of Jini services. The client searches 1256 Jini Chapter 22 through these services to locate the particular service that can satisfy the client’s needs. Class ServiceDiscoveryManager facilitates discovering available services and enables clients to perform finer-grained searches than are possible with the ServiceRegistrar interface. • There are three primary ways in which Jini clients use class ServiceDiscoveryManager— creating a local cache of services, receiving event notifications when services become available or unavailable and performing detailed searches not possible with simple ServiceTemplates. •Class ServiceDiscoveryManager can enhance Jini-client performance by creating a local cache of discovered services. This local cache—implemented as a LookupCache. TERMINOLOGY SELF-REVIEW EXERCISES 22.1 Fill in the blanks in each of the following statements: a) Name three required services for running Jini services and clients: , and . b) Two ways to discover lookup services are and . serviceAdded method of ServiceDiscoveryListener LeaseRenewalManager class locator discovery AbstractEntry class lookup method of ServiceDiscoveryManagercreateLookupCache method of ServiceDiscoveryManager lookup method of ServiceDiscoveryManagerdiscovery DiscoveryEvent class lookup method of LookupCache DiscoveryListener interface lookup service DiscoveryManagement class LookupCache interface Entry interface LookupDiscovery class getFrom method of LookupDiscoveryManager LookupDiscoveryManager class LookupLocator class getGroups method of LookupDiscovery LookupLocatorDiscovery class getHost method of class LookupLocator multicast discovery getPort method of class LookupLocator Name class getRegistrar method of class LookupLocator plug and play Reggie lookup service getRegistrars method of class LookupDiscovery renewFor method of LeaseRenewalManager group discovery renewUntil method of LeaseRenewalManagerJini Jini client ServiceDiscoveryListener interface Jini Software Kit ServiceDiscoveryManager class Jini Technology Core Platform Compatibility Kit ServiceID class Jini Technology Starter Kit serviceIDNotify method of ServiceIDListenerJini transaction manager service jini: URL ServiceItem class join protocol ServiceItemFilter interface JoinManager class ServiceRegistrar interface Lease class serviceRemoved method in ServiceDiscoveryListenerlease renewal service Lease.FOREVER constant ServiceTemplate class LeaseListener interface unicast discovery [...]... net.jini.space.JavaSpace; // Java core packages import java. io.*; import java. rmi.*; import java. net.*; Fig 23 .1 Discovering a JavaSpaces service (part 1 of 3) Chapter 23 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 JavaSpaces 126 3 // Java extension package import javax.swing.*; public class JavaSpaceFinder... new AttendeeCounter( day ); space.write( counter, null, Lease.FOREVER ); Writing an Entry into a JavaSpaces service (part 1 of 2) 126 8 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 JavaSpaces Chapter 23 output += "Initialize the Entry: \n"; output += " Day: " + day + "\n"; output += " Count:... the listener to the JavaSpaces service so that when a client writes a matching Entry into the JavaSpaces service, the JavaSpaces service will call notify Method notify (lines 35– 40) forwards the notification to the NotifyOperation application (Fig 23 .10) Chapter 23 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 JavaSpaces 127 7 // EntryListener .java. .. 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 JavaSpaces 127 9 // specify matching template, asks JavaSpace to // send notification when matching entry is written // to JavaSpace try { AttendeeCounter counter = new AttendeeCounter( day ); MarshalledObject handback = new MarshalledObject( "JavaSpace... Chapter 23 JavaSpaces 125 9 Outline 23 .1 Introduction 23 .2 JavaSpaces Service Properties 23 .3 23 .5 JavaSpaces Service JavaSpace Interface 23 .6 Defining an Entry 23 .4 23 .7 Discovering the JavaSpaces Service Write Operation 23 .8 Read and Take Operations 23 .8. 1 23 .8 .2 23.9 Read Operation Take Operation Notify Operation 23 .10 Method snapshot 23 .11 Updating Entries with Jini Transaction Service 23 .11.1 Defining... MarshalledObject) is an object that the JavaSpaces service provides to the remote listener as part of the notification 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 // NotifyOperation .java // This application receives a notification when a matching entry // is written to the JavaSpace package com.deitel.advjhtp1.javaspace.notify; // Jini core... count, null, JavaSpace.NO_WAIT ); if ( resultCounter == null) { output += "No Entry for " + day + " is available from the JavaSpace.\n"; } else { output += "Entry is taken away from "; output += "the JavaSpace successfully.\n"; } } Taking an Entry from a JavaSpaces service (part 2 of 4) Chapter 23 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94... Figure 23 .4 shows the results of running the WriteOperation application 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 // WriteOperation .java // This application initializes an new Entry, // and puts this Entry to the JavaSpace package com.deitel.advjhtp1.javaspace.write; // Jini core packages import net.jini.core.lease.Lease;... permissions 1 2 3 4 5 6 7 8 // TakeOperation .java // This application removes an Entry from the JavaSpace package com.deitel.advjhtp1.javaspace.take; // Jini core packages import net.jini.core.transaction.TransactionException; import net.jini.core.entry.UnusableEntryException; Fig 23 .7 Taking an Entry from a JavaSpaces service (part 1 of 4) 127 4 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30... net.jini.space.JavaSpace; // Java core packages import java. rmi.RemoteException; // Java extension package import javax.swing.*; // Deitel packages import com.deitel.advjhtp1.javaspace.common.*; public class SnapshotUsage { private JavaSpace space; Fig 23 . 12 Removing entries from JavaSpaces service using method snapshot (part 1 of 3) 1 28 2 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 . SeminarServiceJoinManager.jar Seminar.class comdeiteladvjhtp1jiniseminar SeminarInterface.class comdeiteladvjhtp1jiniseminarservice SeminarProxy.class comdeiteladvjhtp1jiniseminarservice Fig. 22 . 38 Fig. 22 .38Fig. 22 . 38 Fig. 22 . 38 SeminarServiceJoinManager.jar contents (part 1 of 2) . Fig. 22 .37 Fig. 22 .37Fig. 22 .37 Fig. 22 .37 SeminarInfoJoinService uses class JoinManager to facilitate. notifications BackendInterface.class comdeiteladvjhtp1jiniseminarservice SeminarInfo.class comdeiteladvjhtp1jiniseminarservice SeminarInfo_Stub.class comdeiteladvjhtp1jiniseminarservice SeminarProvider.class comdeiteladvjhtp1jiniutilitiesentry SeminarInfoJoinService.class comdeiteladvjhtp1jiniutilitiesjoin Class File Directory in SeminarServiceJoinManager.jar Fig. 22 . 38 Fig. 22 .38Fig. 22 . 38 Fig. 22 . 38 SeminarServiceJoinManager.jar contents (part 2 of 2) . Chapter 22 Jini 125 3 when services become. 3). Chapter 23 JavaSpaces 126 3 18 // Java extension package 19 import javax.swing.*; 20 21 public class JavaSpaceFinder { 22 23 private JavaSpace space; 24 25 public JavaSpaceFinder( String jiniURL ) 26

Ngày đăng: 09/08/2014, 12:23

TỪ KHÓA LIÊN QUAN