簡體   English   中英

RabbitMQ Spring 雲 stream 僅在提交數據庫事務時發布消息

[英]RabbitMQ Spring cloud stream Publish message only when database transaction is committed

我有這個用例,其中只有在事務提交到數據庫時才必須發布消息,現在偵聽器在事務提交到數據庫之前選擇消息,這將導致偵聽器端出現異常.. .我正在重試使用DLQ在偵聽器處處理消息,但我想首先避免這個問題,因為我在多個地方都有這個問題,並且所有日志都被重試嘗試污染了。

I have followed the approach documented here but somehow am not able to make it work Transaction in Spring cloud Stream , here RabbitTransactionManager bean is getting created but it's not referred anywhere, not sure how's the wiring happening with rest of the stuff,

這是我嘗試過的代碼,有人可以告訴我我在哪里出錯了嗎?

@Component
@Slf4j
@NoArgsConstructor
public class MQSender {

    public boolean send(MessageChannel messageChannel, AbstractMqMsg abstractMqMsg) {
        return messageChannel.send(MessageBuilder.withPayload(abstractMqMsg).build());
    }
}


public interface MetadataMQChannels {
    @Output(QueueConstants.VALIDATE_METADATA_CHANNEL)
    MessageChannel validateMetadataPublishChannel();
}

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class QueueConstants {
    public static final String VALIDATE_METADATA_CHANNEL = "validateMetadataPublishChannel";
}
#application-properties 
spring.cloud.stream.rabbit.bindings.validateMetadataPublishChannel.producer.transacted=true
spring.cloud.stream.rabbit.bindings.validateMetadataPublishChannel.producer.routingKeyExpression='validatemetadata'
spring.cloud.stream.bindings.validateMetadataErrorPublishChannel.destination=metadata-error-exchange
spring.cloud.stream.bindings.validateMetadataErrorPublishChannel.producer.requiredGroups=validate-metadata-error-queue
spring.cloud.stream.rabbit.bindings.validateMetadataErrorPublishChannel.producer.exchangeType=direct
@Service
@Slf4j
class ValidatePackage{

    @Autowired
    private MQSender mqSender;
    
    @Autowired
    private MetadataMQChannels metadataMQChannels;
    
    @Autowired
    private MetadataService metadataService;
    
    //Publishing the message in the context of transaction 
    @Transactional(rollbackFor = Exception.class)
    public MetaDataDto create(MetaDataDto metadata) throws BaseException {
        
        metadataService.saveMetadata(); //persist dto to the Database
        mqSender.send(metadataMQChannels.validateMetadataChannel(), metadata); //publish the message 
    }
}


@SpringBootApplication
@Slf4j
@EnableBinding(value = {MetadataMQChannels.class})
@EnableAsync
@EnableTransactionManagement
public class ManagerApplication {

    public static void main(String[] args) {

        SpringApplication.run(ManagerApplication.class);
        log.info("Manager Application has been started successfully");
    }
    
    @Bean
    public RabbitTransactionManager transactionManager(ConnectionFactory cf) {
        return new RabbitTransactionManager(cf);
    }
}

您需要設置transacted的兔子生產者綁定屬性,以便其RabbitTemplate將參與交易

https://docs.spring.io/spring-cloud-stream-binder-rabbit/docs/3.0.10.RELEASE/reference/html/spring-cloud-stream-binder-rabbit.html#rabbit-prod-props

成交

是否使用交易渠道。

默認值:假。

您似乎已將其設置為 true,因此必須進行其他操作。 我建議在RabbitTemplate.execute中設置一個斷點,看看發生了什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM