简体   繁体   English

Java:互联网中断时在线更新数据的最佳实践

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


I have 2 applications: 我有2个应用程序:

  • desktop (java) 桌面(java)
  • web (symfony) 网络(symfony)

I have some data in the desktop app that must be consistent with the data in the web app. 我的桌面应用程序中有一些数据必须与Web应用程序中的数据一致。
So basically I send a POST request from the desktop app to the web app to update online data. 因此,基本上,我从台式机应用程序向Web应用程序发送了POST请求以更新在线数据。

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. 我想到了2个解决方案,也许第二个更适合您的体系结构。

Use case 1) 用例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. 文档说明了重新交付限制如何在Weblogic服务器上工作。

Use case 2) 用例2)

You can create an interface table in the database of the destop application. 您可以在destop应用程序的数据库中创建接口表。 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. 此计时器控制的过程将尝试将数据发布到Web服务。 If the HTTP response is 200 then update the status of the record to " SENT ". 如果HTTP响应为200,则将记录的状态更新为“ SENT ”。

Boot can work like a charm. 靴子可以像魅力一样工作。

You can solve it many way. 您可以通过多种方式解决它。 Here give 2 way: 这里给出两种方式:

1.You can use circuit breaker pattern . 1.您可以使用circuit breaker pattern You can get link about it from here 您可以从这里获得有关它的链接

  1. You can use JMS concept to manage this. 您可以使用JMS概念进行管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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