- Java 17+
- Maven 3
Service service = new ServiceWithLock();
Service service = new ServiceWithoutLock();
Optional<Object> data = service.getData("A");
Integer batchID = service.startBatch();
PriceData pricingData = new PriceData("A",Instant.now(),100);
service.addToBatch(batchID,List.of(pricingData));
service.completeBatch(batchID);
service.cancelBatch(batchID);
- There are two implementations.
ServiceWithLock
andServiceWithoutLock
. I am running the same test suite on them to verify that they have the same behaviour. ServiceWithLock
is using two different locks to make sure that batch and complete are synchronized. First lock is commit lock that ensures only one batch is completed at any time. Second lock is for batch that ensures only one producer can add data to a specific batch.ServiceWithoutLock
is usingAtomicReference
andConcurrentLinkedQueue
. IncompleteBatch
, it is using stamp to update the main data. IfcompleteBatch
method called for two different batch,AtomicReference
will ensure main data will be updated without overriding since stamp will be updated.ConcurrentLinkedQueue
is used to allow different producers to add data to the same batch without locking.