/*
* 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.counter;
import java.util.concurrent.atomic.AtomicInteger;
import org.openspaces.core.GigaSpace;
import org.openspaces.core.IteratorBuilder;
import org.openspaces.core.context.GigaSpaceContext;
import org.springframework.beans.factory.InitializingBean;
import com.gigaspaces.examples.tutorials.queries.common.Account;
import com.gigaspaces.examples.tutorials.queries.common.OrderEvent;
import com.j_spaces.core.client.GSIterator;
import com.j_spaces.core.client.SQLQuery;
/**
* Simple bean used to read and display the "Insecure" orderEvent objects.
* Shows use of GSIterator.
*/
public class RiskyOrderEventCounter implements InitializingBean{
private AtomicInteger riskyOrderEventCounter = new AtomicInteger(0);
@GigaSpaceContext(name = "gigaSpace")
private GigaSpace gigaSpace;
public void setGigaSpace(GigaSpace gigaSpace) {
this.gigaSpace = gigaSpace;
}
/**
* The first method to run upon bean Initialization when implementing InitializingBean.
* Runs the init() method
*/
public void afterPropertiesSet() throws Exception {
init();
}
/**
* Starts a new thread running the OrderEventIteratorTask.
*/
public void init(){
Thread thread = new Thread(new OrderEventIteratorTask());
thread.start();
}
/**
* Iterates through all the orderEvents matching the specified templates.
* Counts every orderEvent read, that has an associated account (same user name as the orderEvent)
* with a satisfying risk allowed.
*/
public class OrderEventIteratorTask implements Runnable {
SQLQuery<OrderEvent> queryInsecureOrders = new SQLQuery<OrderEvent>(OrderEvent.class,"status='"+OrderEvent.STATUS_NEW+"' and type='"+OrderEvent.TYPE_INSECURE+"'");
IteratorBuilder iteratorBuilder = new IteratorBuilder(gigaSpace)
.addTemplate(queryInsecureOrders)
.bufferSize(100) .withHistory();
public void run()
{
try
{
System.out.println("Counter creats iterator over the space insecure orders:");
GSIterator gsIterator = iteratorBuilder.iterate();
System.out.println("Counter iterator thread reading messages");
OrderEvent insecureOrderEvent;
SQLQuery<Account> accountQuery = new SQLQuery<Account>(Account.class,"firstName = ? and lastName = ? and riskAllowed > ?");
while (true)
{
while (gsIterator.hasNext())
{
insecureOrderEvent = (OrderEvent)gsIterator.next();
System.out.println("\nCounter examines order: "+insecureOrderEvent);
accountQuery.setParameters(insecureOrderEvent.getFirstName(), /* account user first name*/
insecureOrderEvent.getLastName(), /* account user last name*/
insecureOrderEvent.getRiskInvolved()); /* account amount*/
Account account = (Account)gigaSpace.read((Object)accountQuery);
if (account != null)
{
System.out.println("Counter - examined order's risk is higher then allowed.");
riskyOrderEventCounter.incrementAndGet();
}
else
{
System.out.println("Counter - examined order's risk is allowed.");
}
System.out.println("Counter - total of ["+riskyOrderEventCounter+"] insecure risky orders (with risk higher then allowed) counted.");
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}