Close
Tutorials    
Snapshot   
Deploying Hyperon

Connecting Application via Spring Annotations

Prerequisites:

  • Java 1.8 or higher
  • Maven 3.x
  • Spring Framework
  • HyperonStudio with H2 database (default for bundle distribution)

We will show how to configure Hyperon Engine using Spring annotated configuration (http://docs.spring.io).

Maven configuration

First add Hyperon Runtime dependency to your pom.xml file:

<dependency>
    <groupId>pl.decerto</groupId>
    <artifactId>hyperon-runtime</artifactId>
    <version>1.5.19</version>
</dependency>

Remember to substitute 1.5.19 above with current version available on Download page. Hyperon Runtime Library will be downloaded by Maven from Maven Central Repo so there is no need to do it manually.

Next add other required libraries for the purpose of this example:

<!-- Spring Framework -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.6.RELEASE</version>
</dependency>

<!-- Database access libraries -->
<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.3.176</version>
</dependency>

Above libraries set up Spring and allows us to connect to H2 database via pooled data source.

Spring configuration

Add required beans to your java class annotated with @Configuration:

@Autowired
private Environment env;

private static final Logger log = LoggerFactory.getLogger(TestConfiguration.class);

@Bean
public DialectRegistry getDialectRegistry() {
    DialectRegistry registry = new DialectRegistry();
    registry.setDialect(env.getProperty("hyperon.database.dialect"));
    return registry;
}

@Bean
public DialectTemplate getDialectTemplate() {
    return getDialectRegistry().create();
}

@Bean(destroyMethod = "close")
public DataSource getDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setUsername(env.getProperty("hyperon.database.username"));
    dataSource.setPassword(env.getProperty("hyperon.database.password"));
    dataSource.setUrl(env.getProperty("hyperon.database.url"));
    dataSource.setInitialSize(4);
    dataSource.setMaxActive(8);
    dataSource.setDriverClassName(getDialectTemplate().getJdbcDriverClassName());
    return dataSource;
}

@Bean(destroyMethod = "destroy")
public HyperonEngineFactory getHyperonEngineFactory() {
    log.info("Engine factory begin creation...");
    HyperonEngineFactory hyperonEngineFactory = new HyperonEngineFactory();
    hyperonEngineFactory.setDataSource(getDataSource());
    return hyperonEngineFactory;
}

@Bean
public HyperonEngine getHyperonEngine() {
    log.info("Engine begin creation...");
    return getHyperonEngineFactory().create();
}

Add @PropertySource("classpath:app.properties") to your configuration class so that external properties are resolved.

Resource files

Create a file called: app.properties with content as follows:

hyperon.database.username=sa
hyperon.database.password=sa
hyperon.database.url=jdbc:h2:c:/bundles/hyperon-studio-1.5.19/database/hyperon.1205.02;AUTO_SERVER=TRUE;MVCC=TRUE;IFEXISTS=TRUE
hyperon.database.dialect=h2

Be sure to properly point hyperon.database.url to your bundle database subdirectory and put the result file in your resource files directory in location specified at @PropertySource above.

If you enable logging, you can see such output after getHyperonEngineFactory().create():

DEBUG [HyperonEngineFactory] - creating Hyperon Runtime component...
INFO  [IntegerType] - cached integer holders: 10000
INFO  [ListRepository] - ParamRepository repository: registering MpParameterProvider
INFO  [MapRepository] - FunctionRepository repository: registering MpPropFunctionProvider ...
INFO  [MapRepository] - FunctionRepository repository: registering MpFunctionProvider ...
INFO  [MapRepository] - FunctionInvoker repository: registering RhinoFunctionInvoker ...
INFO  [MapRepository] - FunctionInvoker repository: registering PropFunctionInvoker ...
INFO  [MapRepository] - Type repository: registering DateType ...
INFO  [MapRepository] - Type repository: registering NumberType ...
INFO  [MapRepository] - Type repository: registering BooleanType ...
INFO  [MapRepository] - Type repository: registering StringType ...
INFO  [MapRepository] - Type repository: registering IntegerType ...
INFO  [MapRepository] - Matcher repository: registering BetweenMatcher ...
INFO  [MapRepository] - Matcher repository: registering ContainsAnyMatcher ...
INFO  [MapRepository] - Matcher repository: registering ContainsNoneMatcher ...
INFO  [MapRepository] - Matcher repository: registering BetweenMatcher ...
INFO  [MapRepository] - Matcher repository: registering InMatcher ...
INFO  [MapRepository] - Matcher repository: registering RegexMatcher ...
INFO  [MapRepository] - Matcher repository: registering LikeMatcher ...
INFO  [MapRepository] - Matcher repository: registering NotInMatcher ...
INFO  [MapRepository] - Matcher repository: registering ContainsAllMatcher ...
DEBUG [HyperonEngineFactory] - created core engine: org.smartparam.engine...
INFO  [HyperonEngineFactory] - runtime mode: production
DEBUG [HyperonEngineFactory] - using value-never-null mode: true
DEBUG [HyperonEngineFactory] - creating domain cache component...
DEBUG [HyperonEngineFactory] - rhino invoker initialized with such global objects:
global variables (10)
1. str=pl.decerto.hyperon.runtime.rhino.RhinoStr@33b40727
2. date=pl.decerto.hyperon.runtime.rhino.RhinoDate@233210d1
3. util=pl.decerto.hyperon.runtime.rhino.RhinoUtil@1504d1f1
4. log=pl.decerto.mpp.mp.service.rhino.logger.SimulationRhinoLogger@4c26edf1
5. hyperon=pl.decerto.hyperon.runtime.rhino.RhinoHyperon@5fb8466d
6. domain=pl.decerto.hyperon.runtime.rhino.RhinoDomain@6c3f71d3
7. math=pl.decerto.hyperon.runtime.rhino.RhinoMath@5f07befc
8. type=pl.decerto.hyperon.runtime.rhino.RhinoType@24b59988
9. mpp=pl.decerto.hyperon.runtime.rhino.RhinoHyperon@3923285d
10. life=pl.decerto.hyperon.mp.simulation.life.invest.api.RhinoSimulationLife@2e856743

DEBUG [HyperonEngineFactory] - installing function invocation interceptor
INFO  [HyperonEngineFactory] - successfully created Hyperon Runtime component.
More tutorials

Deploying Hyperon