Quickstart Guide - Part 1

This document is a guide to getting a simple JEMM based project up and running using Maven. Unique id generation among a group of processes can be painful and error prone, typically relying on an underlying database to achieve. This example demonstrates a persistent thread-safe counter. In part two we will build on this example to show how it can be used by multiple processes simultaneously.

Step 1 - Create a project

Use maven to create a project.

mvn archetype:create -DgroupId=com.test -DartifactId=jemm-app

Step 2 - Import the JEMM library

Edit jemm-app/pom.xml add the below dependency

<dependency>
  <groupId>org.sourceforge.jemm</groupId>
  <artifactId>jemm</artifactId>
  <version>0.1</version>
</dependency>

Because the 0.1 release is not yet in the main Maven repository you will also have to add the JEMM repository to your pom:

<repositories>
  <repository>
    <id>jemm-repo</id>
    <url>http://jemm.sourceforge.net/m2repo</url>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
  </repository>
</repositories>

Step 3 - Create your classes

Update src/main/java/com/test/App.java as follows:

package com.test;

import java.io.File;

import org.sourceforge.jemm.PersistentStore;
import org.sourceforge.jemm.Session;
import org.sourceforge.jemm.Store;

public class App {
    public static void main( String[] args ) {
        Store store = new PersistentStore(new File(args[0])); 
        Session.setStore(store);

        Counter counter = (Counter) Session.getRoot("counter");
                
        if(counter == null) {
            counter = new Counter();
            counter = (Counter)Session.setRootIfNull("counter", counter);
        }
                
        for(int i=0;i<100;i++)
            counter.incrementCount();

        System.out.println("Counter=" + counter.getCount());            
        Session.shutdown();
    }
}

Create src/main/java/com/test/Counter.java as follows:

package com.test;

import org.sourceforge.jemm.Entity;

@Entity
public class Counter {
    private int count = 0;
        
    public synchronized void incrementCount() {
            count++;
    }
        
    public synchronized int getCount() {
        return count;
    }
}

Step 4 - Compile

mvn install

Step 5 - Run

First export the classpath from maven:

mvn dependency:build-classpath

Export the classpath so java can see it, and take note of where the JEMM library is. This library is needed to be passed (as well as in the classpath) as a javaagent parameter to the java runtime.

Before you run the application first create a directory for the persistent data to be stored:

> mkdir rundata

Then run the application with the below command:

> java -javaagent:<path to jemm jar> com.test.App rundata
    
e.g. 
> java -javaagent:~/.m2/repo/org/sourceforge/jemm/jemm/0.1-SNAPSHOT/jemm-0.1-SNAPSHOT.jar 
      App rundata

Step 6 - Done!

So this example is running a persistent thread-safe counter. Re-running it increments the counter further.

Currently the persistence is being managed by the user process, so although thread-safe it is not currently process safe. Part 2 expands on this example and shows you how to run a JEMM Server instance and share the counter between multiple processes.

Next