DistSimHelloWorld: Difference between revisions
No edit summary |
No edit summary |
||
Line 131: | Line 131: | ||
} // class: hello |
} // class: hello |
||
---- |
---- |
||
This simulation expects the database configuration as standard input |
This simulation expects the database configuration as standard input which is the convention when working with the DistSim framework. The input is loaded with |
||
simulationProperties.load(System.in); |
simulationProperties.load(System.in); |
||
It then creates a DbSaver object which does the actual work: |
It then creates a DbSaver object which does the actual work: |
Latest revision as of 11:30, 19 February 2007
This class is a minimal simulation utilizing the OR-Mapping library. See below for a short description.
////////////////////////////////////////////////// // JIST (Java In Simulation Time) Project // // Copyright (C) 2004 by Cornell University // All rights reserved. // Refer to LICENSE for terms and conditions of use. package jist.minisim; import java.io.IOException; import java.sql.SQLException; import java.util.Properties; import java.util.Random; import jist.runtime.JistAPI; import or_mapper.util.DBSaver; class Result { Result(int counter, String greeting) { this.counter = counter; this.greeting = greeting; } int counter; String greeting; } /** * Hello World with distsim. * * @author Rimon Barr <barr+jist@cs.cornell.edu> and Ulf Hermann * @version $Id: hello.java,v 1.7 2004/06/09 18:54:16 barr Exp $ * @since JIST1.0 */ public class DistsimTest implements JistAPI.Entity { private Random random; private String greeting; private DBSaver saver; private int counter; /** * Hello event. */ public void myEvent() { counter = counter + 1; try { if (counter < 2) { saver.save(new Result(counter, greeting), "distsim-test"); } else saver.quickSave(new Result(counter, greeting), "quicksim"); int pos = random.nextInt(greeting.length()); greeting = greeting.replace(greeting.charAt(pos), (char) random .nextInt(128)); System.out.println("distsim mutters: " + counter); } catch (Exception e) { e.printStackTrace(); } if (counter < 10) JistAPI.sleep(1); else { try { saver.finalize(); } catch (Throwable e1) { e1.printStackTrace(); } return; } myEvent(); // delay execution /*try { Thread.sleep(500); } catch (InterruptedException e) { }*/ } /** * Program entry point: show difference between Java and JiST execution * models with a "hello world!" example. * * @param args * command-line parameters */ public static void main(String[] args) { System.out.println("starting simulation."); DistsimTest t = new DistsimTest(); t.myEvent(); } public DistsimTest() { try { random = new Random(); greeting = "Hello world!"; Properties simulationProperties = new Properties(); simulationProperties.load(System.in); saver = new DBSaver("jdbc:mysql://" + simulationProperties.getProperty("results.host") + "/" + simulationProperties.getProperty("results.database"), simulationProperties.getProperty("results.username"), simulationProperties.getProperty("results.password"), Integer.parseInt(simulationProperties .getProperty("simulationId"))); } catch (IOException e) { e.printStackTrace(); } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } // class: hello
This simulation expects the database configuration as standard input which is the convention when working with the DistSim framework. The input is loaded with
simulationProperties.load(System.in);
It then creates a DbSaver object which does the actual work:
saver = new DBSaver("jdbc:mysql://" + simulationProperties.getProperty("results.host") + "/" + simulationProperties.getProperty("results.database"), simulationProperties.getProperty("results.username"), simulationProperties.getProperty("results.password"), Integer.parseInt(simulationProperties .getProperty("simulationId")));
Later on objects of type Result are saved using DBSaver.save
saver.save(new Result(counter, greeting), "distsim-test");
In the case of Result no recursive saving takes place, as Result has only trivial members. If it had other classes as members, these would be saved recursively. After saving the first result the class Result is known to the DbSaver, so we can perform the same operation with quickSave, which is much faster but will throw an exception if an unknown class is found. Optionally classes could also be introduced with rebuildSession.
Unfortunately polymorphic references to Java arrays and to java.lang.Class can't be saved with this method, so you need to be careful about the objects you pass to save. For example you can't save any collections of Class, neither can you save multi-dimensional arrays, as these are internally represented as Arrays of Objects which "happen" to be Arrays.