private RedisConnectionProvider redisConnectionProvider;
@Override
public void appendEvents(String type, final DomainEventStream events) {
Jedis jedis = redisConnectionProvider.newConnection();
DomainEventMessage firstEvent = events.peek();
final byte[] key = (type + "." + firstEvent.getAggregateIdentifier()).getBytes(IOUtils.UTF8);
jedis.watch(key);
Long eventCount = jedis.llen(key);
if ((firstEvent.getSequenceNumber() != 0 && eventCount == null)
|| firstEvent.getSequenceNumber() != eventCount) {
jedis.unwatch();
throw new ConcurrencyException(
String.format("Concurrent modification detected for Aggregate identifier [%s], sequence: [%s]",
firstEvent.getAggregateIdentifier(),
firstEvent.getSequenceNumber()));
}
try {
// jedis.pipelined(new PipelineBlock() {
// @Override
// public void execute() {
Transaction multi = jedis.multi();
while (events.hasNext()) {
DomainEventMessage domainEvent = events.next();
multi.rpush(new String(key, IOUtils.UTF8), new String(eventSerializer.serialize(domainEvent, byte[].class)
.getData(), IOUtils.UTF8));
}
multi.exec();
// }