Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: potential information loss #493

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@

import java.io.Serializable;
import java.util.Date;
import java.util.Objects;

@Entity
@IdClass(ErpAdapterTriggerDataset.Key.class)
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class ErpAdapterTriggerDataset {

@Id
Expand Down Expand Up @@ -67,6 +67,20 @@ public String toString() {
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ErpAdapterTriggerDataset dataset)) return false;
return Objects.equals(partnerBpnl, dataset.partnerBpnl) && Objects.equals(ownMaterialNumber,
dataset.ownMaterialNumber) && assetType == dataset.assetType &&
Objects.equals(directionCharacteristic, dataset.directionCharacteristic);
}

@Override
public int hashCode() {
return Objects.hash(partnerBpnl, ownMaterialNumber, assetType, directionCharacteristic);
}

@Getter
@Setter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,22 @@ public class ErpAdapterTriggerService {

private final Runnable daemon = () -> {
log.info("Daemon thread started");
ArrayList<ErpAdapterTriggerDataset> updatedData = new ArrayList<>();
while (true) {
lock.lock();
try {
// keep the most recent information in case of conflict
for (ErpAdapterTriggerDataset dataset : datasets) {
for (ErpAdapterTriggerDataset updatedDataset : updatedData) {
if (dataset.equals(updatedDataset)) {
dataset.setLastPartnerRequest(
Math.max(dataset.getLastPartnerRequest(), updatedDataset.getLastPartnerRequest()));
dataset.setNextErpRequestScheduled(
Math.max(dataset.getNextErpRequestScheduled(), updatedDataset.getNextErpRequestScheduled()));
}
}
}
updatedData.clear();
repository.saveAll(datasets);
datasets.clear();
} finally {
Expand Down Expand Up @@ -103,6 +116,7 @@ public class ErpAdapterTriggerService {
// schedule next request
dataset.setNextErpRequestScheduled(now + erpAdapterConfiguration.getRefreshInterval());
dataset = repository.save(dataset);
updatedData.add(dataset);
log.info("Scheduled next erp adapter request: {}", dataset);
}
}
Expand Down
Loading