Search in sources :

Example 1 with TpcMqMessage

use of com.paascloud.provider.model.domain.TpcMqMessage in project paascloud-master by paascloud.

the class TpcMqMessageServiceImpl method saveAndSendMessage.

@Override
public void saveAndSendMessage(TpcMqMessageDto tpcMqMessageDto) {
    if (StringUtils.isEmpty(tpcMqMessageDto.getMessageTopic())) {
        throw new TpcBizException(ErrorCodeEnum.TPC10050001);
    }
    Date now = new Date();
    TpcMqMessage message = new ModelMapper().map(tpcMqMessageDto, TpcMqMessage.class);
    message.setMessageStatus(MqSendStatusEnum.SENDING.sendStatus());
    message.setId(generateId());
    message.setUpdateTime(now);
    message.setCreatedTime(now);
    tpcMqMessageMapper.insertSelective(message);
    // 创建消费待确认列表
    this.createMqConfirmListByTopic(message.getMessageTopic(), message.getId(), message.getMessageKey());
    this.directSendMessage(tpcMqMessageDto.getMessageBody(), tpcMqMessageDto.getMessageTopic(), tpcMqMessageDto.getMessageTag(), tpcMqMessageDto.getMessageKey(), tpcMqMessageDto.getProducerGroup(), tpcMqMessageDto.getDelayLevel());
}
Also used : TpcMqMessage(com.paascloud.provider.model.domain.TpcMqMessage) TpcBizException(com.paascloud.provider.exceptions.TpcBizException) Date(java.util.Date) ModelMapper(org.modelmapper.ModelMapper)

Example 2 with TpcMqMessage

use of com.paascloud.provider.model.domain.TpcMqMessage in project paascloud-master by paascloud.

the class TpcMqMessageServiceImpl method setMessageToAlreadyDead.

@Override
public void setMessageToAlreadyDead(Long messageId) {
    final TpcMqMessage task = tpcMqMessageMapper.selectByPrimaryKey(messageId);
    if (task == null) {
        throw new TpcBizException(ErrorCodeEnum.TPC10050006);
    }
    tpcMqMessageMapper.updateAlreadyDeadByMessageId(messageId);
}
Also used : TpcMqMessage(com.paascloud.provider.model.domain.TpcMqMessage) TpcBizException(com.paascloud.provider.exceptions.TpcBizException)

Example 3 with TpcMqMessage

use of com.paascloud.provider.model.domain.TpcMqMessage in project paascloud-master by paascloud.

the class TpcMqMessageServiceImpl method resendMessageByMessageKey.

@Override
public void resendMessageByMessageKey(String messageKey) {
    final TpcMqMessage task = tpcMqMessageMapper.getByMessageKey(messageKey);
    this.resendMessage(task);
}
Also used : TpcMqMessage(com.paascloud.provider.model.domain.TpcMqMessage)

Example 4 with TpcMqMessage

use of com.paascloud.provider.model.domain.TpcMqMessage in project paascloud-master by paascloud.

the class TpcMqMessageServiceImpl method saveMessageWaitingConfirm.

@Override
public void saveMessageWaitingConfirm(TpcMqMessageDto messageDto) {
    if (StringUtils.isEmpty(messageDto.getMessageTopic())) {
        throw new TpcBizException(ErrorCodeEnum.TPC10050001);
    }
    Date now = new Date();
    TpcMqMessage message = new ModelMapper().map(messageDto, TpcMqMessage.class);
    message.setMessageStatus(MqSendStatusEnum.WAIT_SEND.sendStatus());
    message.setUpdateTime(now);
    message.setCreatedTime(now);
    tpcMqMessageMapper.insertSelective(message);
}
Also used : TpcMqMessage(com.paascloud.provider.model.domain.TpcMqMessage) TpcBizException(com.paascloud.provider.exceptions.TpcBizException) Date(java.util.Date) ModelMapper(org.modelmapper.ModelMapper)

Example 5 with TpcMqMessage

use of com.paascloud.provider.model.domain.TpcMqMessage in project paascloud-master by paascloud.

the class HandleSendingMessageJob method processJobData.

/**
 * Process job data.
 *
 * @param taskList the task list
 */
@Override
@Transactional(rollbackFor = Exception.class)
protected void processJobData(List<TpcMqMessage> taskList) {
    for (TpcMqMessage message : taskList) {
        Integer resendTimes = message.getResendTimes();
        if (resendTimes >= messageMaxSendTimes) {
            tpcMqMessageService.setMessageToAlreadyDead(message.getId());
            continue;
        }
        int times = (resendTimes == 0 ? 1 : resendTimes) * messageResendMultiplier;
        long currentTimeInMillis = Calendar.getInstance().getTimeInMillis();
        long needTime = currentTimeInMillis - times * 60 * 1000;
        long hasTime = message.getUpdateTime().getTime();
        // 判断是否达到了可以再次发送的时间条件
        if (hasTime > needTime) {
            log.debug("currentTime[" + com.xiaoleilu.hutool.date.DateUtil.formatDateTime(new Date()) + "],[SENDING]消息上次发送时间[" + com.xiaoleilu.hutool.date.DateUtil.formatDateTime(message.getUpdateTime()) + "],必须过了[" + times + "]分钟才可以再发送。");
            continue;
        }
        // 前置状态
        List<Integer> preStatusList = Lists.newArrayList(JobTaskStatusEnum.TASK_CREATE.status());
        // 设置任务状态为执行中
        message.setPreStatusList(preStatusList);
        message.setTaskStatus(JobTaskStatusEnum.TASK_EXETING.status());
        int updateRes = tpcMqMessageService.updateMqMessageTaskStatus(message);
        if (updateRes > 0) {
            try {
                // 查询是否全部订阅者都确认了消息 是 则更新消息状态完成, 否则重发消息
                int count = tpcMqConfirmMapper.selectUnConsumedCount(message.getMessageKey());
                int status = JobTaskStatusEnum.TASK_CREATE.status();
                if (count < 1) {
                    TpcMqMessage update = new TpcMqMessage();
                    update.setMessageStatus(MqSendStatusEnum.FINISH.sendStatus());
                    update.setId(message.getId());
                    tpcMqMessageService.updateMqMessageStatus(update);
                    status = JobTaskStatusEnum.TASK_SUCCESS.status();
                } else {
                    tpcMqMessageService.resendMessageByMessageId(message.getId());
                }
                // 前置状态
                preStatusList = Lists.newArrayList(JobTaskStatusEnum.TASK_EXETING.status());
                // 设置任务状态为执行中
                message.setPreStatusList(preStatusList);
                message.setTaskStatus(status);
                tpcMqMessageService.updateMqMessageTaskStatus(message);
            } catch (Exception e) {
                log.error("重发失败 ex={}", e.getMessage(), e);
                // 设置任务状态为执行中
                preStatusList = Lists.newArrayList(JobTaskStatusEnum.TASK_EXETING.status());
                message.setPreStatusList(preStatusList);
                message.setTaskStatus(JobTaskStatusEnum.TASK_SUCCESS.status());
                tpcMqMessageService.updateMqMessageTaskStatus(message);
            }
        }
    }
}
Also used : TpcMqMessage(com.paascloud.provider.model.domain.TpcMqMessage) Date(java.util.Date) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

TpcMqMessage (com.paascloud.provider.model.domain.TpcMqMessage)7 TpcBizException (com.paascloud.provider.exceptions.TpcBizException)5 Date (java.util.Date)4 ModelMapper (org.modelmapper.ModelMapper)2 Transactional (org.springframework.transaction.annotation.Transactional)1