/* Example with Persistent Caching */
system.addActor(() -> new Actor("manager") {
protected PersistentActorCacheManager<String, ExampleEntity> manager;
@Override
public void preStart() {
UUID dataAccess = addChild(() ->
new MongoDataAccessActor<String, ExampleEntity>(
"dc", mongoClient, "db-actor4j-test", ExampleEntity.class));
manager = new PersistentActorCacheManager<String, ExampleEntity>(
this, "cache", "key", "cache");
addChild(manager.create(INSTANCES, 500, dataAccess));
manager.set("key1", new ExampleObject("key1", "value1"));
manager.set("key2", new ExampleObject("key2", "value2"));
manager.set("key3", new ExampleObject("key3", "value3"));
}
@Override
public void receive(ActorMessage<?> message) {
// empty
}
});
…
system.addActor(() -> new Actor("client") {
protected PersistentActorCacheManager<String, ExampleEntity> manager;
@Override
public void preStart() {
manager = new PersistentActorCacheManager<String, ExampleEntity>(
this, "cache", "key", "cache");
manager.get("key2");
}
@Override
public void receive(ActorMessage<?> message) {
Pair<String, ExampleEntity> pair = manager.get(message);
if (pair!=null)
System.out.printf("value for '%s': %s%n", pair.a(), pair.b().value());
}
});
/* Full examples under https://github.com/relvaner/actor4j/tree/master/actor4j-examples */