/*
 * Copyright 2008 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.
 */

package com.gigaspaces.examples.tutorials.queries.codebasedvalidator;

import org.openspaces.core.GigaSpace;
import org.openspaces.core.GigaSpaceConfigurer;
import org.openspaces.core.space.UrlSpaceConfigurer;
import org.openspaces.events.notify.SimpleNotifyContainerConfigurer;
import org.openspaces.events.notify.SimpleNotifyEventListenerContainer;
import org.openspaces.events.polling.SimplePollingContainerConfigurer;
import org.openspaces.events.polling.SimplePollingEventListenerContainer;

import com.j_spaces.core.IJSpace;
import com.j_spaces.core.client.SQLQuery;

import com.gigaspaces.examples.tutorials.queries.common.OrderEvent;
import com.gigaspaces.examples.tutorials.queries.validator.NormalOrderEventValidator;
import com.gigaspaces.examples.tutorials.queries.validator.InsecureRiskyOrderEventCounter;

/**
 * CodeBasedValidator
 * <p> 
 * The validator connects to the remote space and:
 * 1. Registers a notify container that receives by notification "Insecure"&"New"
 *    orders using query, examines and counts according to their risk.
 * 2. Starts a polling container that takes "Normal"&"New" orders using query,
 *    validates them and writes them back to the space as approved/rejected.
 * 
 * This "CodeBased" version of the validator is configured inside the main() method.
 * The Processing Unit version of the validator uses a pu.xml configuration file and can 
 * ran inside a stand alone container or onto GigaSpaces ServiceGrid. 
 */
public class CodeBasedValidator {

	public static IJSpace space;
	public static GigaSpace gigaSpace;
	
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		
		// Connect to the space
		// ====================
		// Connects to the remote space using its URL	
		space = new UrlSpaceConfigurer("jini://*/*/spaceQueries").space();
        // Create a GigaSpace simpler interface to interact with the space
		gigaSpace = new GigaSpaceConfigurer(space).gigaSpace();
		
		
		// Create a Notify event container registered for notification when an orderEvent object
		// matching the query is written to the space.
		//============================================
		// Create a SQLQuery to query orderEvent objects with attributes status="New" and type="Insecure"
		SQLQuery<OrderEvent> queryInsecureOrders = new SQLQuery<OrderEvent>(new OrderEvent(),"type='"+OrderEvent.TYPE_INSECURE+"' and status='"+OrderEvent.STATUS_NEW+"'");
		// Create the Notify event container
		SimpleNotifyEventListenerContainer notifyEventListenerContainer = 
			new SimpleNotifyContainerConfigurer(gigaSpace) /* The space the notify container is connected to */
        		.template(queryInsecureOrders) /* The query to match */
        		.eventListenerAnnotation(new InsecureRiskyOrderEventCounter()) /* The listener class containing the method to invoke upon notification (annotated @SpaceDataEvent) */
        		.notifyContainer();
		
		
		// Create a Polling event container to periodically try to take 
		// orderEvent objects matching the query, from the space.
		//=======================================================
		// Create a SQLQuery to query orderEvent objects with attributes status="New" and type="Normal"
		SQLQuery<OrderEvent> queryNormalOrders = new SQLQuery<OrderEvent>(new OrderEvent(),"type='"+OrderEvent.TYPE_NORMAL+"' and status='"+OrderEvent.STATUS_NEW+"'");
		// Create the Polling event container
		SimplePollingEventListenerContainer pollingEventListenerContainer = 
			new SimplePollingContainerConfigurer(gigaSpace) /* The space the polling container is connected to */
        		.template(queryNormalOrders) /* The query to match */
        		.eventListenerAnnotation(new NormalOrderEventValidator()) /* The class containing the method to invoke upon match (annotated @SpaceDataEvent) */
        		.pollingContainer();
	}
}
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence