Spring trigger/notify explicitly a Scheduled method
Asked Answered
C

2

1

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.

Cowan answered 14/3, 2017 at 16:9 Comment(0)
M
0

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
 }     
}
Market answered 14/3, 2017 at 16:39 Comment(2)
can you elaborate bit moreCowan
Can not, Request can not wait until inventory processor complete. inventory processor is time consuming processCowan
D
0

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

Decani answered 14/3, 2017 at 21:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.