Skip to content

Commit 769af67

Browse files
committed
BAEL-2435 Introduce Query message logic
Introduce a FindAllOrderedProductsQuery query message, which is published on the QueryGateway through the OrderRestEndpoint and handled by the OrderedProductsEventHandler
1 parent 84b6dc1 commit 769af67

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.axon.coreapi.queries;
2+
3+
public class FindAllOrderedProductsQuery {
4+
5+
}

axon/src/main/java/com/baeldung/axon/gui/OrderRestEndpoint.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
package com.baeldung.axon.gui;
22

3+
import java.util.List;
34
import java.util.UUID;
45

56
import org.axonframework.commandhandling.gateway.CommandGateway;
7+
import org.axonframework.messaging.responsetypes.ResponseTypes;
8+
import org.axonframework.queryhandling.QueryGateway;
9+
import org.springframework.web.bind.annotation.GetMapping;
610
import org.springframework.web.bind.annotation.PostMapping;
711
import org.springframework.web.bind.annotation.RestController;
812

913
import com.baeldung.axon.coreapi.commands.ConfirmOrderCommand;
1014
import com.baeldung.axon.coreapi.commands.PlaceOrderCommand;
1115
import com.baeldung.axon.coreapi.commands.ShipOrderCommand;
16+
import com.baeldung.axon.coreapi.queries.FindAllOrderedProductsQuery;
17+
import com.baeldung.axon.coreapi.queries.OrderedProduct;
1218

1319
@RestController
1420
public class OrderRestEndpoint {
1521

1622
private static final String DEFAULT_PRODUCT = "Deluxe Chair";
1723

1824
private final CommandGateway commandGateway;
25+
private final QueryGateway queryGateway;
1926

20-
public OrderRestEndpoint(CommandGateway commandGateway) {
27+
public OrderRestEndpoint(CommandGateway commandGateway, QueryGateway queryGateway) {
2128
this.commandGateway = commandGateway;
29+
this.queryGateway = queryGateway;
2230
}
2331

2432
@PostMapping("/ship-order")
@@ -37,4 +45,10 @@ public void shipUnconfirmedOrder() {
3745
commandGateway.send(new ShipOrderCommand(orderId));
3846
}
3947

48+
@GetMapping("/all-orders")
49+
public List<OrderedProduct> findAllOrderedProducts() {
50+
return queryGateway.query(new FindAllOrderedProductsQuery(), ResponseTypes.multipleInstancesOf(OrderedProduct.class))
51+
.join();
52+
}
53+
4054
}

axon/src/main/java/com/baeldung/axon/querymodel/OrderedProductsEventHandler.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package com.baeldung.axon.querymodel;
22

3+
import java.util.ArrayList;
34
import java.util.HashMap;
5+
import java.util.List;
46
import java.util.Map;
57

68
import org.axonframework.eventhandling.EventHandler;
9+
import org.axonframework.queryhandling.QueryHandler;
710
import org.springframework.stereotype.Service;
811

9-
import com.baeldung.axon.coreapi.queries.OrderedProduct;
1012
import com.baeldung.axon.coreapi.events.OrderConfirmedEvent;
1113
import com.baeldung.axon.coreapi.events.OrderPlacedEvent;
1214
import com.baeldung.axon.coreapi.events.OrderShippedEvent;
15+
import com.baeldung.axon.coreapi.queries.FindAllOrderedProductsQuery;
16+
import com.baeldung.axon.coreapi.queries.OrderedProduct;
1317

1418
@Service
1519
public class OrderedProductsEventHandler {
@@ -39,4 +43,9 @@ public void on(OrderConfirmedEvent event) {
3943
});
4044
}
4145

46+
@QueryHandler
47+
public List<OrderedProduct> handle(FindAllOrderedProductsQuery query) {
48+
return new ArrayList<>(orderedProducts.values());
49+
}
50+
4251
}

0 commit comments

Comments
 (0)