0

Let's say I have multiple REST applications where I need to store fairly limited data which the applications will maintain (CRUD) and process data per user request. The data will be always small enough to keep it in the memory which is a big performance boost and also much easier data manipulation since it's directly java object. As a bonus it makes websocket-push implementation much easier.

But, this data must be persisted (be available after shutdown or crash) and also be available to other servers via network connection (scaling).

One way to accomplish this would be I guess using PostgreSQL's JSON capabilities with Notify feature. Or I guess a NoSQL database with some sort of notify functionality will do as well. Those require a lot of boilerplate code/SQL around each table. I prefer to have something simpler or a wrapper that does it for me.

An example in my head:

public class MyRestResource {

  // This map will be updated realtime if any other service updates it; and when it's updated it will update the database.
  private Map<String, MyPojo> myPojos;

  public MyRestResource(PojoSynchronizer pojoSynchronizer){
    // getTable will return a special Map implementation with events and triggers and being observed by the pojoSynchronizer.
    this.myPojos = pojoSynchronizer.getTable("myPojos", MyPojo.class);
  }
}

Of course this is just one approach with probably many challenges. It could be a different implementation. Is something like this available? Or can a wrapper written easily using the approaches above like PostgreSQL JSON and Notify?

Natan
  • 2,816
  • 20
  • 37
  • 1
    Try [Coherence](http://www.oracle.com/technetwork/middleware/coherence/overview/index.html). It was bought by Oracle. Confluence uses it to keep pages in synch across a multi-cluster wiki. – Chloe Jul 03 '15 at 22:56
  • 1
    When trying to share data between processes (services) may be [http://stackoverflow.com/questions/25396664/shared-memory-between-two-jvms] helps you. A specially Solution #2 with java.nio.FileChannel based memory mapping may be helpfull – Oncaphillis Jul 03 '15 at 23:00
  • @Oncaphilis, I have actually meant servers by services, which means it would require network connection available to the framework. I should have been more clear.. – Natan Jul 04 '15 at 09:19
  • 1
    @Chloe, Coherence does sound like exactly what I need here. Thanks. – Natan Jul 04 '15 at 15:07

3 Answers3

1

Perhaps you don't need a framework for this. We can use Serializable interface in your classes and do something like this for your object graph, which you need persist before shutdown.

try{

  FileOutputStream fout = new FileOutputStream("my-data.ser");
  ObjectOutputStream oos = new ObjectOutputStream(fout);   
  oos.writeObject(myRootObject);
  oos.close();
  System.out.println("Done");

} catch(Exception ex){
  ex.printStackTrace();
}

To build an object graph for your application you need one RootObject with one or more Map, ArrayList, Tree or something more specific.

To read data on start of program you can use ObjectInputStream readObject method http://docs.oracle.com/javase/8/docs/api/java/io/ObjectInputStream.html

See also : http://docs.oracle.com/javase/8/docs/api/java/io/ObjectOutputStream.html

CEPEL SOMA
  • 98
  • 6
  • Thought of that but it doesn't meet "also be available to other services (scalable)", which means if another service should be able to connect from through network and update or get notified by the changes. – Natan Jul 04 '15 at 08:19
0

Years ago, I have used prevayler for a non-database web-application. It was incredibly fast! It uses POJOs. Easy to understand and implement.

Data structure was very simple. Think about it as a tree with a default node. Prevayler knows the root of the tree and you add your data to this root object. You can take snapshots of the tree as a backup mechanism. You can even use XML snapshots.

Prevailer is a failsafe, as it writes changes to the disk and then updates the tree.So, even if there is a power cut, when you boot the system up, it reads logs and re-creates where it was left off.

Prevayler was one of the very first of this kind. I am sure there are other libraries. Make a bit of a research before you decide which one to go with.

I hope it helps. =]

Alp
  • 3,027
  • 1
  • 13
  • 28
  • Thanks! It looks like a pretty cool framework for small applications. But as I responded the previous answer, it doesn't meet "also be available to other services (scalable)", which means if another service should be able to connect from through network and update or get notified by the changes. I guess I should update my question and make that more clear. – Natan Jul 04 '15 at 08:41
0

Following @Chloe's comment on Oracle-Coherence, I concluded that the concept I was looking for is In-Memory Data Grid. There are variety of tools that is useful such as Hazelcast, EhCache, Infinispan and so on that could help.

An example from Hazelcast:

    Config cfg = new Config();
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);
    Map<Integer, String> mapCustomers = instance.getMap("customers");
Community
  • 1
  • 1
Natan
  • 2,816
  • 20
  • 37