public interface MessageQuery extends MetaModelQueryDsl<Message,MessageQuery,MessageQueryModel,MessageExpansionModel<Message>>
For further information how to use the query API to consult the Query API documentation.
withOrderAndReturnInfo(client(), ((order, returnInfo) -> {
final MessageQuery query = MessageQuery.of()
//example predicate to fetch for a specific message type
.withPredicates(m -> m.type().is("ReturnInfoAdded").and(m.resource().is(order)))
.withSort(m -> m.createdAt().sort().desc())
.withExpansionPaths(m -> m.resource())
.withLimit(1L);
assertEventually(() -> {
final Optional<Message> message = client().executeBlocking(query).head();
assertThat(message).isPresent();
assertThat(message.get().getResource().getObj()).isNotNull();
assertThat(message.get().getResource()).isEqualTo(order.toReference());
assertThat(message.get().getResource().getId()).isEqualTo(order.getId());
});
return order;
}));
See the test code.
withOrderAndReturnInfo(client(), ((order, returnInfo) -> {
final Query<ReturnInfoAddedMessage> query =
MessageQuery.of()
.withPredicates(m -> m.resource().is(order))
.withSort(m -> m.createdAt().sort().desc())
.withExpansionPaths(m -> m.resource())
.withLimit(1L)
.forMessageType(ReturnInfoAddedMessage.MESSAGE_HINT);
assertEventually(() -> {
final PagedQueryResult<ReturnInfoAddedMessage> pagedQueryResult = client().executeBlocking(query);
final Optional<ReturnInfoAddedMessage> message = pagedQueryResult.head();
assertThat(message).isPresent();
assertThat(message.get().getReturnInfo()).isEqualTo(returnInfo);
assertThat(message.get().getResource().getObj()).isNotNull();
assertThat(message.get().getResource().getId()).isEqualTo(order.getId());
});
return order;
}));
See the test code.
ProductFixtures.withUpdateableProduct(client(), product -> {
//create some messages apart from ProductCreatedMessage
final Product publishedProduct = client().executeBlocking(ProductUpdateCommand.of(product, Publish.of()));
final Product unpublishedProduct = client().executeBlocking(ProductUpdateCommand.of(publishedProduct, Unpublish.of()));
//these are the classes which are expected as message
final List<MessageDerivateHint<? extends Message>> messageHints =
asList(ProductCreatedMessage.MESSAGE_HINT,
ProductPublishedMessage.MESSAGE_HINT,
//as fallback for other product messages
SimpleProductMessage.MESSAGE_HINT);
final Query<Message> query = MessageQuery.of()
.withPredicates(m -> m.resource().is(product))
.withSort(m -> m.type().sort().asc())
.withExpansionPaths(m -> m.resource())
.forMessageTypes(messageHints);
assertEventually(Duration.ofSeconds(45), Duration.ofMillis(300), () -> {
final List<Message> messages = client().executeBlocking(query).getResults();
assertThat(messages).hasSize(3);
assertThat(messages.get(0)).isInstanceOf(ProductCreatedMessage.class);
assertThat(messages.get(1)).isInstanceOf(ProductPublishedMessage.class);
assertThat(messages.get(2)).isInstanceOf(SimpleProductMessage.class);
//use some kind of pattern matching
messages.stream()
.forEachOrdered(message -> {
if (message instanceof ProductCreatedMessage) {
final ProductCreatedMessage m = (ProductCreatedMessage) message;
assertThat(m.getResource()).isEqualTo(product.toReference());
} else if (message instanceof ProductPublishedMessage) {
final ProductPublishedMessage m = (ProductPublishedMessage) message;
assertThat(m.getResource()).isEqualTo(product.toReference());
} else if (message instanceof SimpleProductMessage) {
final SimpleProductMessage m = (SimpleProductMessage) message;
assertThat(m.getResource()).isEqualTo(product.toReference());
assertThat(m.getType()).isEqualTo("ProductUnpublished");
} else {
throw new RuntimeException("unexpected type of " + message);
}
});
});
return unpublishedProduct;
});
See the test code.
withOrderAndReturnInfo(client(), ((order, returnInfo) -> {
final MessageQuery query = MessageQuery.of()
.withPredicates(m -> m.resource().is(order))
.withSort(m -> m.createdAt().sort().desc())
.withExpansionPaths(m -> m.resource());
assertEventually(() -> {
final List<Message> results = client().executeBlocking(query).getResults();
final Optional<Message> returnInfoAddedUntypedMessage = results.stream()
.filter(m -> {
final String messageType = ReturnInfoAddedMessage.MESSAGE_TYPE;
return m.getType().equals(messageType);
})
.findFirst();
assertThat(returnInfoAddedUntypedMessage).isPresent();
final ReturnInfoAddedMessage returnInfoAddedMessage =
returnInfoAddedUntypedMessage.get().as(ReturnInfoAddedMessage.class);
assertThat(order.getReturnInfo()).contains(returnInfoAddedMessage.getReturnInfo());
final Order expandedOrder = returnInfoAddedMessage.getResource().getObj();
assertThat(expandedOrder.getCreatedAt()).isEqualTo(order.getCreatedAt());
});
return order;
}));
See the test code.
withOrderAndReturnInfo(client(), ((order, returnInfo) -> {
final Query<SimpleOrderMessage> query = MessageQuery.of()
.withPredicates(m -> m.resource().id().is(order.getId()))
.withSort(m -> m.createdAt().sort().desc())
.withExpansionPaths(m -> m.resource())
.forMessageType(SimpleOrderMessage.MESSAGE_HINT);
assertEventually(() -> {
final List<SimpleOrderMessage> results = client().executeBlocking(query).getResults();
final Optional<Order> orderOptional = Optional.ofNullable(results.get(0).getResource().getObj());
assertThat(orderOptional.map(o -> o.getCreatedAt())).contains(order.getCreatedAt());
});
return order;
}));
See the test code.
MAX_OFFSET, MIN_OFFSET
Modifier and Type | Method and Description |
---|---|
default <T extends Message> |
forMessageType(MessageDerivateHint<T> hint)
Creates a new query that queries only for messages that can be mapped to a certain Java type.
|
default Query<Message> |
forMessageTypes(List<MessageDerivateHint<? extends Message>> messageHints)
Creates a new query that queries only for messages that can be mapped to certain Java types.
|
static MessageQuery |
of() |
static com.fasterxml.jackson.core.type.TypeReference<PagedQueryResult<Message>> |
resultTypeReference()
Creates a container which contains the full Java type information to deserialize the query result (NOT this class) from JSON.
|
plusPredicates, plusPredicates, plusPredicates, plusPredicates, plusSort, plusSort, plusSort, plusSort, withPredicates, withPredicates, withPredicates, withPredicates, withQueryParam, withSort, withSort, withSort, withSort, withSortMulti
withFetchTotal, withLimit, withLimit, withOffset, withOffset
endpoint, expansionPaths, fetchTotal, limit, offset, predicates, sort
deserialize, toQuery
canDeserialize, httpRequestIntent
plusExpansionPaths, withExpansionPaths
plusExpansionPaths, plusExpansionPaths, plusExpansionPaths, plusExpansionPaths, withExpansionPaths, withExpansionPaths, withExpansionPaths, withExpansionPaths
static com.fasterxml.jackson.core.type.TypeReference<PagedQueryResult<Message>> resultTypeReference()
static MessageQuery of()
default <T extends Message> Query<T> forMessageType(MessageDerivateHint<T> hint)
withOrderAndReturnInfo(client(), ((order, returnInfo) -> {
final Query<ReturnInfoAddedMessage> query =
MessageQuery.of()
.withPredicates(m -> m.resource().is(order))
.withSort(m -> m.createdAt().sort().desc())
.withExpansionPaths(m -> m.resource())
.withLimit(1L)
.forMessageType(ReturnInfoAddedMessage.MESSAGE_HINT);
assertEventually(() -> {
final PagedQueryResult<ReturnInfoAddedMessage> pagedQueryResult = client().executeBlocking(query);
final Optional<ReturnInfoAddedMessage> message = pagedQueryResult.head();
assertThat(message).isPresent();
assertThat(message.get().getReturnInfo()).isEqualTo(returnInfo);
assertThat(message.get().getResource().getObj()).isNotNull();
assertThat(message.get().getResource().getId()).isEqualTo(order.getId());
});
return order;
}));
See the test code.
T
- the type of a single message that should be the outcome of a query.hint
- a container containing the message type and type references. You can find it as static field on the message you want, e.g., DeliveryAddedMessage.MESSAGE_HINT
.default Query<Message> forMessageTypes(List<MessageDerivateHint<? extends Message>> messageHints)
ProductFixtures.withUpdateableProduct(client(), product -> {
//create some messages apart from ProductCreatedMessage
final Product publishedProduct = client().executeBlocking(ProductUpdateCommand.of(product, Publish.of()));
final Product unpublishedProduct = client().executeBlocking(ProductUpdateCommand.of(publishedProduct, Unpublish.of()));
//these are the classes which are expected as message
final List<MessageDerivateHint<? extends Message>> messageHints =
asList(ProductCreatedMessage.MESSAGE_HINT,
ProductPublishedMessage.MESSAGE_HINT,
//as fallback for other product messages
SimpleProductMessage.MESSAGE_HINT);
final Query<Message> query = MessageQuery.of()
.withPredicates(m -> m.resource().is(product))
.withSort(m -> m.type().sort().asc())
.withExpansionPaths(m -> m.resource())
.forMessageTypes(messageHints);
assertEventually(Duration.ofSeconds(45), Duration.ofMillis(300), () -> {
final List<Message> messages = client().executeBlocking(query).getResults();
assertThat(messages).hasSize(3);
assertThat(messages.get(0)).isInstanceOf(ProductCreatedMessage.class);
assertThat(messages.get(1)).isInstanceOf(ProductPublishedMessage.class);
assertThat(messages.get(2)).isInstanceOf(SimpleProductMessage.class);
//use some kind of pattern matching
messages.stream()
.forEachOrdered(message -> {
if (message instanceof ProductCreatedMessage) {
final ProductCreatedMessage m = (ProductCreatedMessage) message;
assertThat(m.getResource()).isEqualTo(product.toReference());
} else if (message instanceof ProductPublishedMessage) {
final ProductPublishedMessage m = (ProductPublishedMessage) message;
assertThat(m.getResource()).isEqualTo(product.toReference());
} else if (message instanceof SimpleProductMessage) {
final SimpleProductMessage m = (SimpleProductMessage) message;
assertThat(m.getResource()).isEqualTo(product.toReference());
assertThat(m.getType()).isEqualTo("ProductUnpublished");
} else {
throw new RuntimeException("unexpected type of " + message);
}
});
});
return unpublishedProduct;
});
See the test code.
messageHints
- internal containers which register the known messages