简体   繁体   中英

Java: best practice to update data online when internet can be interrupted


I have 2 applications:

  • desktop (java)
  • web (symfony)

I have some data in the desktop app that must be consistent with the data in the web app.
So basically I send a POST request from the desktop app to the web app to update online data.

But the problem is that the internet cannot always be available when I send my request and in the same time I can't prevent the user from updating the desktop data
So far, this is what I have in mind to make sure to synchronize data when the internet is available.

在此处输入图片说明 Am I on the right direction or not ?
If not, I hope you guys put me in the right path to achieve my goal in a professional way.
Any link about this kind of topics will be appreciated.

In this case the usefull pattern is to assume that sending data is asynchronous by default. The data, after collecting, are stored in some intermediate structure and wait for a sutable moment to be send. I think the queue could be useful because it can be backend with a database and prevent data lost in case of the sending server failure. Separate thread (eg a job) check for data in the queue and if exists, read them and try to send. If sending was performed correctly the data are removed from queue. If failure occurs, the data stays in queue and an attempt will be made to send them next time.

This is a typical scenario when you want to send a message to an un-transactional external system in a transaction and you need to garantee that data will be transfered to the external system as soon as possible without losing it.

2 solutions come up in my mind, maybe the second fits better to your architecture.

Use case 1)

You can use message queue + redelivery limit setting with dead letter pattern. In t that case you need to have an application server.

Here you can read details about the Dead letter pattern.

This document explain how redelivery limit works on Weblogic server.

Use case 2)

You can create an interface table in the database of the destop application. Then insert your original data into database and insert a new record into the interface table as well (all in same transaction). The data what you want to POST needs to be inserted into the interface table as well. The status flag of the new record in the interface table can be " ARRIVED ". Then create an independent timer in your desktop app which search periodically for records in the interface table with status " ARRIVED ". This timer controlled process will try to POST data to webservice. If the HTTP response is 200 then update the status of the record to " SENT ".

Boot can work like a charm.

You can solve it many way. Here give 2 way:

1.You can use circuit breaker pattern . You can get link about it from here

  1. You can use JMS concept to manage this.

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