fix: 发送通知时线程安全问题

This commit is contained in:
CaptainB 2021-10-25 15:21:00 +08:00 committed by 刘瑞斌
parent e2808365ce
commit 63037ffb1c
3 changed files with 14 additions and 5 deletions

View File

@ -2,11 +2,12 @@ package io.metersphere.notice.domain;
import lombok.Data; import lombok.Data;
import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@Data @Data
public class MessageDetail { public class MessageDetail implements Serializable {
private List<String> userIds = new ArrayList<>(); private List<String> userIds = new ArrayList<>();
private String id; private String id;
private String event; private String event;

View File

@ -4,12 +4,13 @@ import io.metersphere.notice.domain.Receiver;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import java.io.Serializable;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@Data @Data
@Builder @Builder
public class NoticeModel { public class NoticeModel implements Serializable {
/** /**
* 保存 测试id * 保存 测试id
*/ */

View File

@ -8,6 +8,7 @@ import io.metersphere.notice.domain.MessageDetail;
import io.metersphere.notice.sender.AbstractNoticeSender; import io.metersphere.notice.sender.AbstractNoticeSender;
import io.metersphere.notice.sender.NoticeModel; import io.metersphere.notice.sender.NoticeModel;
import io.metersphere.notice.sender.impl.*; import io.metersphere.notice.sender.impl.*;
import org.apache.commons.lang3.SerializationUtils;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -66,7 +67,9 @@ public class NoticeSendService {
messageDetails.stream() messageDetails.stream()
.filter(messageDetail -> StringUtils.equals(messageDetail.getEvent(), noticeModel.getEvent())) .filter(messageDetail -> StringUtils.equals(messageDetail.getEvent(), noticeModel.getEvent()))
.forEach(messageDetail -> { .forEach(messageDetail -> {
this.getNoticeSender(messageDetail).send(messageDetail, noticeModel); MessageDetail m = SerializationUtils.clone(messageDetail);
NoticeModel n = SerializationUtils.clone(noticeModel);
this.getNoticeSender(m).send(m, n);
}); });
} catch (Exception e) { } catch (Exception e) {
@ -102,7 +105,9 @@ public class NoticeSendService {
messageDetails.stream() messageDetails.stream()
.filter(messageDetail -> StringUtils.equals(messageDetail.getEvent(), noticeModel.getEvent())) .filter(messageDetail -> StringUtils.equals(messageDetail.getEvent(), noticeModel.getEvent()))
.forEach(messageDetail -> { .forEach(messageDetail -> {
this.getNoticeSender(messageDetail).send(messageDetail, noticeModel); MessageDetail m = SerializationUtils.clone(messageDetail);
NoticeModel n = SerializationUtils.clone(noticeModel);
this.getNoticeSender(m).send(m, n);
}); });
} catch (Exception e) { } catch (Exception e) {
@ -133,7 +138,9 @@ public class NoticeSendService {
messageDetails.stream() messageDetails.stream()
.filter(messageDetail -> StringUtils.equals(messageDetail.getEvent(), noticeModel.getEvent())) .filter(messageDetail -> StringUtils.equals(messageDetail.getEvent(), noticeModel.getEvent()))
.forEach(messageDetail -> { .forEach(messageDetail -> {
this.getNoticeSender(messageDetail).send(messageDetail, noticeModel); MessageDetail m = SerializationUtils.clone(messageDetail);
NoticeModel n = SerializationUtils.clone(noticeModel);
this.getNoticeSender(m).send(m, n);
}); });
} catch (Exception e) { } catch (Exception e) {