package com.gigaspaces.examples.tutorials.topologies;
import org.openspaces.core.GigaSpace;
import org.openspaces.core.GigaSpaceConfigurer;
import org.openspaces.core.space.UrlSpaceConfigurer;
import com.j_spaces.core.IJSpace;
public class DataLoader {
private Integer maxAccounts;
private Integer sampleSize;
GigaSpace gigaSpace;
public void setMaxAccounts(Integer maxAccounts){
this.maxAccounts = maxAccounts;
}
public Integer getMaxAccounts(){
return maxAccounts;
}
public void setSampleSize(Integer sampleSize){
this.sampleSize = sampleSize;
}
public Integer getSampleSize(){
return sampleSize;
}
/**
* Configure writer and connect to a space
* @param url
* @throws Exception
*/
public DataLoader(String url) throws Exception {
this.maxAccounts = 100;
this.sampleSize = 20;
IJSpace space = new UrlSpaceConfigurer(url).space();
gigaSpace = new GigaSpaceConfigurer(space).gigaSpace(); }
/**
* Clears space of account POJOs
*/
public void clearSpaceOfAccountPOJOs() throws Throwable{
Account account = new Account();
int count = gigaSpace.count(account);
if (count != 0){
System.out.println("Clearing space of account objects...");
gigaSpace.clear(account); }
}
/**
* Writes accounts to the space
* @throws Throwable
*/
public void write() throws Throwable {
long waitDuration = 3000;
for (int i = 1; i < getMaxAccounts() +1; i++) {
Account account = new Account(i,i*10);
gigaSpace.write(account);
if (i % getSampleSize() == 0 && i != 0){
System.out.println("Writing "+sampleSize+" account objects; total of "+i+" objects; ");
if (i != getMaxAccounts()) sleep(waitDuration);
}
}
}
/**
* Sleep <timeToSleepInMillis> milliseconds
* @param timeToSleepInMillis
* @throws Throwable
*/
public void sleep(long timeToSleepInMillis) throws Throwable{
System.out.print("Sleeping");
Thread.sleep(timeToSleepInMillis/4);
System.out.print(".");
Thread.sleep(timeToSleepInMillis/4);
System.out.print(".");
Thread.sleep(timeToSleepInMillis/4);
System.out.println(".");
Thread.sleep(timeToSleepInMillis/4);
}
/**
*
* @param args
*/
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage: <URL>");
System.out.println("<protocol>:);
System.exit(1);
}
try {
System.out.println("\nWelcome to GigaSpaces Data Grid Topologies Example");
System.out.println("This example writes to various caching topologies...");
DataLoader dataLoader = new DataLoader(args[0]);
dataLoader.clearSpaceOfAccountPOJOs();
System.out.println("\nWriting " + dataLoader.getMaxAccounts() + " account objects to space:");
dataLoader.write();
System.exit(0);
} catch (Throwable e) {
e.printStackTrace();
}
}
}