Using Makai Persistence aka Getting Started with Makai/JAMP part II

From Resin 4.0 Wiki

Revision as of 00:37, 19 June 2013 by Rick (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


Files

├── makaiExample.iml  (InteliJ project file)
├── pom.xml                (Maven project file)
├── taskListModel        (Model project, clean Java no Makai or Java EE depedencies)
│   ├── model.iml
│   ├── pom.xml
│   ├── src
│   │   ├── main
│   │   │   ├── java
│   │   │   │   └── example                        Simple Object Model. Just Pojos
│   │   │   │       ├── Group.java
│   │   │   │       ├── OwnerRepo.java
│   │   │   │       ├── Person.java
│   │   │   │       ├── Task.java
│   │   │   │       ├── TaskManager.java
│   │   │   │       ├── TaskOwner.java
│   │   │   │       └── Utils.java
│   │   │   └── resources
│   │   └── test
│   │       └── java
│   │           └── io
│   │               └── jamp
│   │                   └── example
│   │                       └── model
│   │                           └── TaskManagerTest.java      //Simple Test Class
│   └── taskListModel.iml
└── taskListService
    ├── pom.xml
    ├── src
    │   └── main
    │       ├── java
    │       │   ├── example
    │       │   │   ├── TaskData.java          DTO object for sending TaskData without all of the parameters
    │       │   │   └── TaskService.java      The actual Makai Service
    │       │   └── io
    │       │       └── makai
    │       │           └── example
    │       ├── resources
    │       │   └── META-INF
    │       │       └── beans.xml                 IMPORTANT DON'T FORGET THIS! CDI beans.xml file
    │       └── webapp
    │           ├── META-INF
    │           │   └── MANIFEST.MF
    │           ├── WEB-INF
    │           │   └── resin-web.xml
    │           └── todo.html
    └── taskListService.iml


Task Service

package example;


import io.makai.core.*;
import io.makai.store.MakaiStore;

import javax.ejb.Startup;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.*;


@Service("/taskService")
@Export
@Journal
@Singleton
@Startup
public class TaskService {

    @Inject
    MakaiStore store;


    TaskManager manager = new TaskManager();
    OwnerRepo repo = new OwnerRepo();
    Group group = new Group(repo, "shared");
    boolean replay;
    boolean starting;

    public String addTask(String taskName) {
          if (replay) {
              System.out.println("IN REPLAY MODE during addTask");
          }
          Task task = new Task(taskName);
          manager.addTask(group, task);
          return task.id();
    }

    public boolean changeDescription(String id, String description) {
        if (replay) {
            System.out.println("IN REPLAY MODE during changeDescription");
        }

        Task task = manager.getTaskBy(id);
        task.setDescription(description);
        return true;
    }


    public Set<TaskData> tasks() {
        if (replay) {
            System.out.println("IN REPLAY MODE during tasks");
        }

        Set<TaskData> taskData = new HashSet<>();

        for (Task task : manager.tasks()) {
            taskData.add(new TaskData(task)) ;
        }
        return taskData;
    }


    @OnCheckpoint
    private boolean checkpoint() {

        //This is a blocking checkpoint example
        try {
            store.store("tasks", manager.tasks());
            store.store("ownerToTask", manager.owners());
        } finally {
            return true;
        }
    }

    @OnStart
    private boolean start() {
        starting = true;
        MakaiFuture<Boolean> future = new MakaiFuture<>();
        class Loader implements MakaiStore.OnLoad {
            Set<Task> tasks;
            Map<TaskOwner, Set<Task>> owners;

            @Override
            public void onLoad(Object key, Object value) {
                if (key.equals("tasks")) {
                    tasks = (Set<Task>) value;
                }else if (key.equals("ownerToTask")) {
                     owners = (Map<TaskOwner, Set<Task>>) value;
                }
            }
        };
        Loader loader = new Loader();
        store.loadAll(loader, future, future);
        manager.init(loader.owners, loader.tasks);


        replay = true;
        starting = false;
        return future.get();
    }

    @AfterReplay
    private void doneWithReplay() {
        this.replay = false;

    }



}

TaskManager (POJO)

package example;

import java.util.*;

public class TaskManager {

    private Set<Task> tasks = new HashSet<Task>();
    private transient Map<TaskOwner, Set<Task>> ownerToTaskMap = new HashMap<>();
    private transient Map<String, Task> idToTask = new HashMap<>();

    public void addTask(TaskOwner owner, Task task) {
        task.setOwner(owner);
        tasks.add(task);

        Set<Task> ownerTasks = ownerToTaskMap.get(owner);
        if (ownerTasks == null) {
            ownerTasks = new HashSet<>();
        }
        ownerTasks.add(task);
        idToTask.put(task.id(), task);

    }

    public void removeTask(Task task) {
        tasks.remove(task);
        idToTask.remove(task.id());

        Set<Task> ownerTasks = ownerToTaskMap.get(task.owner());
        if (ownerTasks == null) {
            ownerTasks = new HashSet<>();
        }
        ownerTasks.remove(task);

    }


    public Set<Task> listTasksForOwner(TaskOwner doer) {
        return ownerToTaskMap.get(doer);
    }

    public Task getTaskBy(String id) {
        return idToTask.get(id);
    }


    //These two are needed for persistence
    public Set<Task> tasks() {
        return tasks;
    }

    public Map<TaskOwner, Set<Task>> owners() {
        return ownerToTaskMap;
    }

    //This gets called as starup once owners and tasks are read from async store
    public void init(Map<TaskOwner, Set<Task>> owners, Set<Task> tasks) {
        if (owners!=null) {
            this.ownerToTaskMap = owners;
        }

        if (tasks!=null) {
            this.tasks = tasks;
        }

        /* We can rebuild taskIdMap from tasks so we don't store it */
        for (Task task : this.tasks) {
            this.idToTask.put(task.id(), task);
        }

    }
}

Personal tools
TOOLBOX
LANGUAGES