/*
 * 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.feeder;

import org.openspaces.core.GigaSpace;
import org.openspaces.core.context.GigaSpaceContext;

import org.springframework.beans.factory.InitializingBean;
import com.gigaspaces.examples.tutorials.queries.common.Account;

/**
 * A loader bean that writes Account objects with unique user names to the
 * space. Since the write is executed in the init() method called directly in the afterPropertiesSet method
 * (and not in a new thread), the processing unit waits until the loading is
 * finished before initializing the next bean.
 */
public class AccountPreLoader implements InitializingBean {

	/**
	 * Number of accounts to be loaded by the loader, hard-coded to 100, can be overridden 
	 * in the pu.xml (by setting the prop key "numberOfAccounts")
	 */
	private int numberOfAccounts = 100;
	
	@GigaSpaceContext(name = "gigaSpace")
    private GigaSpace gigaSpace;
	
	public void setGigaSpace(GigaSpace gigaSpace) {
		this.gigaSpace = gigaSpace;
	}

	/**
	 * Allows to control the number of accounts that will be initially
	 * loaded to the Space. Defaults to <code>100</code>.
	 */
	public void setNumberOfAccounts(int numberOfAccounts) {
		this.numberOfAccounts = numberOfAccounts;
	}

	/**
	 * The first method to run upon bean Initialization when implementing InitializingBean.
	 * Runs the pre-loader init() method 
	 */
	public void afterPropertiesSet() throws Exception {
		init();	
	}
	
	/**
	 * init - Initializes the pre-loader:
	 * Writes <numberOfAccounts> unique accounts to the space.
	 */
	public void init() throws Exception {
		
		System.out.println("\nFeeder Pre-Loader Starts writing accounts");
		// Writing <numberOfAccounts> accountData objects to the space.
		for (int i = 1; i <= numberOfAccounts; i++) {
			Account account = new Account("FN"+i /* firstName */
								,"LN"+i /* lastName */
								,1000 /* amount */
								,50); /* riskAllowedFactor */
			gigaSpace.write(account);
		}

		System.out.println("Feeder Wrote "+numberOfAccounts+" Accounts\n");
	}
	
}
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence