Search in sources :

Example 1 with Receiver

use of org.hzero.boot.message.entity.Receiver in project choerodon-asgard by open-hand.

the class NoticeServiceImpl method sendSagaFailNotice.

@Override
@Async("notify-executor")
public void sendSagaFailNotice(SagaInstanceDTO instance) {
    // 捕获异常,以免影响saga一致性
    try {
        // 构建消息对象
        MessageSender messageSender = new MessageSender();
        // 消息code
        messageSender.setMessageCode(BusinessTypeCode.SAGA_INSTANCE_FAIL.value());
        // 默认为0L,都填0L,可不填写
        messageSender.setTenantId(0L);
        // 消息参数 消息模板中${projectName}
        Map<String, String> argsMap = new HashMap<>();
        argsMap.put("sagaInstanceId", instance.getId().toString());
        argsMap.put("sagaCode", instance.getSagaCode());
        argsMap.put("level", instance.getLevel());
        User user = iamFeignClient.queryInfo(instance.getCreatedBy()).getBody();
        // 接收者
        List<Receiver> receiverList = new ArrayList<>();
        Receiver receiver = new Receiver();
        receiver.setUserId(user.getId());
        // 发送邮件消息时 必填
        receiver.setEmail(user.getEmail());
        // 发送短信消息 必填
        receiver.setPhone(user.getPhone());
        // 必填
        receiver.setTargetUserTenantId(user.getOrganizationId());
        argsMap.put("userName", user.getRealName());
        receiverList.add(receiver);
        messageSender.setReceiverAddressList(receiverList);
        messageSender.setArgs(argsMap);
        Map<String, Object> objectMap = new HashMap<>();
        // 发送组织层和项目层消息时必填 当前组织id
        addSourceId(objectMap, instance.getLevel(), instance.getSourceId());
        messageSender.setAdditionalInformation(objectMap);
        messageClient.async().sendMessage(messageSender);
    } catch (Exception e) {
        LOGGER.error("saga instance fail send notice fail", e);
    }
}
Also used : MessageSender(org.hzero.boot.message.entity.MessageSender) Receiver(org.hzero.boot.message.entity.Receiver) CommonException(io.choerodon.core.exception.CommonException) Async(org.springframework.scheduling.annotation.Async)

Example 2 with Receiver

use of org.hzero.boot.message.entity.Receiver in project choerodon-asgard by open-hand.

the class NoticeServiceImpl method sendSagaFailNoticeForVindicator.

@Override
@Async("notify-executor")
public void sendSagaFailNoticeForVindicator(SagaInstanceDTO instance) {
    try {
        if (mapEvent.containsKey(instance.getSagaCode())) {
            List<User> userList = baseServiceClientOperator.listVindicators();
            if (!CollectionUtils.isEmpty(userList)) {
                Map<String, String> argsMap = new HashMap<>();
                argsMap.put("instanceId", instance.getId().toString());
                switch(instance.getSagaCode()) {
                    case IAM_CREATE_ORG_USER:
                    case IAM_CREATE_USER:
                    case IAM_CREATE_PROJECT:
                        Organization organization = baseServiceClientOperator.queryTenantById(instance.getSourceId());
                        argsMap.put("organizationName", organization.getTenantName());
                        break;
                    default:
                }
                MessageSender messageSender = new MessageSender();
                messageSender.setArgs(argsMap);
                messageSender.setMessageCode(mapEvent.get(instance.getSagaCode()));
                messageSender.setTenantId(0L);
                List<Receiver> receiverList = userList.stream().map(t -> {
                    Receiver receiver = new Receiver();
                    receiver.setUserId(t.getId());
                    receiver.setEmail(t.getEmail());
                    receiver.setPhone(t.getPhone());
                    receiver.setTargetUserTenantId(Objects.requireNonNull(t.getOrganizationId(), "receiver tenant id can't be null"));
                    return receiver;
                }).collect(Collectors.toList());
                Map<String, Object> objectMap = new HashMap<>();
                // 发送组织层和项目层消息时必填 当前组织id
                addSourceId(objectMap, instance.getLevel(), instance.getSourceId());
                messageSender.setAdditionalInformation(objectMap);
                messageSender.setReceiverAddressList(receiverList);
                messageClient.async().sendMessage(messageSender);
            }
        }
    } catch (Exception e) {
        LOGGER.error("saga instance fail send notice fail for vindicator create event", e);
    }
}
Also used : BusinessTypeCode(io.choerodon.asgard.infra.enums.BusinessTypeCode) Async(org.springframework.scheduling.annotation.Async) java.util(java.util) MessageAdditionalType(io.choerodon.core.enums.MessageAdditionalType) NoticeService(io.choerodon.asgard.app.service.NoticeService) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) io.choerodon.asgard.api.vo(io.choerodon.asgard.api.vo) QuartzTaskMemberDTO(io.choerodon.asgard.infra.dto.QuartzTaskMemberDTO) Service(org.springframework.stereotype.Service) MessageClient(org.hzero.boot.message.MessageClient) CommonException(io.choerodon.core.exception.CommonException) MessageSender(org.hzero.boot.message.entity.MessageSender) Logger(org.slf4j.Logger) BaseServiceClientOperator(io.choerodon.asgard.infra.feign.operator.BaseServiceClientOperator) QuartzTaskDTO(io.choerodon.asgard.infra.dto.QuartzTaskDTO) Collectors(java.util.stream.Collectors) SagaInstanceDTO(io.choerodon.asgard.infra.dto.SagaInstanceDTO) Receiver(org.hzero.boot.message.entity.Receiver) CollectionUtils(org.springframework.util.CollectionUtils) MemberType(io.choerodon.asgard.infra.enums.MemberType) SagaTaskInstanceDTO(io.choerodon.asgard.infra.dto.SagaTaskInstanceDTO) IamFeignClient(io.choerodon.asgard.infra.feign.IamFeignClient) BeanUtils(org.springframework.beans.BeanUtils) ResourceLevel(io.choerodon.core.iam.ResourceLevel) MessageSender(org.hzero.boot.message.entity.MessageSender) Receiver(org.hzero.boot.message.entity.Receiver) CommonException(io.choerodon.core.exception.CommonException) Async(org.springframework.scheduling.annotation.Async)

Example 3 with Receiver

use of org.hzero.boot.message.entity.Receiver in project choerodon-message by open-hand.

the class RelSendMessageC7nServiceImpl method filterReceiver.

private void filterReceiver(MessageSender messageSender, String messageType) {
    TemplateServer templateServer = templateServerService.getTemplateServer(messageSender.getTenantId(), messageSender.getMessageCode());
    Long tempServerId = templateServer.getTempServerId();
    Long projectId = null;
    Long envId = null;
    String eventName = null;
    if (!CollectionUtils.isEmpty(messageSender.getAdditionalInformation()) && !ObjectUtils.isEmpty(messageSender.getAdditionalInformation().get(MessageAdditionalType.PARAM_PROJECT_ID.getTypeName()))) {
        projectId = Long.valueOf(String.valueOf(messageSender.getAdditionalInformation().get(MessageAdditionalType.PARAM_PROJECT_ID.getTypeName())));
    }
    if (!CollectionUtils.isEmpty(messageSender.getAdditionalInformation()) && !ObjectUtils.isEmpty(messageSender.getAdditionalInformation().get(MessageAdditionalType.PARAM_ENV_ID.getTypeName()))) {
        envId = Long.valueOf(String.valueOf(messageSender.getAdditionalInformation().get(MessageAdditionalType.PARAM_ENV_ID.getTypeName())));
    }
    if (!CollectionUtils.isEmpty(messageSender.getAdditionalInformation()) && !ObjectUtils.isEmpty(messageSender.getAdditionalInformation().get(MessageAdditionalType.PARAM_EVENT_NAME.getTypeName()))) {
        eventName = String.valueOf(messageSender.getAdditionalInformation().get(MessageAdditionalType.PARAM_EVENT_NAME.getTypeName()));
    }
    List<Receiver> receiverList = messageSender.getReceiverAddressList();
    if (messageType.equals(HmsgConstant.MessageType.WEB) || messageType.equals(HmsgConstant.MessageType.EMAIL)) {
        receiveFilter(receiverList, tempServerId, projectId, messageType);
    }
    // 如果项目层未启动 则不发送 接受者为null
    if ((messageType.equals(HmsgConstant.MessageType.WEB) || messageType.equals(HmsgConstant.MessageType.EMAIL) || messageType.equals(HmsgConstant.MessageType.SMS)) && messageSettingC7nMapper.selectProjectMessage().contains(messageSender.getMessageCode())) {
        // 项目层设置未开启
        if (!projectFilter(messageSender, projectId, envId, eventName, messageType)) {
            receiverList.clear();
        }
    }
}
Also used : Receiver(org.hzero.boot.message.entity.Receiver) TemplateServer(org.hzero.message.domain.entity.TemplateServer)

Example 4 with Receiver

use of org.hzero.boot.message.entity.Receiver in project choerodon-message by open-hand.

the class C7nWebSendServiceImpl method sendMessage.

@Override
public Message sendMessage(Long organizationId, MessageSender messageSender) {
    Message message = super.sendMessage(organizationId, messageSender);
    String marshalByJackson = JsonHelper.marshalByJackson(message);
    LOGGER.info(">>>>>>>>>>>>>marshalByJackson:{}>>", marshalByJackson);
    // 推送给前端
    /**
     * {
     * 	"objectVersionNumber": 3,
     * 	"_token": "HuFIjmaG4cz+TpuCS5HbgArigzGiq+XyrilqFCmujSs3G2VY1/eO0IX8Et2UIAxgOiUxFDmdWD6PviC57nxvmNeCOUuFWjf4X3X/Z8v1uqsy/0KBqoGc9k47eOFR8J0R",
     * 	"messageId": 182536898873528320,
     * 	"tenantId": 0,
     * 	"messageTypeCode": "WEB",
     * 	"templateCode": "DISABLE_ORGANIZATION.WEB",
     * 	"lang": "zh_CN",
     * 	"subject": "组织停用",
     * 	"content": "<p>您好,</p><p>您所在的组织:nyintel 已被停用。</p>",
     * 	"sendFlag": 1,
     * 	"transactionId": 182536915520720896,
     * 	"plainContent": "<p>您好,</p><p>您所在的组织:nyintel 已被停用。</p>",
     * 	"templateEditType": "RT"
     * }
     */
    // 获得消息的接收者
    List<Receiver> receiverAddressList = messageSender.getReceiverAddressList();
    PopMessageVO popMessageVO = new PopMessageVO();
    popMessageVO.setContent(message.getContent());
    popMessageVO.setTitle(message.getSubject());
    popMessageVO.setMessageId(message.getMessageId());
    // 推送给前端
    receiverAddressList.forEach(receiver -> {
        socketSendHelper.sendByUserId(receiver.getUserId(), "choerodon-pop-ups", JsonHelper.marshalByJackson(popMessageVO));
    });
    return message;
}
Also used : Message(org.hzero.message.domain.entity.Message) PopMessageVO(io.choerodon.message.api.vo.PopMessageVO) Receiver(org.hzero.boot.message.entity.Receiver)

Example 5 with Receiver

use of org.hzero.boot.message.entity.Receiver in project agile-service by open-hand.

the class SiteMsgUtil method sendProjectReport.

@Async
public void sendProjectReport(Long projectId, List<ProjectReportReceiverDTO> receiverList, String html) {
    // 获取接收人, 抄送人
    Map<String, List<ProjectReportReceiverDTO>> group = receiverList.stream().collect(Collectors.groupingBy(ProjectReportReceiverDTO::getType));
    List<Long> toList = group.get(ProjectReportReceiverDTO.TYPE_RECEIVER).stream().map(ProjectReportReceiverDTO::getReceiverId).collect(Collectors.toList());
    Assert.notNull(toList, BaseConstants.ErrorCode.DATA_NOT_EXISTS);
    List<Long> ccList = group.getOrDefault(ProjectReportReceiverDTO.TYPE_CC, Collections.emptyList()).stream().map(ProjectReportReceiverDTO::getReceiverId).collect(Collectors.toList());
    List<Receiver> toReceiver = new ArrayList<>();
    List<Receiver> ccReceiver = new ArrayList<>();
    handleReceiver(toReceiver, toList);
    Assert.isTrue(CollectionUtils.isNotEmpty(toList), BaseConstants.ErrorCode.DATA_NOT_EXISTS);
    handleReceiver(ccReceiver, ccList);
    // 设置参数
    Map<String, String> argsMap = new HashMap<>();
    argsMap.put("data", html);
    // 设置sender
    MessageSender sender = new MessageSender();
    sender.setMessageCode("PROJECT_REPORT");
    sender.setTenantId(ConvertUtil.getOrganizationId(projectId));
    sender.setReceiverAddressList(toReceiver);
    // 额外参数
    Map<String, Object> objectMap = new HashMap<>();
    objectMap.put(MessageAdditionalType.PARAM_PROJECT_ID.getTypeName(), projectId);
    sender.setAdditionalInformation(objectMap);
    sender.setCcList(ccReceiver.stream().map(Receiver::getEmail).collect(Collectors.toList()));
    sender.setArgs(argsMap);
    messageClient.async().sendMessage(sender);
}
Also used : MessageSender(org.hzero.boot.message.entity.MessageSender) Receiver(org.hzero.boot.message.entity.Receiver) Async(org.springframework.scheduling.annotation.Async)

Aggregations

Receiver (org.hzero.boot.message.entity.Receiver)24 MessageSender (org.hzero.boot.message.entity.MessageSender)12 CommonException (io.choerodon.core.exception.CommonException)5 ProjectDTO (io.choerodon.devops.infra.dto.iam.ProjectDTO)5 Async (org.springframework.scheduling.annotation.Async)5 IamUserDTO (io.choerodon.devops.infra.dto.iam.IamUserDTO)4 java.util (java.util)4 Collectors (java.util.stream.Collectors)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 Service (org.springframework.stereotype.Service)4 Tenant (io.choerodon.devops.infra.dto.iam.Tenant)3 UserDTO (io.choerodon.agile.infra.dto.UserDTO)2 MessageAdditionalType (io.choerodon.core.enums.MessageAdditionalType)2 MessageSettingVO (io.choerodon.devops.api.vo.notify.MessageSettingVO)2 TargetUserDTO (io.choerodon.devops.api.vo.notify.TargetUserDTO)2 MessageCodeConstants (io.choerodon.devops.infra.constant.MessageCodeConstants)2 BaseServiceClientOperator (io.choerodon.devops.infra.feign.operator.BaseServiceClientOperator)2 PageRequest (io.choerodon.mybatis.pagehelper.domain.PageRequest)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2