|
Summary: Understanding and running the .NET Hello World example at <GigaSpaces-Root>/dotnet/examples/hello-world.
Use Space-Based .NET API OverviewThis example is located at: <GigaSpaces ROOT>\dotnet\examples\hello-world Hello World ExampleThe Hello World example demonstrates how you can use a preexisting, custom .NET type together with a Gigaspaces space in .NET.
Application Classusing com.j_spaces.core ; using com.j_spaces.core.client ; using net.jini.core.lease ; using net.jini.core.transaction ; using System ; using System.Threading ; using net.jini.core.transaction.server; /// /// @brief The C# Hello World example /// /// This is a heavily documented example that illustrates some very basic /// characteristics of the C# .NET version of the JavaSpaces bindings . /// /// This example demonstrates the use of a custom Java <c>Entry</c> type with /// a Gigaspaces JavaSpace. We generated a proxy type for the Java <c>Message</c > /// type and are therefore able to use it with the same ease as the Gigaspaces /// framework and the Java library types . /// public class Application { public static void Main( string[] args ) { if( args.Length != 1 ) { Console.Error.WriteLine( "Usage: <URL >" ); Console.Error.WriteLine( "rmi://localhost/./mySpace " ); } else { try { // set up the Java runtime environment (defined in 'Example.cs ') // // The InitJvm() function has to at least initialize // - the classpath , // - the JVM path (unless you wish to rely on a default JVM we might or might not find ), // - the security manager // - the security policy file // // You can perform these steps in code or rely on a configuration file , // it's up to you. All these cases are documented in the 'rtconfig' examples. // Example.InitJvm( args ); // Lookup the JavaSpace, just as in the Java example IJSpace space = (IJSpace)SpaceFinder.find( args[ 0 ] ); if ( space == null ) { Console.Error.WriteLine ( "Space not found: {0}", args[ 0 ] ); return ; } // register our type with the Gigaspaces runtime // making a snapshot as the first use of the Person type // is an easy way to register the metadata Person preg = new Person (); ExternalEntry regEntry = PersonMarshaller.Marshal( preg ); space.snapshot( regEntry ); // create a Local transaction manager LocalTransactionManager ltm = new LocalTransactionManager( space ); // create a Jini Distributed transaction manager java.lang.Class txclass = java.lang.Class.forName ("net.jini.core.transaction.server.TransactionManager"); TransactionManagerImpl dist_tm = (TransactionManagerImpl) LookupFinder.find( null, // service name new java.lang.Class[] { txclass }, // service class name null, // service attributes "localhost", // unicast lookup host null, // lookup groups 10*1000); // timeout 10 seconds // lets create local transaction from the local Tx manager TransactionImpl.Created tCreated = TransactionFactory.create( ltm, 3600 ); Transaction txn = tCreated.transaction ; // the p instance represents our .Net data; 'Person' is a pure C# // data type and will therefore have to be marshalled into a generic // entry instance. The best way to do that is to write a 'marshal ' // method that can perform this task as a reusable utility . Person p = new Person (); p.id = "011-1111111 "; p.name = "Alex Krapf "; p.age = 38 ; p.birthDay = "01/01/1967 "; p.height = 6.2 ; p.bMarried = true ; p.phone = 9783698583L ; p.weight = (float)200.5 ; ExternalEntry ee = PersonMarshaller.Marshal( p ); // implemented in 'Person.cs ' Console.WriteLine( "\nWriting Person to Space\n " ); space.write( ee, txn, LeaseImpl.FOREVER ); // commit the transaction txn.commit (); // after the take operation, we end up with a generic entry instance ; // for easier usability, we then call our custom unmarshal function to // put the information into a properly .Net type instance Console.WriteLine( "\nTaking Person from Space\n " ); ExternalEntry tmplate = new ExternalEntry( "Person", null, null ); ExternalEntry resultRead = (ExternalEntry)space.read ( tmplate, null, LeaseImpl.FOREVER ); ExternalEntry resultTake = (ExternalEntry)space.take ( tmplate, null, LeaseImpl.FOREVER ); Person result = PersonMarshaller.Unmarshal ( resultTake ); // print the taken Person if( result != null ) Console.WriteLine( result.ToString () ); // let the notification arrive //Thread.Sleep( 2000 ); } catch( FinderException ex ) { // you can write hierarchical exception catch blocks, just like in Java // and you have full access to the entire exception information and // functionality Console.Error.WriteLine( "Could not find space: {0}", args[ 0 ] ); Console.Error.WriteLine( "Please check that GigaSpaces Server is running ." ); // 'Message' and 'StackTrace' properties are available as expected Console.Error.WriteLine( "Message: {0}", ex.Message ); Console.Error.WriteLine( "Stacktrace: {0}", ex.StackTrace ); return ; } catch( java.lang.Exception e ) { // another way to handle an exception ... e.printStackTrace (); return ; } catch( System.Exception e ) { // it's always good to catch .NET exceptions as well // to account for normal .NET things going wrong Console.Error.WriteLine( "Message: {0}", e.Message ); Console.Error.WriteLine( "Stacktrace: {0}", e.StackTrace ); return ; } } } } Person Classusing System.Text ; using com.j_spaces.core.client ; using java.lang ; /// <summary > /// A custom data type, representing one of your data types /// that you now wish to use with the GigaSpaces platform . /// </summary > /// <remarks > /// This type publishes some public fields (this is not /// a requirement, just a shortcut to make the program simple ). /// Please ignore the suitability of the respective field types /// for their designated purpose, we're just trying to illustrate /// how different types are used . ///</remarks > public class Person { public string id ; public string name ; public int age ; public string birthDay ; public long phone ; public bool bMarried ; public float weight ; public double height ; public Person () { } public override string ToString () { StringBuilder builder = new StringBuilder (); builder.Append( "--------------------------------\n " ); builder.Append( " id : " + id + "\n " ); builder.Append( " name : " + name + "\n " ); builder.Append( " age : " + age.ToString() + "\n " ); builder.Append( " bday : " + birthDay + "\n " ); builder.Append( " phone : " + phone.ToString() + "\n " ); builder.Append( " marit : " + ( bMarried ? "yes" : "no" ) + "\n " ); builder.Append( " weight : too high, no matter what the space says :-)\n " ); builder.Append( " height : " + height.ToString() + "\n " ); return builder.ToString (); } } PersonMarshaller Class/// <summary > /// A utility type that we wrote to make the preexisting <c>Person</c> type /// usable easily with the Gigaspaces platform . /// </summary > /// <remarks > /// You have to write such a type once or, alternatively, use a reflection-based /// mechanism to populate the object on the other side. Eventually, we will provide /// a generic marshaller within the framework to save you this work . /// </remarks > public class PersonMarshaller { private static bool bTypeInitialized = false ; public static ExternalEntry Marshal( Person p ) { ExternalEntry result = null ; ObjectArray values = new ObjectArray( 8 ); StringArray fields = null ; StringArray types = null ; if( p != null ) { values[ 0 ] = p.age != 0 ? new java.lang.Integer( p.age ) : null ; values[ 1 ] = p.birthDay != null ? new java.util.Date ( java.util.Date.parse( p.birthDay ) ) : null ; values[ 2 ] = p.height != 0 ? new java.lang.Double( p.height ) : null ; values[ 3 ] = p.id ; values[ 4 ] = p.bMarried ? java.lang.Boolean.TRUE : java.lang.Boolean.FALSE ; values[ 5 ] = p.name ; values[ 6 ] = p.phone != 0 ? new java.lang.Long( p.phone ) : null ; values[ 7 ] = p.weight != 0 ? new java.lang.Float( p.weight ) : null ; } if( !bTypeInitialized ) { fields = new StringArray( 8 ); types = new StringArray( 8 ); fields[ 0 ] = "age "; fields[ 1 ] = "birthDate "; fields[ 2 ] = "height "; fields[ 3 ] = "id "; fields[ 4 ] = "married "; fields[ 5 ] = "name "; fields[ 6 ] = "phone "; fields[ 7 ] = "weight "; types[ 0 ] = "java.lang.Integer "; types[ 1 ] = "java.util.Date "; types[ 2 ] = "java.lang.Double "; types[ 3 ] = "java.lang.String "; types[ 4 ] = "java.lang.Boolean "; types[ 5 ] = "java.lang.String "; types[ 6 ] = "java.lang.Long "; types[ 7 ] = "java.lang.Float "; result = new ExternalEntry( "Person", values, fields, types ); bTypeInitialized = true ; } else result = new ExternalEntry( "Person", values ); return result ; } public static Person Unmarshal( ExternalEntry ee ) { if( ee == null ) return null ; else { Person result = new Person (); java.lang.Integer age = (java.lang.Integer)ee.m_FieldsValues[ 0 ]; if( age != null ) result.age = age.intValue (); else result.age = 0 ; java.util.Date bd = (java.util.Date)ee.m_FieldsValues[ 1 ]; if( bd != null ) result.birthDay = bd.ToString (); else result.birthDay = null ; java.lang.Double height = (java.lang.Double)ee.m_FieldsValues[ 2 ]; if( height != null ) result.height = height.doubleValue (); else result.height = 0 ; result.id = (string)ee.m_FieldsValues[ 3 ]; java.lang.Boolean married = (java.lang.Boolean)ee.m_FieldsValues[ 4 ]; if( married != null ) result.bMarried = married.booleanValue (); else result.bMarried = false ; result.name = (string)ee.m_FieldsValues[ 5 ]; java.lang.Long phone = (java.lang.Long)ee.m_FieldsValues[ 6 ]; if( phone != null ) result.phone = phone.longValue (); else result.phone = 0 ; java.lang.Float weight = (java.lang.Float)ee.m_FieldsValues[ 7 ]; if( weight != null ) result.weight = height.floatValue (); else result.weight = 0 ; return result ; } } } |
GigaSpaces 6.0 Documentation Contents (Current Page in Bold)
(works on Firefox 2 and Internet Explorer 7)
Labels
(None)