use of actor4j.core.messages.ActorMessage in project actor4j-core by relvaner.
the class PersistenceServiceActor method receive.
@Override
public void receive(ActorMessage<?> message) {
if (message.tag == PERSIST_EVENTS) {
try {
JSONArray array = new JSONArray(message.valueAsString());
if (array.length() == 1) {
Document document = Document.parse(array.get(0).toString());
events.insertOne(document);
} else {
List<WriteModel<Document>> requests = new ArrayList<WriteModel<Document>>();
for (Object obj : array) {
Document document = Document.parse(obj.toString());
requests.add(new InsertOneModel<Document>(document));
}
events.bulkWrite(requests);
}
parent.send(new ActorMessage<Object>(null, INTERNAL_PERSISTENCE_SUCCESS, self(), message.source));
} catch (Exception e) {
e.printStackTrace();
parent.send(new ActorMessage<Exception>(e, INTERNAL_PERSISTENCE_FAILURE, self(), message.source));
}
} else if (message.tag == PERSIST_STATE) {
try {
Document document = Document.parse(message.valueAsString());
states.insertOne(document);
parent.send(new ActorMessage<Object>(null, INTERNAL_PERSISTENCE_SUCCESS, self(), message.source));
} catch (Exception e) {
e.printStackTrace();
parent.send(new ActorMessage<Exception>(e, INTERNAL_PERSISTENCE_FAILURE, self(), message.source));
}
} else if (message.tag == RECOVER) {
try {
JSONObject obj = new JSONObject();
Document document = null;
FindIterable<Document> statesIterable = states.find(new Document("persistenceId", message.valueAsString())).sort(new Document("timeStamp", -1)).limit(1);
document = statesIterable.first();
if (document != null) {
JSONObject stateValue = new JSONObject(document.toJson());
stateValue.remove("_id");
long timeStamp = stateValue.getJSONObject("timeStamp").getLong("$numberLong");
stateValue.put("timeStamp", timeStamp);
obj.put("state", stateValue);
FindIterable<Document> eventsIterable = events.find(new Document("persistenceId", message.valueAsString()).append("timeStamp", new Document("$gte", timeStamp))).sort(new Document("timeStamp", -1));
JSONArray array = new JSONArray();
MongoCursor<Document> cursor = eventsIterable.iterator();
while (cursor.hasNext()) {
document = cursor.next();
JSONObject eventValue = new JSONObject(document.toJson());
eventValue.remove("_id");
timeStamp = eventValue.getJSONObject("timeStamp").getLong("$numberLong");
eventValue.put("timeStamp", timeStamp);
array.put(eventValue);
}
cursor.close();
obj.put("events", array);
} else
obj.put("state", new JSONObject());
parent.send(new ActorMessage<String>(obj.toString(), INTERNAL_PERSISTENCE_RECOVER, self(), message.source));
} catch (Exception e) {
e.printStackTrace();
JSONObject obj = new JSONObject();
obj.put("error", e.getMessage());
parent.send(new ActorMessage<String>(obj.toString(), INTERNAL_PERSISTENCE_RECOVER, self(), message.source));
}
}
}
use of actor4j.core.messages.ActorMessage in project actor4j-core by relvaner.
the class BehaviourFeature method test_stack_become_unbecomeAll.
@Test(timeout = 2000)
public void test_stack_become_unbecomeAll() {
CountDownLatch testDone = new CountDownLatch(1);
final AtomicBoolean[] behaviour = new AtomicBoolean[3];
for (int i = 0; i < behaviour.length; i++) behaviour[i] = new AtomicBoolean(false);
UUID dest = system.addActor(new ActorFactory() {
@Override
public Actor create() {
return new Actor() {
protected Consumer<ActorMessage<?>> newBehaviour1 = new Consumer<ActorMessage<?>>() {
@Override
public void accept(ActorMessage<?> t) {
behaviour[0].set(true);
become(newBehaviour2, false);
}
};
protected Consumer<ActorMessage<?>> newBehaviour2 = new Consumer<ActorMessage<?>>() {
@Override
public void accept(ActorMessage<?> t) {
behaviour[1].set(true);
unbecomeAll();
}
};
protected boolean first = true;
@Override
public void receive(ActorMessage<?> message) {
if (first) {
become(newBehaviour1);
first = false;
} else {
behaviour[2].set(true);
testDone.countDown();
}
}
};
}
});
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.start();
try {
testDone.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
system.shutdown(true);
assertEquals(true, behaviour[0].get());
assertEquals(true, behaviour[1].get());
assertEquals(true, behaviour[2].get());
}
use of actor4j.core.messages.ActorMessage in project actor4j-core by relvaner.
the class BehaviourFeature method test_become_unbecome.
@Test(timeout = 2000)
public void test_become_unbecome() {
CountDownLatch testDone = new CountDownLatch(1);
final AtomicBoolean[] behaviour = new AtomicBoolean[2];
for (int i = 0; i < behaviour.length; i++) behaviour[i] = new AtomicBoolean(false);
UUID dest = system.addActor(new ActorFactory() {
@Override
public Actor create() {
return new Actor() {
protected Consumer<ActorMessage<?>> newBehaviour = new Consumer<ActorMessage<?>>() {
@Override
public void accept(ActorMessage<?> t) {
behaviour[0].set(true);
unbecome();
}
};
protected boolean first = true;
@Override
public void receive(ActorMessage<?> message) {
if (first) {
become(newBehaviour);
first = false;
} else {
behaviour[1].set(true);
testDone.countDown();
}
}
};
}
});
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.start();
try {
testDone.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
system.shutdown(true);
assertEquals(true, behaviour[0].get());
assertEquals(true, behaviour[1].get());
}
use of actor4j.core.messages.ActorMessage in project actor4j-core by relvaner.
the class BehaviourFeature method test_become.
@Test(timeout = 2000)
public void test_become() {
CountDownLatch testDone = new CountDownLatch(1);
final AtomicBoolean behaviour = new AtomicBoolean(false);
UUID dest = system.addActor(new ActorFactory() {
@Override
public Actor create() {
return new Actor() {
protected Consumer<ActorMessage<?>> newBehaviour = new Consumer<ActorMessage<?>>() {
@Override
public void accept(ActorMessage<?> t) {
behaviour.set(true);
testDone.countDown();
}
};
@Override
public void receive(ActorMessage<?> message) {
become(newBehaviour);
}
};
}
});
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.start();
try {
testDone.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
system.shutdown(true);
assertEquals(true, behaviour.get());
}
use of actor4j.core.messages.ActorMessage in project actor4j-core by relvaner.
the class BehaviourFeature method test_stack_become_unbecome.
@Test(timeout = 2000)
public void test_stack_become_unbecome() {
CountDownLatch testDone = new CountDownLatch(1);
final AtomicBoolean[] behaviour = new AtomicBoolean[2];
for (int i = 0; i < behaviour.length; i++) behaviour[i] = new AtomicBoolean(false);
UUID dest = system.addActor(new ActorFactory() {
@Override
public Actor create() {
return new Actor() {
protected Consumer<ActorMessage<?>> newBehaviour1 = new Consumer<ActorMessage<?>>() {
protected boolean first = true;
@Override
public void accept(ActorMessage<?> t) {
if (first) {
become(newBehaviour2, false);
first = false;
} else {
behaviour[1].set(true);
testDone.countDown();
}
}
};
protected Consumer<ActorMessage<?>> newBehaviour2 = new Consumer<ActorMessage<?>>() {
@Override
public void accept(ActorMessage<?> t) {
behaviour[0].set(true);
unbecome();
}
};
@Override
public void receive(ActorMessage<?> message) {
become(newBehaviour1);
}
};
}
});
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
system.start();
try {
testDone.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
system.shutdown(true);
assertEquals(true, behaviour[0].get());
assertEquals(true, behaviour[1].get());
}
Aggregations