1. 问题描述

         通过rabbitmq队列,消费方出现不能消费消息的情况,经过细查rabbitmq的配置没有问题,然而消费方不能执行逻辑。

  交换机配置exchange:

  交换机绑定的队列:
 

       同样也发现properties文件中的配置是正确的:

spring.rabbitmq.port = 5672
spring.rabbitmq.username = admin
spring.rabbitmq.password = 
spring.rabbitmq.virtual-host = /dev
spring.rabbitmq.host = rabbitmq.dev

       

 2. 解决方案

        最后发现没有添加@RestController注解。 继承了AbstractWorkflowEventConsumerInterface的类上添加@RestController注解,即可解决问题!

      经过查看源码发现,最终执行的approve方法,是一个restFul接口,因此需要在类上添加一个@RestController将接口暴露出来。

@RestController
public class WorkflowEventConsumer extends AbstractWorkflowEventConsumerInterface {}

     其中  

@PostMapping({"/api/implement/workflow/approve"}) public abstract ApprovalResultCO approve(@RequestBody ApprovalNotificationCO approvalNoticeCO);

    是一个restFul接口。

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.hand.hcf.app.mdata.client.workflow.event;

import com.hand.hcf.app.mdata.base.util.OrgInformationUtil;
import com.hand.hcf.app.mdata.client.workflow.dto.ApprovalNotificationCO;
import com.hand.hcf.app.mdata.client.workflow.dto.ApprovalResultCO;
import com.hand.hcf.app.mdata.client.workflow.dto.WorkflowMessageCO;
import com.hand.hcf.app.mdata.client.workflow.enums.DocumentOperationEnum;
import com.hand.hcf.core.exception.BizException;
import com.hand.hcf.core.security.domain.PrincipalLite;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

public abstract class AbstractWorkflowEventConsumerInterface implements WorkflowEventConsumerInterface {
    @Value("${spring.application.name:}")
    private String applicationName;
    private static final Logger logger = LoggerFactory.getLogger(AbstractWorkflowEventConsumerInterface.class);

    public AbstractWorkflowEventConsumerInterface() {
    }

    @Transactional(
        rollbackFor = {Exception.class}
    )
    public void workFlowConsumer(WorkflowCustomRemoteEvent workflowCustomRemoteEvent) {
        String destinationService = this.applicationName + ":**";
        WorkflowMessageCO workflowMessage = workflowCustomRemoteEvent.getWorkflowMessage();
        if (destinationService.equalsIgnoreCase(workflowCustomRemoteEvent.getDestinationService()) && workflowMessage.getStatus() > DocumentOperationEnum.APPROVAL.getId()) {
            if (logger.isInfoEnabled()) {
                logger.info("接收到工作流事件消息:" + workflowCustomRemoteEvent);
            }

            PrincipalLite userBean = workflowMessage.getUserBean();
            OrgInformationUtil.setAuthentication(userBean);
            this.doWorkFlowConsumer(workflowCustomRemoteEvent, workflowMessage);
        }

    }

    protected void doWorkFlowConsumer(WorkflowCustomRemoteEvent workflowCustomRemoteEvent, WorkflowMessageCO workflowMessage) {
        ApprovalNotificationCO approvalNotificationCO = new ApprovalNotificationCO();
        approvalNotificationCO.setDocumentId(workflowMessage.getDocumentId());
        approvalNotificationCO.setDocumentOid(workflowMessage.getEntityOid());
        approvalNotificationCO.setDocumentCategory(Integer.parseInt(workflowMessage.getEntityType()));
        approvalNotificationCO.setDocumentStatus(workflowMessage.getStatus());
        approvalNotificationCO.setDocumentTypeId(workflowMessage.getDocumentTypeId());
        approvalNotificationCO.setDocumentTypeCode(workflowMessage.getDocumentTypeCode());
        ApprovalResultCO approvalResultCO = this.approve(approvalNotificationCO);
        if (Boolean.FALSE.equals(approvalResultCO.getSuccess())) {
            throw new BizException(approvalResultCO.getError());
        }
    }

    @PostMapping({"/api/implement/workflow/approve"})
    public abstract ApprovalResultCO approve(@RequestBody ApprovalNotificationCO approvalNoticeCO);
}

workflowEventConsumer接口:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.hand.hcf.app.mdata.client.workflow.event;

import org.springframework.context.event.EventListener;

public interface WorkflowEventConsumerInterface {
    @EventListener({WorkflowCustomRemoteEvent.class})
    void workFlowConsumer(WorkflowCustomRemoteEvent event);
}

3. 总结

      1. 检查服务有没有连接上消息队列。

      2. 检查队列绑定的路由器是不是和代码里一样的。

     3.检查接口,有没有被调用到。

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐