2017-03-14 15 views
1

交易喜歡的是通過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 
}  
} 

有一個獨立的後臺庫存的過程,更新的SKU被出售或從上述trnsactions購買的清單創建銷售和採購。

public class InventoryProcessor { 

    @Scheduled(fixedRate = 900000,initialDelay = 3000) // 15 mins 
    @Transactional 
    public void doInventory() { 
    // open Transactions, update inventory records 
    } 

} 

此過程每15分鐘運行一次。但是,每當新的交易到達時,都需要明確地觸發或通知InventoryProcessordoInventory方法立即執行庫存。

春天有沒有選擇。

回答

0

您可以將InventoryProcessor注入到事務中並以編程方式調用該方法嗎?或者,如果需要異步完成,則將該調用包裝在另一個標記爲@Async的方法中。

@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 
}  
} 
+0

你能否解釋有點 – vels4j

+0

編輯,包括代碼 – Gee2113

+0

不能,請求的執行發佈自定義事件不能等到庫存處理器完成。庫存處理器是一個耗時的過程 – vels4j