Search in sources :

Example 11 with ActorMessage

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));
        }
    }
}
Also used : JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) Document(org.bson.Document) WriteModel(com.mongodb.client.model.WriteModel) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) ActorMessage(actor4j.core.messages.ActorMessage)

Example 12 with ActorMessage

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());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(java.util.function.Consumer) Actor(actor4j.core.actors.Actor) ActorMessage(actor4j.core.messages.ActorMessage) UUID(java.util.UUID) ActorFactory(actor4j.core.utils.ActorFactory) Test(org.junit.Test)

Example 13 with ActorMessage

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());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(java.util.function.Consumer) Actor(actor4j.core.actors.Actor) ActorMessage(actor4j.core.messages.ActorMessage) UUID(java.util.UUID) ActorFactory(actor4j.core.utils.ActorFactory) Test(org.junit.Test)

Example 14 with ActorMessage

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());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(java.util.function.Consumer) Actor(actor4j.core.actors.Actor) ActorMessage(actor4j.core.messages.ActorMessage) UUID(java.util.UUID) ActorFactory(actor4j.core.utils.ActorFactory) Test(org.junit.Test)

Example 15 with ActorMessage

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());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(java.util.function.Consumer) Actor(actor4j.core.actors.Actor) ActorMessage(actor4j.core.messages.ActorMessage) UUID(java.util.UUID) ActorFactory(actor4j.core.utils.ActorFactory) Test(org.junit.Test)

Aggregations

ActorMessage (actor4j.core.messages.ActorMessage)17 UUID (java.util.UUID)14 CountDownLatch (java.util.concurrent.CountDownLatch)10 Test (org.junit.Test)10 Actor (actor4j.core.actors.Actor)8 Consumer (java.util.function.Consumer)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 ActorFactory (actor4j.core.utils.ActorFactory)6 ActorSystem (actor4j.core.ActorSystem)5 ArrayList (java.util.ArrayList)3 Assert (org.junit.Assert)3 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ActorTimer (actor4j.core.ActorTimer)1 EmbeddedActor (actor4j.core.actors.EmbeddedActor)1 PersistentActor (actor4j.core.actors.PersistentActor)1 PrimaryActor (actor4j.core.actors.PrimaryActor)1 PseudoActor (actor4j.core.actors.PseudoActor)1 SecondaryActor (actor4j.core.actors.SecondaryActor)1