Hello Team,
I am new to this Axon framework and facing below issue.
In Saga I am unable to publish command and events via RabbitMQ when we call CommandGateway.send(new CreateInvoiceCommand(orderCreatedEvent.orderId)). However if publish directly from the RabbitMQ the messages are consumed in the application. So it seems rabbitmq is not binded with axon framework.
- Need to use rabbitmq as a message broker instead of AXON server…
- And also Need to use rabbitmq as a message broker along with AXON server.
Please let me know the feasibility and help me to bind axon framework with rabbitmq.
I have shared below code for your reference.
server:
port: 8080
spring:
application:
name: order-service
datasource:
driver-class-name: org.postgresql.Driver
username: postgres
password: admin
url: jdbc:postgresql://${DATABASE_HOST}:5432/saga-order-service
jpa:
properties:
'[hibernate.default_schema]': public
show-sql: true
generate-ddl: true
hibernate:
ddl-auto: update
database: postgresql
rabbitmq:
host: ${RABBITMQ_HOST:localhost}
username: ${RABBITMQ_USER:retailadmin}
password: ${RABBITMQ_PASSWORD:retailadmin}
axon:
axonserver:
enabled: true
amqp:
exchange: mysagaexchange
transaction-mode: transactional
eventhandling:
processors:
amqpEvents:
source: myQueueMessageSource
mode: subscribing
logging:
level:
org.axonframework: DEBUG
@Configuration
public class OrderServiceConfiguration {
@Bean
public SpringAMQPMessageSource myQueueMessageSource(AMQPMessageConverter messageConverter) {
return new SpringAMQPMessageSource(messageConverter) {
@RabbitListener(queues = "mysagaqueue")
@Override
public void onMessage(Message message, Channel channel) {
System.out.println("!!!! order service queue:" + message);
super.onMessage(message, channel);
}
};
}
@Bean
public AMQPMessageConverter amqpMessageConverter(Serializer serializer) {
return DefaultAMQPMessageConverter.builder().serializer(serializer).build();
}
}
@Saga
public class OrderManagementSaga {
// TODO if we remove transient then it is not working only CreateInvoiceCommand
// is keep on creating
@Inject
private transient CommandGateway commandGateway;
@StartSaga
@SagaEventHandler(associationProperty = "orderId")
public void handle(OrderCreatedEvent orderCreatedEvent) {
System.out.println("Saga invoked and order id:" + orderCreatedEvent.orderId + " and order status:"
+ orderCreatedEvent.orderStatus);
// send the command to create invoice without the paymentId
commandGateway.send(new CreateInvoiceCommand(orderCreatedEvent.orderId));
}
@SagaEventHandler(associationProperty = "orderId")
public void handle(InvoiceCreatedEvent invoiceCreatedEvent) {
// send the create shipping command without the shippingId
commandGateway.send(new CreateShippingCommand(invoiceCreatedEvent.orderId, invoiceCreatedEvent.paymentId));
}
@SagaEventHandler(associationProperty = "orderId")
public void handle(OrderShippedEvent orderShippedEvent) {
System.out.println("Saga continued for OrderShippedEvent.");
commandGateway
.send(new UpdateOrderStatusCommand(orderShippedEvent.orderId, String.valueOf(OrderStatus.SHIPPED)));
}
@SagaEventHandler(associationProperty = "orderId")
public void handle(OrderUpdatedEvent orderUpdatedEvent, OrderCommandService orderCommandService) {
OrderStatus status = orderCommandService.updateOrder(orderUpdatedEvent.orderId, orderUpdatedEvent.orderStatus);
System.out.println("Saga ends for OrderUpdatedEvent:" + status.name());
SagaLifecycle.end();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.progressivecoder.ordermanagement</groupId>
<artifactId>order-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>order-service</name>
<description>Order Service for E-Commerce Store</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- DB dependency Starts -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<!-- DB dependency Ends -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- RabbitMQ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.axonframework.extensions.amqp</groupId>
<artifactId>axon-amqp-spring-boot-starter</artifactId>
</dependency>
<!-- Axon -->
<dependency>
<groupId>org.axonframework</groupId>
<artifactId>axon-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>com.progressivecoder.saga-pattern</groupId>
<artifactId>core-apis</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.axonframework</groupId>
<artifactId>axon-bom</artifactId>
<version>4.8.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>