@Transactional(rollbackFor = Exception.class) TackAnOrderVo tackAnOrder(@RequestBody @Validated PlaceOrderVo vo){

//提交订单

//提交库存

/提交其它

// 发送消息到 MQ TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter(){ @Override public void afterCommit() { //发送mq更新es库存标志 for (Long drugId : drugIdList) { DrugChangeMessage drugChangeMessage = new DrugChangeMessage(); drugChangeMessage.setDrugId(drugId); drugChangeMessage.setStoreId(order.getStoreId()); drugChangeMessage.setChangeType(5); drugChangeMessage.setMerchantId(Long.parseLong(merchantId)); drugChangeMessage.setEnterpriseId(Long.parseLong(enterpriseId)); drugChangeMessage.setPlatformId(Long.parseLong(platformId)); drugProducer.sendDrugChangeMsg(drugChangeMessage); }

}

});

}

rocketmq消息在其它服务消费,查询库存还是扣减前的库存数量,怎么改代码,消费是查询库存时是变更后的库存内容:To ensure that the consumer service queries the updated inventory quantity after the deduction, you can modify the code in the consumer service to query the inventory after the deduction.

One way to achieve this is to update the inventory quantity in the database immediately after deducting it in the producer service. Then, in the consumer service, you can query the updated inventory quantity from the database instead of relying solely on the message received from the producer service.

Here's an example of how you can modify the consumer service code to query the updated inventory quantity:

@Service
public class InventoryService {

    @Autowired
    private InventoryRepository inventoryRepository;

    public void processInventoryChange(DrugChangeMessage message) {
        // Get the drugId and storeId from the message
        Long drugId = message.getDrugId();
        Long storeId = message.getStoreId();

        // Query the updated inventory quantity from the database
        Inventory inventory = inventoryRepository.findByDrugIdAndStoreId(drugId, storeId);

        // Use the updated inventory quantity for further processing
        int updatedQuantity = inventory.getQuantity();
        // ...
    }
}

In this example, InventoryRepository is a repository interface that provides methods to query the inventory from the database. The findByDrugIdAndStoreId method is used to retrieve the inventory based on the drugId and storeId.

By querying the inventory from the database in the consumer service, you can ensure that you are always using the updated inventory quantity after the deduction.

@Transactional(rollbackFor = Exception.class) - Ensuring Updated Inventory Quantity in Consumer Service

原文地址: https://www.cveoy.top/t/topic/pgKQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录