问题
Transactions likes sales and purchase that are created via REST
@Component
@Path("txns")
public class Transaction {
@Path("/purchases")
public Response postPurchaseTrnsaction(Transaction txn) {
// persistence takes place here
}
@Path("/sales")
public Response postSalesTrnsaction(Transaction txn) {
// persistence takes place here
}
}
There is a separate Background Inventory process that updates Inventory of SKUs which are sold or purchased from the above trnsactions.
public class InventoryProcessor {
@Scheduled(fixedRate = 900000,initialDelay = 3000) // 15 mins
@Transactional
public void doInventory() {
// open Transactions, update inventory records
}
}
This process runs every 15 mins. However, whenever a new transactions arrive, need to trigger or notify the InventoryProcessor
doInventory
method explicitly to perform inventory immediately.
Is there a option in spring.
回答1:
Can you inject InventoryProcessor into Transaction and call the method programatically? Or wrap the call in another method marked @Async if it needs to be done asynchronously.
@Component
@Path("txns")
public class Transaction {
@Inject
private InventoryProcessor inventoryProcessor
@Path("/purchases")
public Response postPurchaseTrnsaction(Transaction txn) {
// persistence takes place here
inventoryProcessor.doInventory();
}
@Path("/sales")
public Response postSalesTrnsaction(Transaction txn) {
// persistence takes place here
}
}
回答2:
You can implement observer pattern using ApplicationEventPulistherAware in your inventory Processor and have your transaction functions publish the custom events through an implementation of ApplicationEventPublisher
来源:https://stackoverflow.com/questions/42791023/spring-trigger-notify-explicitly-a-scheduled-method