简体   繁体   中英

Data share between different threads

I have one method under lock (ReentrantLock) this method sending some message to server, receiving response right away and create/save data in database. This response have id of message that was send. Client have a listener which is waiting for report from server (this report can return after data send to server), server can also return some kind of report (with id and some status) and I need to map that report (by id) to the previous saved record and change status field for that record.

The problem is that this report can be returned faster (and listener will grab it and try to process) then message from first method is saved in database. What's the ways to sole that problem?

I had and idea to create additional table in database and save data from message in one table and save data from report to other table. Then synchronize them via some other process, but maybe it is possible to create some buffer/queue where message id will be stored from first method and when we receive report we will check that buffer/queue for corresponding message id and if we found that message id, then that report listener will continue it's work?

I think it's best to store report and message separately in the database and link them later on. Supposing your DB does what it's made for, this reduces the chance of data loss. Additionally, you let the DB do the difficult task of locking/synchronizing, and usually they are quite good at this.

I would store the data in a database and when you want to send a message to another system to read that data use JMS to send the message. This way you can ensure all the data is available when the message is received by the second system. The second system just needs to listen to messages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM