Tutorial summary: Learn how to create and run a Processing Unit - a scalable unit of deployment, inside your development environment. Learn how to use the GigaSpaces basic API, by implementing a simple processor and feeder application. Approx 10 min Overview
Before you beginWe recommend that you go through the following steps before you begin this tutorial:
GoalsCreate a scalable processor application (a Processing Unit) that processes objects as they are written to the space (data grid). Create a feeder application that feeds objects to the space (data grid) for the processor to read and process. Contents
ComponentsThere are two components in our scenario:
blank-line
Code WalkthroughFirst let's take a look at the Message object that is being written to the space by the feeder application: The Message Object (Message.java)This is a simple POJO containing two attributes: id, which represents the object id, and info, which represents the information that this object holds. Both have setter and getter methods.
private Integer id; // object id public void setId(Integer id) { this.id = id; } @SpaceRouting public Integer getId() { return id; } private String info; // info represents the info the object holds public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } A necessary default empty constructor and another constructor to construct public Message() { // Mandatory empty constructor } public Message(Integer id, String info) { this.id = id; this.info = info; } Next, let's take a look at the Processor Processing Unit. The Processor Processing Unit (pu.xml, Processor.java)The Processor Processing Unit contains two components: a space (cache), which holds objects in memory, and a processor bean that takes, modifies and writes objects back to this space. Processor Processing Unit Configuration (META-INF/spring/pu.xml) A Processing Unit always has an XML file called pu.xml, that resides under the META-INF\spring directory.
The third, key component in this workflow is the Polling Container, which continuously removes (takes) objects matching certain criteria from the space. The criteria are expressed in the form of a template object (also known as example object). In our case, the polling container is instructed to take objects of type Message. However, it does not take all instances of the Message class, only those whose "info" property equals the string "Hello ". When a match is found, the object is taken and passed to a listener bean - here the listener is the previously defined Processor bean. This bean has a method annotated with the @SpaceDataEvent annotation, which is invoked with the taken object as a parameter. It returns a processed Message object, which is written back to the space by the Polling Container. <os-events:polling-container id="helloProcessorPollingEventContainer" giga-space="gigaSpace"> <os-events:tx-support tx-manager="transactionManager"/> <os-core:template> <bean class="org.openspaces.example.helloworld.common.Message"> <property name="info" value="Hello "/> </bean> </os-core:template> <os-events:listener> <os-events:annotation-adapter> <os-events:delegate ref="helloProcessor"/> </os-events:annotation-adapter> </os-events:listener> </os-events:polling-container> Next we'll see the source code for the Processor bean.
We saw that the pu.xml file defines a bean called Processor. Now let's look at this bean's source code. public class Processor { @SpaceDataEvent public Message processMessage(Message msg) { System.out.println("Processor PROCESSING : " + msg); msg.setInfo(msg.getInfo()+"World !!"); return msg; } public Processor(){ System.out.println("Processor instantiated..."); } } Now we are ready to view feeder application that feeds Message objects to the space. blank-line The Feeder Application (Feeder.java)The feeder main method constructs a new Feeder instance, and passes the space URL to it, to connect to the space. public static void main(String [] args) { if(args.length==0){ System.out.println("Usage: java Feeder <space URL>"); System.exit(1); } Feeder feeder = new Feeder (args[0]); // create the feeder and connect it to the space feeder.feed(1000); // run the feeder (start feeding) feeder.readResults(); // read back results } Here's the constructor of the Feeder connects to the Processor Processing unit Space by using the input URL: public Feeder(String url){ // Connect to a space using the url IJSpace space = new UrlSpaceConfigurer(url).space(); // Wrap the space with the gigaSpace API this.gigaSpace = new GigaSpaceConfigurer(space).gigaSpace(); } The feed() method loops and writes Message objects to the space by using the gigaSpace.write() method: public void feed(int numberOfMessages){ for(int counter=0;counter<numberOfMessages;counter++){ Message msg = new Message(counter, "Hello "); gigaSpace.write(msg); } System.out.println("FEEDER WROTE " + numberOfMessages + " messages"); } Here's how all processed objects are read from the space, using template matching. The number of processed objects in the space (all of them should have their info property set to "Hello World !!") is then printed out: public void readResults(){ Message template = new Message(); // Create a template to read a Message with info template.setInfo("Hello World !!"); // attribute that equals "Hello World !!" // Read an object matching the template System.out.println("Here is one of them printed out: "+gigaSpace.read(template)); //wait 100 millis for all to be processed: try{ Thread.sleep(100); }catch(InterruptedException ie){ /*do nothing*/} // Count number of objects in the space matching the template int numInSpace=gigaSpace.count(template); System.out.println("There are "+numInSpace+" processed Message objects in the space now."); } Next, we compile and run the sample application Compiling and Running the Application within your IDE
If you haven't already done so,download GigaSpaces and set up your development environment
Running the Processor
6. Before running the feeder, you should wait for the following output to appear in the Console tab at the bottom of the screen: Processor instantiated, waiting for messages feed... This indicates the Processor is up and running.
10. Click the Run button blank-line
Expected outputRunning the processor and the feeder results in the following output, which can be viewed in the Console tab at the bottom of the screen. Feeder expected output The feeder starts, writes 100 message objects to the space, reads and prints one of them at random, and finally prints the number of processed messages in the space: Starting the Feeder (Will wait for the space to initialize first...) FEEDER WROTE 1000 objects Here is one of them printed out: id[47] info[Hello World !!] There are 841 processed Message objects in the space now. Press any key to continue . . . blank-line Processor expected output The processor prints the id and info attributes for each messages it takes for processing: Processor PROCESSING : id[445] info[Hello ] Processor PROCESSING : id[904] info[Hello ] Processor PROCESSING : id[896] info[Hello ] Processor PROCESSING : id[446] info[Hello ] Processor PROCESSING : id[889] info[Hello ] . . . Processor PROCESSING : id[893] info[Hello ] Processor PROCESSING : id[905] info[Hello ] Processor PROCESSING : id[897] info[Hello ] Processor PROCESSING : id[875] info[Hello ] Processor PROCESSING : id[900] info[Hello ] blank-line What's Next?
Or return to the Quick Start Guide. |
![]() |
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence |