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()));
}
}
});
}
};
}
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;
}
Aggregations