This is main class in this example. It basically starts grid locally (local node) and executes credit risk calculation. If there are grid nodes available this calculation will be gridified to other nodes.
Full Source Code
CreditRiskExample.java
package org.gridgain.examples.montecarlo.gigaspaces; import java.util.*; import org.gridgain.grid.*; import org.gridgain.grid.gridify.*; import org.gridgain.grid.gridify.aop.spring.*; import com.j_spaces.map.*; public final class CreditRiskExample { /** Task type ID used for this example. */ public static final String TASK_TYPE_ID = "GridifyCreditRiskTask"; /** * JavaSpace space name. * Example put portfolio into the space to exchange it with job. */ private static String spaceMapName = "jini://localhost/*/mySpaceMap?groups=gigaspaces-6.0EE"; /** * Ensure singleton. */ private CreditRiskExample() { // No-op. } /** * Method creates portfolio and calculates credit risks. * * @param args Command line arguments, none required. * @throws Exception If example execution failed. */ public static void main(String[] args) throws Exception { // Start GridGain instance with default configuration. GridFactory.start(); try { // Create portfolio. Credit[] portfolio = new Credit[5000]; Random rnd = new Random(); // Generate some test portfolio items. for (int i = 0; i < portfolio.length; i++) { portfolio[i] = new Credit( 50000 * rnd.nextDouble(), // Credit amount. rnd.nextInt(1000), // Credit term in days. rnd.nextDouble() / 10, // APR. rnd.nextDouble() / 20 + 0.02 // EDF. ); } // Forecast horizon in days. final int horizon = 365; // Percentile. double percentile = 0.95; // Number of Monte-Carlo iterations. int iter = 10000; // Instance of credit risk manager. CreditRiskManager riskMgr = new CreditRiskManager(); // Mark the stopwatch. long start = System.currentTimeMillis(); // Put portfolio into the map. IMap mapSpace = (IMap)CacheFinder.find(spaceMapName); String mapKey = "portfolio"; mapSpace.put(mapKey, portfolio); // Calculate credit risk and print it out. // As you can see the grid enabling is completely hidden from the caller // and it is fully transparent to him. In fact, the caller is never directly // aware if method was executed just locally on the 100s of grid nodes. // Credit risk crdRisk is the minimal amount that creditor has to have // available to cover possible defaults. double crdRisk = riskMgr.calculateCreditRiskMonteCarlo(spaceMapName, mapKey, horizon, iter, percentile); System.out.println("Credit risk [crdRisk=" + crdRisk + ", duration=" + (System.currentTimeMillis() - start) + "ms.]"); } // We specifically don't do any error handling here to // simplify the example. Real application may want to // add error handling and application specific recovery. finally { // Stop GridGain instance. GridFactory.stop(true); } } }
