temirlan100

Untitled

Dec 2nd, 2024
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. import io.javalin.Javalin;
  2. import org.apache.kafka.streams.KafkaStreams;
  3. import org.apache.kafka.streams.StoreQueryParameters;
  4. import org.apache.kafka.streams.state.QueryableStoreTypes;
  5. import org.apache.kafka.streams.state.ReadOnlyKeyValueStore;
  6.  
  7. public class InventoryRestService {
  8.  
  9.     private final KafkaStreams streams;
  10.  
  11.     public InventoryRestService(KafkaStreams streams) {
  12.         this.streams = streams;
  13.     }
  14.  
  15.     public void start() {
  16.         var app = Javalin.create().start(7777);
  17.  
  18.         app.get("/inventory/{productId}", ctx -> {
  19.             String productId = ctx.pathParam("productId");
  20.  
  21.             ReadOnlyKeyValueStore<String, Integer> keyValueStore = streams.store(
  22.                     StoreQueryParameters.fromNameAndType("inventory-store", QueryableStoreTypes.keyValueStore())
  23.             );
  24.  
  25.             var quantity = keyValueStore.get(productId);
  26.             if (quantity != null) {
  27.                 ctx.json(new InventoryResponse(productId, quantity));
  28.             } else {
  29.                 ctx.status(404).result("Product not found");
  30.             }
  31.         });
  32.     }
  33.  
  34.     public record InventoryResponse(String productId, int quantity) {
  35.     }
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment