/*
* 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.parallelprocessing.client;
import org.openspaces.core.GigaSpace;
import org.openspaces.core.context.GigaSpaceContext;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import com.gigaspaces.examples.tutorials.parallelprocessing.common.OrderEvent;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.rmi.server.UID;
/**
* A feeder bean that starts a scheduled task that writes a new OrderEvent object to the space.
*
* <p>The space is injected into this bean using OpenSpaces support for @GigaSpaceContext
* annotation.
*
* <p>The scheduled support uses the java.util.concurrent Scheduled Executor Service. It
* is started and stopped based on Spring life-cycle events.
*/
public class OrderEventFeeder implements InitializingBean, DisposableBean {
private Random randomGen = new Random();
private ScheduledExecutorService executorService;
private ScheduledFuture<?> sf;
/**
* Delay between scheduled tasks
*/
private long defaultDelay = 1000;
/**
* The scheduled orderEvent feeding task.
*/
private OrderEventFeederTask orderEventFeederTask;
@GigaSpaceContext(name = "gigaSpace")
private GigaSpace gigaSpace;
/**
* Unique ID for this client
*/
private UID clientID;
/**
* @param defaultDelay - Sets default delay between feeding tasks.
*/
public void setDefaultDelay(long defaultDelay) {
this.defaultDelay = defaultDelay;
}
/**
* The first method run upon bean Initialization when implementing InitializingBean.
* Starts a scheduled orderEvent feeding task.
*/
public void afterPropertiesSet() throws Exception {
clientID = new UID();
System.out.println("CLIENT ["+clientID.toString()+"] Starting feeder with cycle <" + defaultDelay + ">");
executorService = Executors.newScheduledThreadPool(1);
orderEventFeederTask = new OrderEventFeederTask();
sf = executorService.scheduleAtFixedRate(
orderEventFeederTask ,defaultDelay ,defaultDelay ,TimeUnit.MILLISECONDS );
}
public void destroy() throws Exception {
sf.cancel(true);
sf = null;
executorService.shutdown();
}
public class OrderEventFeederTask implements Runnable {
private int counter;
public void run() {
try {
OrderEvent orderEvent = new OrderEvent("USER" +randomGen.nextInt(), clientID.toString());
gigaSpace.write(orderEvent);
System.out.println("CLIENT wrote orderEvent: "+orderEvent);
}
catch (Exception e) {
e.printStackTrace();
}
}
public int getCounter() {
return counter;
}
}
public int getFeedCount() {
return orderEventFeederTask.getCounter();
}
public void setClientID(UID clientID) {
this.clientID = clientID;
}
public UID getClientID() {
return clientID;
}
}