synchronize chat messages to avoid concurrency exception

This commit is contained in:
woodser 2024-02-10 07:20:43 -05:00
parent dc015ad83d
commit 548cae004d

View File

@ -1210,19 +1210,23 @@ public abstract class Trade implements Tradable, Model {
} }
public void addAndPersistChatMessage(ChatMessage chatMessage) { public void addAndPersistChatMessage(ChatMessage chatMessage) {
if (!chatMessages.contains(chatMessage)) { synchronized (chatMessages) {
chatMessages.add(chatMessage); if (!chatMessages.contains(chatMessage)) {
} else { chatMessages.add(chatMessage);
log.error("Trade ChatMessage already exists"); } else {
log.error("Trade ChatMessage already exists");
}
} }
} }
public boolean removeAllChatMessages() { public boolean removeAllChatMessages() {
if (chatMessages.size() > 0) { synchronized (chatMessages) {
chatMessages.clear(); if (chatMessages.size() > 0) {
return true; chatMessages.clear();
return true;
}
return false;
} }
return false;
} }
public boolean mediationResultAppliedPenaltyToSeller() { public boolean mediationResultAppliedPenaltyToSeller() {
@ -1646,6 +1650,12 @@ public abstract class Trade implements Tradable, Model {
throw new IllegalArgumentException("Trade is not buyer, seller, or arbitrator"); throw new IllegalArgumentException("Trade is not buyer, seller, or arbitrator");
} }
public ObservableList<ChatMessage> getChatMessages() {
synchronized (chatMessages) {
return FXCollections.observableArrayList(chatMessages);
}
}
public MessageState getPaymentSentMessageState() { public MessageState getPaymentSentMessageState() {
if (isPaymentReceived()) return MessageState.ACKNOWLEDGED; if (isPaymentReceived()) return MessageState.ACKNOWLEDGED;
if (processModel.getPaymentSentMessageStateProperty().get() == MessageState.ACKNOWLEDGED) return MessageState.ACKNOWLEDGED; if (processModel.getPaymentSentMessageStateProperty().get() == MessageState.ACKNOWLEDGED) return MessageState.ACKNOWLEDGED;
@ -2296,7 +2306,7 @@ public abstract class Trade implements Tradable, Model {
.setPayoutState(Trade.PayoutState.toProtoMessage(payoutState)) .setPayoutState(Trade.PayoutState.toProtoMessage(payoutState))
.setDisputeState(Trade.DisputeState.toProtoMessage(disputeState)) .setDisputeState(Trade.DisputeState.toProtoMessage(disputeState))
.setPeriodState(Trade.TradePeriodState.toProtoMessage(periodState)) .setPeriodState(Trade.TradePeriodState.toProtoMessage(periodState))
.addAllChatMessage(chatMessages.stream() .addAllChatMessage(getChatMessages().stream()
.map(msg -> msg.toProtoNetworkEnvelope().getChatMessage()) .map(msg -> msg.toProtoNetworkEnvelope().getChatMessage())
.collect(Collectors.toList())) .collect(Collectors.toList()))
.setLockTime(lockTime) .setLockTime(lockTime)