Summary: Writing your first XAP.NET application
OverviewThis page explains how to create a simple Hello World application using XAP.NET API. The application is very simple: connect to a space, store an entry and retrieve it. The application shown here is also available in the examples distributed with the product (from the Windows Start menu, select Programs > GigaSpaces XAP.NET > Examples and review the HelloWorld example).
PrerequisitesIf you haven't done so yet, please download and install GigaSpaces XAP.NET. Setting Up the ProjectThis section shows how to create a C# Console Application named HelloWorld with a reference to the GigaSpaces.Core assembly. Create a Console Application Project
Add a Reference to the GigaSpaces Core Assembly
Writing the CodeThis section shows how to write a simple program that connects to the space, stores a Message object with some text, and retrieves it. Creating the Message ClassWe want to demonstrate storing some object to the space. To do this, let's create a simple Message class with a Text property of type String.
Getting StartedThe XAP.NET API used in this example is located in the GigaSpaces.Core namespace. using GigaSpaces.Core; Connecting to the SpaceWe need to establish a connection to a space which stores the object. To do this, we use the FindSpace method from a factory class called GigaSpacesFactory. This takes a URL of the requested space, and returns a space proxy of type ISpaceProxy. Since we don't have any spaces running yet, we use a special URL prefix to indicate that we want the space lookup to occur in-process, and that the searched space should be created in-process if it doesn't exist yet. The space name is myEmbeddedSpace (when the space and the proxy reside in the same process, the space is called an embedded space). Edit the Main method and add the following code: String spaceUrl = "/./myEmbeddedSpace"; // Connect to space: Console.WriteLine("*** Connecting to space using \"" + spaceUrl + "\"..."); ISpaceProxy proxy = GigaSpacesFactory.FindSpace(spaceUrl); Console.WriteLine("*** Connected to space."); Storing a Message ObjectThe next step is to create a Message object, and store it in the space. To do this, we use the Write method in the ISpaceProxy we've just created, and simply pass the object we want to store as an argument: Add the following code to the Main method, after the previous code: // Write a message to the space: Message outgoingMessage = new Message("Hello World"); Console.WriteLine("Writing Message [" + outgoingMessage.Text + "]"); proxy.Write(outgoingMessage); Retrieving the Stored MessageFinally, we want to retrieve the object from the space. To do this, we use the Take method in ISpaceProxy, which takes a template argument and searches for a matching entry in the space. If a match is found, it is removed from the space, and returned to the caller, otherwise null is returned. A template is an object of the type we wish to query, where the null properties are ignored and the non-null properties are matched. For example, creating a Message, and setting the Text to "Goodbye" returns null, because the space does not contain such an object. We use a new Message, without setting the Text property, which matches all possible entries of type Message (of course, we know there's currently only one in the space). Add the following code to the Main method, after the previous code: // Take a message from the space: Message incomingMessage = proxy.Take(new Message()); Console.WriteLine("Took Message [" + incomingMessage.Text + "]"); Console.WriteLine("Press ENTER to exit."); Console.ReadLine(); Running the ProgramTo run the program, from the Debug menu, select Start Debugging. The following shows the complete program code, with some minor modifications: using System; using System.Collections.Generic; using System.Text; using GigaSpaces.Core; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine("Welcome to GigaSpaces.NET Hello World Example!" + Environment.NewLine); try { // Get space URL from command argument, (use default if none): string spaceUrl = (args.Length > 0 ? args[0] : "/./myEmbeddedSpace"); // Connect to space: Console.WriteLine("*** Connecting to space using \"" + spaceUrl + "\"..."); ISpaceProxy proxy = GigaSpacesFactory.FindSpace(spaceUrl); Console.WriteLine("*** Connected to space."); Console.WriteLine(); // Write a message to the space: Random random = new Random(); Message outgoingMessage = new Message("Hello World " + random.Next(1, 1001)); Console.WriteLine("Writing Message [" + outgoingMessage.Text + "]"); proxy.Write(outgoingMessage); // Take a message from the space: Message incomingMessage = proxy.Take(new Message()); Console.WriteLine("Took Message [" + incomingMessage.Text + "]"); proxy.Dispose(); Console.WriteLine(Environment.NewLine + "Hello World Example finished successfully!" + Environment.NewLine); } catch (Exception ex) { Console.WriteLine(Environment.NewLine + "Hello World Example failed: " + ex.ToString()); } Console.WriteLine("Press ENTER to exit."); Console.ReadLine(); } } } |
![]() |
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence |