Search in sources :

Example 1 with Input

use of org.qi4j.io.Input in project qi4j-sdk by Qi4j.

the class GaeEntityStoreMixin method entityStates.

@Override
public Input<Reader, IOException> entityStates() {
    return new Input<Reader, IOException>() {

        @Override
        public <ReceiverThrowableType extends Throwable> void transferTo(Output<? super Reader, ReceiverThrowableType> output) throws IOException, ReceiverThrowableType {
            Query query = new Query();
            PreparedQuery preparedQuery = datastore.prepare(query);
            final QueryResultIterable<Entity> iterable = preparedQuery.asQueryResultIterable();
            output.receiveFrom(new Sender<Reader, IOException>() {

                @Override
                public <ReceiverThrowableType extends Throwable> void sendTo(Receiver<? super Reader, ReceiverThrowableType> receiver) throws ReceiverThrowableType, IOException {
                    for (Entity entity : iterable) {
                        Text serializedState = (Text) entity.getProperty("value");
                        receiver.receive(new StringReader(serializedState.getValue()));
                    }
                }
            });
        }
    };
}
Also used : Entity(com.google.appengine.api.datastore.Entity) Query(com.google.appengine.api.datastore.Query) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) PreparedQuery(com.google.appengine.api.datastore.PreparedQuery) Reader(java.io.Reader) StringReader(java.io.StringReader) Text(com.google.appengine.api.datastore.Text) IOException(java.io.IOException) Input(org.qi4j.io.Input) Output(org.qi4j.io.Output) StringReader(java.io.StringReader)

Example 2 with Input

use of org.qi4j.io.Input in project qi4j-sdk by Qi4j.

the class AbstractApplicationEventStoreMixin method storeEvents.

// This is how transactions are put into the store
@Override
public TransactionApplicationEvents storeEvents(Iterable<ApplicationEvent> events) throws IOException {
    // Create new TransactionApplicationEvents
    ValueBuilder<TransactionApplicationEvents> builder = vbf.newValueBuilder(TransactionApplicationEvents.class);
    Iterables.addAll(builder.prototype().events().get(), events);
    builder.prototype().timestamp().set(getCurrentTimestamp());
    final TransactionApplicationEvents transactionDomain = builder.newInstance();
    // Lock store so noone else can interrupt
    lock();
    try {
        storeEvents(transactionDomain);
    } finally {
        lock.unlock();
    }
    // Notify listeners
    transactionNotifier.submit(new Runnable() {

        @Override
        public void run() {
            synchronized (listeners) {
                Input<TransactionApplicationEvents, RuntimeException> input = Inputs.iterable(Collections.singleton(transactionDomain));
                for (Output<TransactionApplicationEvents, ? extends Throwable> listener : listeners) {
                    try {
                        input.transferTo(listener);
                    } catch (Throwable e) {
                        logger.warn("Could not notify event listener", e);
                    }
                }
            }
        }
    });
    return transactionDomain;
}
Also used : Input(org.qi4j.io.Input) TransactionApplicationEvents(org.qi4j.library.eventsourcing.application.api.TransactionApplicationEvents) Output(org.qi4j.io.Output)

Aggregations

Input (org.qi4j.io.Input)2 Output (org.qi4j.io.Output)2 Entity (com.google.appengine.api.datastore.Entity)1 PreparedQuery (com.google.appengine.api.datastore.PreparedQuery)1 Query (com.google.appengine.api.datastore.Query)1 Text (com.google.appengine.api.datastore.Text)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 TransactionApplicationEvents (org.qi4j.library.eventsourcing.application.api.TransactionApplicationEvents)1