List saved with Baratine store:

../../_images/example-single-store.png

List Service with Baratine store:

Introduction

@OnLoad - called exactly once before any methods
...
@Modify
@Modify
@Modify
@OnSave - saved after batch completes
...
get()   - non-modify methods do not trigger a save
get()
...
@Modify
@OnSave - saved after batch completes

Installation

Prerequisites

Installing Baratine

Download Baratine from http://baratine.io/download and extract it to any directory.

Source Code for the Example

Maven repository

Add the following maven dependency:

<dependencies>
  <dependency>
    <groupId>io.baratine</groupId>
    <artifactId>baratine</artifactId>
    <version>0.10.0</version>
  </dependency>
</dependencies>

ListServiceImpl.java

Create the class ListServiceImpl.java:

package example;

import javax.inject.Inject;
import io.baratine.core.Lookup;
import io.baratine.core.Modify;
import io.baratine.core.Result;
import io.baratine.core.Service;
import io.baratine.store.Store;
import java.util.*;

@Service("public:///list")
public class ListServiceImpl extends ListService
{
  @Inject @Lookup("store://")
  Store<List<String>> _store;

  private ArrayList<String> _list;

  public void get(Result<List<String>> result)
  {
    result.complete(_list);
  }

  @Modify
  public void add(String value)
  {
    _list.add(value);
  }

  @Modify
  public void pop(Result<String> result)
  {
    if (_list.size() > 0) {
      result.complete(_list.remove(0));
    }
    else {
      result.complete(null);
    }
  }

  @OnLoad
  private void onLoad(Result<Boolean> result)
  {
    _list = _store.get("/list", result.from(v->afterLoad(v)));
  }

  private boolean afterLoad(List<String> list)
  {
    _list = list;

    return true;
  }

  @OnSave
  private void onSave()
  {
    _store.put("/list", _list);
  }
}

ListService.java

Create the class ListService.java:

package example;

import io.baratine.core.Result;

public interface ListService
{
  void get(Result<List<String>> result);

  void add(String value);

  void pop(Result<String> result);
}

ListServiceSync.java

Optionally, create the class ListServiceSync.java for Java clients:

package example;

public interface ListServiceSync extends ListService
{
  List<String> get();

  void add(String value);

  String pop();
}

Deploying your service

Compile the class and create a jar named list.jar:

$ mvn clean package

Deploy your new service:

$ bin/baratine deploy list.jar
  deployed list.jar to bfs:///config/pods/pod.cf

Remote Clients

Calling your service from HTTP

Test our service by calling the JSON REST endpoint:

$ curl 'http://localhost:8085/s/pod/list?m=add&p0=hello'
{"status":"ok", "value" : null}

$ curl 'http://localhost:8085/s/pod/list?m=get'
{"status":"ok", "value" : ["hello"]}

Calling your service from Java

The Java remote client code:

package example;

import io.baratine.core.Result;
import io.baratine.core.ServiceClient;

public class CounterClient
{
  public static void main(String[] args)
    throws Exception
  {
    String url = "http://127.0.0.1:8085/s/pod";

    try (ServiceClient client = ServiceClient.newClient(url).build()) {
      ListServiceSync listService;
      listService = client.lookup("remote:///list")
                          .as(ListServiceSync.class);

      listService.add("hello");
      listService.add("world");

      System.out.println("get: " + listService.get());
      System.out.println("pop: " + listService.pop());
      System.out.println("get: " + listService.get());
    }
  }
}