0

Spring AMQP 同期トランザクションのロールバックが機能しません。ここで、ソース内の DB トランザクションは Spring によって処理されません。Spring AMQP メッセージを 1 つのトランザクション内で送受信する必要があります。以下は、関連するコードのスナップショットです。他に何か必要な場合はお知らせください。

/////Connection Factory initialization

@Bean
public ConnectionFactory getConnectionFactory() {
    System.out.println("hello");
    configManager();
    String ip = ConfigManager.getQueueServerHost();
    System.out.println("IP Address : "+ip);
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory(ip);
    connectionFactory.setUsername(ConfigManager.getQueueUserName());
    connectionFactory.setPassword(ConfigManager.getQueuePassword());
    connectionFactory.setPort(ConfigManager.getQueueServerPort());
    //connectionFactory.setPublisherReturns(true);
    //connectionFactory.setPublisherConfirms(true);
    return connectionFactory;

}

/////Rabbit Template initialization

@Bean
public RabbitTemplate producerRabbitTemplate() {
    RabbitTemplate rabbitTemplate = new RabbitTemplate(getConnectionFactory());
    rabbitTemplate.setRoutingKey(ConfigManager.getProducerQueueName());
    rabbitTemplate.setQueue(ConfigManager.getProducerQueueName());
    rabbitTemplate.setMandatory(true);
    rabbitTemplate.setChannelTransacted(true);
    return rabbitTemplate;
}


/////Transactional Code

@Transactional(readOnly=false, rollbackFor=Exception.class)
public void processFile(RabbitTemplate rabbitTemplate)throws Exception{
    rabbitTemplate.setRoutingKey(ConfigManager.getConsumerQueueName());
    rabbitTemplate.setQueue(ConfigManager.getConsumerQueueName());

    Object messageObj = rabbitTemplate.receiveAndConvert();
    Message message = null;
    try{
        if(messageObj != null){
            if (messageObj instanceof Message){
                message = (Message)messageObj;
                System.out.println("Message received is '" + message.getFileName() + "' for Hospital "+message.getHospitalId());
                String newFileName = this.process(message.getFileName(), message.getHospitalId());
                this.sendMessage(newFileName, message.getHospitalId());
            }else{
                System.out.println("Unknown message received '" +  messageObj + "'");           
            }

        }
    }catch(Exception e){
        e.printStackTrace();
        throw e;
    }
}
4

1 に答える 1

0

私にとってはうまくいきます。テストケースが動作していることを示す Gist をアップロードしました。

レベル ロギングをオンにしてTRACE、すべてのトランザクション アクティビティを確認することをお勧めします (そして、それを Gist に入れたログと比較します)。

于 2013-09-17T14:01:47.870 に答える