@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DeleteProductCommand {
@TargetAggregateIdentifier
private String productId;
}
In the controller I have:
@DeleteMapping()
public String deleteProduct(@RequestBody String productId1) {
//create the command
DeleteProductCommand deleteProductCommand = DeleteProductCommand
.builder()
.productId(productId1)
.build();
String result = commandGateway.sendAndWait(deleteProductCommand);
return result;
}
and my event is :
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ProductDeletedEvent {
@AggregateIdentifier
private String productId;
}
and am handling it here :
@EventHandler
public void on(ProductDeletedEvent event) {
productRepository.deleteById(event.getProductId());
}
Then am getting an error :
org.axonframework.axonserver.connector.command.AxonServerRemoteCommandHandlingException: An exception was thrown by the remote message handling component: OUT_OF_RANGE: [AXONIQ-2000] Invalid sequence number 0 for aggregate “ef734cce-fb22-4bb1-a68a-8c8c6c7924e9”, expected 1
the request am sending in postman is →
DELETE → http://localhost:8081/products and body has a String “ef734cce-fb22-4bb1-a68a-8c8c6c7924js”
Can you also show the command handler? You can only delete an item, if it was created first. Your error message seems to suggest the previous message wasn’t event sources, but it was trying to create a new aggregate. This in turn probably means the command is handled in a constructor, and not in a CommandHandler annotated method.
@CommandHandler
public ProductAggregate(DeleteProductCommand deleteProductCommand) {
//business logic or validations confirm if the user exists
//create the events object and since the product command has
//similar data as what we need in the event , then we use beans
ProductDeletedEvent productDeletedEvent =
new ProductDeletedEvent();
BeanUtils.copyProperties(deleteProductCommand, productDeletedEvent);
//publish the events
AggregateLifecycle.apply(productDeletedEvent);
}
I also have a problem in my eventhandler where event.getProductId() gives an object of “productId”:“myId” and I wanted the string “myId” only to be passed to the method productRepo.deleteById(event.getProductId());
Is there anything maybe am missing there.