Parallel Processing A - Plain JavaSpaces - PlainJavaSpaces.java

Source Code for Example: Parallel Processing Tutorial A – Plain JavaSpaces
Pure JavaSpaces application which connects to the space, writes and reads Entries under a transaction.
Download source code (PlainJavaSpaces.java)
/*
 * Copyright 2007 GigaSpaces Technologies Ltd. All rights reserved.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY AND 
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. GIGASPACES WILL NOT 
 * BE LIABLE FOR ANY DAMAGE OR LOSS IN CONNECTION WITH THE SOFTWARE.
 */
/**
 * Title:        	JavaSpaces Hello World Example
 * Description:  	This example provide a basic illustration for 
 * 					getting GigaSpaces space proxy using jini discovery LookupLocator.
 * 					Using some of JavaSpaces basic operations such as write, read, take with
 * 					or without jini transactions.
 */

package com.gigaspaces.examples.tutorials.plainjavaspaces;

import net.jini.core.lease.Lease; 	// The Lease interface defines a type of object that is returned to the 
									// lease holder and issued by the lease grantor.

import net.jini.space.JavaSpace; 	// This interface is implemented by servers that export a JavaSpaces(TM) technology service. 
									// the operations in this interface are the public methods that all such spaces support.

import net.jini.core.transaction.Transaction;	// Interface for classes representing transactions returned by 
												// TransactionManager servers for use with transaction participants 
												// that implement the default transaction semantics.

public class PlainJavaSpaces {

		public static void say (String msg) {
	  	  	System.out.println(msg);
		}
		
		static public void main(String[] args) throws Exception {
			System.err.println("\nWelcome to the GigaSpaces PlainJavaSpaces "+
				"Example\n");
			
			//	Checks if any arguments were passed from batch file.
			if (args.length != 1) {
				// No argumants passed - the app will exit.
				System.out.println("Usage: <URL>");
				System.out.println("<protocol>://host:port/containername/spacename");
				System.exit(1);
			}
			
			//	Finding the space 
			//	(args[0] is passed to the application from the running script "run.bat", it holds the space name
			JavaSpace space = JiniSpaceAccessor.findSpace(args[0]);

			// Checking to see if space found
			if ( space == null ) {	
			  System.err.println("Space url:"+args[0]);
			  System.err.println("Space not found...");
			  System.exit(-1);
			}
			say("Found space: " + args[0]);
						
			// Writing to space
			// ----------------
			// Creating a message entry.
			Message message = new Message();
			message.ID = 1;
			message.content = "My first PlainJavaSpaces Message!";
			say("\nWriting the following message to the space: \n" + message.toString());
			// Writing the message to the space not under transaction and without expiration time.
			space.write(message, null, Lease.FOREVER);
			
			//	Writing another message
			message.ID = 2;
			message.content = "My second PlainJavaSpaces Message!";
			say("\nWriting the following message to the space: \n" + message.toString());
			space.write(message, null, Lease.FOREVER);
			
			//	Reading from space
			//	------------------
			//	Creating a template to read a message from the space.
			//	The template fields not nulled define which entry to read by matching.
			Message template = new Message();
			template.ID = 1;
			say("\nTrying to read from the space message with ID = " + template.ID);
			// Trying to read a message entry from the space not under transaction and without waiting (blocking). 
			Message result = (Message)space.read(template, null, JavaSpace.NO_WAIT);
			if (result != null){
				say("Read message: " + result.toString());
			}
			
			//	Taking from space
			//  -----------------
			// 	Using a template to take a message from the space.
			//	The take operation chooses the entry by matching the fields not set as null in the template
			//	in this case the template ID field is set to 1.
			//	Take operation removes the message entry from the space as apposed to read operation which
			//	only reads it.
			template.ID = 2;
			say("\nTrying to take from the space message with ID = " + template.ID);
			// Trying to take a message entry from the space not under transaction and without waiting (blocking). 
			result = (Message)space.take(template, null, JavaSpace.NO_WAIT);
			if (result != null){
				say("Taken message: " + result.toString());
			}
			
			//	Reading from space (Blocking)
			// 	-----------------------------
			//	Attempting to read a message with ID = 2 that was taken in the previous step,
			//	because the wait time is set to 5 sec, the application blocks while waiting to read.
			say("\nTrying to read from the space message with ID = " + template.ID);
			// Trying to read a message entry from the space (not under transaction) until found or 5 sec timeout. 
			result = (Message)space.read(template, null, 5 * 1000);
			if( result != null) 
	        	say("Read message: " + result.toString());
	        else 
	        	say("Message not found after 5 sec.");
			
			//	Writing to space with a lease
			// 	-----------------------------
			//	Writing to the space a leased message entry with expiration time, 
			//	then trying to read the message during the lease period and after the lease has expired.
			message.ID = 3;
			message.content = "My first leased message!";
			say("\nWriting message to space leased for 5 sec.");
			// Writing the message to the space not under transaction and with 5 min expiration time.
			space.write(message, null, 5 * 1000);
			say("Trying to read from the space the leased message.");
			template.ID = 3;
			result = (Message)space.read(template, null, JavaSpace.NO_WAIT);
			if( result != null) 
	        	say("Read message: " + result.toString());
	        else 
	        	say("Message not found.");
			System.out.println("Sleeping for 6 seconds. . .");
			Thread.sleep(6 * 1000);
			say("Trying to read from the space the leased message.");
			result = (Message)space.read(template, null, JavaSpace.NO_WAIT);
			if( result != null) 
	        	say("Read message: " + result.toString());
	        else 
	        	say("Message not found, Lease expired.");
			
			//	Space operations under transaction
			//	----------------------------------
			say("\nSpace operations under transaction");
			say("----------------------------------");
			//	Transaction lease will expire after one minute (automatic abort / rollback)
	        say("Finding a Jini transaction manager and creating a transaction proxy.");
			Transaction.Created txn = TransactionHelper.getInstance().getJiniTransaction(60 * 1000);
	        say("Transaction manager found.");
			
			//	Writing a message to the space under transaction
	        message.ID = 4;
	        message.content = "Hello Transaction!";
	        say("Write message under a transaction...");
	        space.write(message, txn.transaction, Lease.FOREVER);
	        
	        //	Abort the transaction and try to read the message to show that it wasn't written
	        say("Abort transaction...");
	        txn.transaction.abort(1000);
	        say("Try to read the message written under transaction.");
	        template.ID = 4;
	        result = (Message)space.read(template, null, 1000);
	        if( result != null) 
	        	say("Message found: " + result.toString());
	        else 
	        	say("Message Entry not in space");
	        
	        //	Writing a message to the space under transaction
			//	commiting the transaction and trying to read the message.
	        say("\nFinding a Jini transaction manager and creating a transaction proxy.");
			txn = TransactionHelper.getInstance().getJiniTransaction(60 * 1000);
			say("Transaction manager found");
	        say("Write again a message under a transaction...");
	        message.content = "Hello Transaction another try";
	        space.write(message, txn.transaction, Lease.FOREVER);
	        say("Commiting transaction...");
	        txn.transaction.commit(1000);
	        say("Try to read the message written under transaction");
	        template.ID = 4;
	        result = (Message)space.read(template, null, 1000);
	        if( result != null) 
	        	say("Message read content: \n" + result.toString());
	        else 
	        	say("Message Entry not in space");
	    
			System.exit(0);
		}		
}

Labels

 
(None)