We have a background job(java batch) which listens/reads from an MQ and persists a message onto the database(local table). Then a trigger sends this message into a queuing table in WAITING status. My polling program (multithreaded in java) continuous polls for WAITING records, picks them up and sends them to the proper consumer ( java program ). Also messages of same type needs to be processed synchronously. (same order number but both arrive at the Nth min.one needs to complete before another starts) However in this case due to database polling there was a possibility where same record can be picked up by two threads which caused an issue so we put the polling as synchronized which is now resulting in deadlocks.
How can i remove this database polling and make use of an event based mechanism or queues which would be viable rather than database polling and also preserve synchronization.
Edit-- Further message processing is comprised of 4 tasks which are async in nature but all the tasks need to be completed before processing the 2nd message of the same type(orderid). Parent process needs to be synchronous and child are async . Will 2 queues be needed for the same?