Search code examples
spring-boottransactionsqueuewebhooksblockingqueue

How to process incoming webhooks in a Spring Boot Application


I'm integrating with a vendor that sends statuses of the integration via webhooks. In some cases, they send multiple statuses of the same type and unique id.

I have a database queue of each webhook status. Before I persist each webhook status, I check if the same type and unique id of this webhook has already been recorded or not. If not, then I record it otherwise I simply ignore it.

The issue is, when two of the same webhooks status come back to back, my coded processes both simultaneously in different threads and persist both statuses in the database because the if (exists) check returns false since neither has yet persisted their corresponding status into the db.

I have wrapped my handleWebook method with @Transactional(propagation = Propagation.REQUIRES_NEW) but obviously this won't help since both webhooks are processed in different threads in their own transactions.

I'm now thinking about adding all webhooks to a BlockingQueue which my handleWebhook then takes from and processes each webhook in order. But I don't know how to cleanly do this in Spring Boot. I also am not sure if this truly is the solution to my issue. Maybe my understanding of @Transactional is not robust enough and there's a Spring setting for it that I can add to my annotation that can resolve the issue I'm facing.

Any advise and suggestions greatly appreciated.


Solution

  • This is how you can achieve this thing in spring.

    @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
    public boolean isIdExists(){
    Your business logic will be here 
    }