Home / Java / How to Use Axon Framework in Java to Develop Web applications

How to Use Axon Framework in Java to Develop Web applications

Technology: Aaxon is the Framework for Java language which is used develop the java applications using CQRS and Event Sourcing concepts. It provided annotation driven approach to send receive the message from message bus.

Aaxon Framework is the implementation of CQRS (Command Query Responsibility Segregation) and Event sourcing.

CQRS (Command Query Responsibility Segregation): It is a pattern which provides different model for write operation and also different model for read operation.

Generally we will use same model for write and read operations.

FrameWork One

After implementing CQRS pattern the implementation looks like:

FrameWork 2

Where we are using Query Model for read operation (for data retrieval) and Command Model for writing operations (for persisting into Data Store like create, update, delete operations).

Mostly Query Model and Command Model will run on different processes perhaps on different machines, means there will two separate services hosted on two different servers.

Eg: Suppose we have some HTML form, to read all records the request goes to Query Model Services, for creating, updating, deleting the record request goes to Command Model, it will send a notification to Query Model.

The Data-stores may be in-memory for both processes, and there should be communication medium like database which can communicate each other using this medium.

The two models might not be separate object models, it could be that the same objects have different interfaces for their command side and their query side, rather like views in relational databases. According to CQRS principals they must be two separate models.

Event Sourcing: As same suggest it is the source of events, and it is well fits with CQRS, Query Model and Command Model can be deployed as two different services which communicate using event sourcing.

Eager Read Derivations: if we want to read data from different sources then instead of going to main database it can directly read from Query Models.

Eg: suppose based on userid we want to know profile information and orders information that he did in the some e-commerce site.

Here user management will be one Query Model and Order management will be another Query Model, for getting information we can use query both Query Models instead of going to Main database.

Aaxon Framework is the implementation of CQRS and Event source, it has Command bus for processing the commands and Event-source bus for triggering the events for write operations, using BeanPostProcessors it will integrate with spring framework easily.

Advantages of using Aaxon framework:

  1. Scalability: Aaxon Framework decouples application into multiple services, coupling using asynchronous message bus, this enabled the application components spread across various physical servers.
  2. Agility: Aaxon Framework supports decoupling of application components, This ensures that changes in one component will not directly affect behavior in another component, it also make the adding new documents easy, after adding new components, we can couple to existing components using asynchronous message bus.
  3. Performance: Aaxon Framework decouples read and write operations, for most of the cases we need to fetch data, so we can optimize the Query Model using either in-memory data or using cache servers like Redis, Ehcache, mem-cache etc… Which will increase performance.
  4. Axon Framework provides authentication, authorization and auditing capabilities out of the box. Not only can unauthorized access be prevented, unauthorized use can be tracked.

Integrating Aaxon Framework with spring framework:

Spring Framework provides BeanPostProcessors for adding external frameworks with spring. Aaxon Framework provides BeanPostProcessors for scanning Aaxon related annotations like @CommandHandler and @EventHandler.

Using spring framework Spring`s Extensible schema based configuration Aaxon Framework is providing their own xsd file for easier configuration.

JSR-250 Support:

As Aaxon is extending the spring framework features, it will supports the JSR-250 annotations (@PostConstruct and @PreDestroy), Aaxon is using this features to build blocks, but by default spring will not support JSR-250 features, through configuration we can achieve this.

<context:annotation-config/>

 

Aaxon NameSpace:

Aaxon is providing their own namespace for configuring bean through which we can integrate with spring framework easily.

Additional namespace in spring beans declaration:

<beans xmlns=”http://www.springframework.org/schema/beans”

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

xmlns:axon=”http://www.axonframework.org/schema/core”

xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.axonframework.org/schema/core http://www.axonframework.org/schema/axon-core.xsd”>

Configuring Event Handlers:

Using @EventHandler we can make any method as Event handler, argument to this method will inform Aaxon what of type of events that it can handle.

 

Eg: @EventHandler

void on(TaskCreatedEvent event) {

TaskEntry task = new TaskEntry(event.getId(), event.getUsername(), event.getTitle(), false, false);

taskEntryRepository.save(task);

}

Here, we annotated method as @EventHandler means it will act as eventhandler for TaskCreatedEvent type.

Aaxon is providing AnnotationEventListenerBeanPostProcessor to discover all EventHandlers, for this to happen we need to create a bean of the type AnnotationEventListenerBeanPostProcessor, these annonated eventlisteners will automatically added eventBus.

 

Annotation-Style:

@Bean

public AnnotationEventListenerBeanPostProcessor annotationEventListenerBeanPostProcessor() {

AnnotationEventListenerBeanPostProcessor processor = new AnnotationEventListenerBeanPostProcessor();

processor.setEventBus(eventBus());

return processor;

}

@Bean

public EventBus eventBus() {

return new SimpleEventBus();

}

 

Xml bean declaration:

<bean class=”org.axonframework.eventhandling.annotation.AnnotationEventListenerBeanPostProcessor”>

<property name=”eventBus” ref=”eventBus”/>

</bean>

Using Aaxon Namespace:

<axon:event-bus id=”eventBus”/>

Where we are using eventBus bean, which is optional if the applicationContext having single eventBus, by default Aaxon Framework will create SimpleEventBus, if the applicationContext having more than one eventBus then we must need to bind the instance.

Configuring Command Handlers:

Using @CommandHandler we can make any method as Event handler, argument to this method will inform Aaxon what of type of events that it can handle.

Eg: @CommandHandler

void on(ModifyTaskTitleCommand command) {

assertNotCompleted();

apply(new TaskTitleModifiedEvent(command.getId(), command.getTitle()));

}

Here, we annonated method as @CommandHandler means it will act as commandhandler for ModifyTaskTitleCommand type.

 

Aaxon is providing AnnotationCommandHandlerBeanPostProcessor to discover all CommandHandlers, for this to happen we need to create a bean of the type AnnotationCommandHandlerBeanPostProcessor , these annonated commandlisteners will automatically added commandBus.

Annotation-Style:

@Bean

public AnnotationCommandHandlerBeanPostProcessor annotationCommandHandlerBeanPostProcessor() {

AnnotationCommandHandlerBeanPostProcessor processor = new AnnotationCommandHandlerBeanPostProcessor();

processor.setCommandBus(commandBus());

return processor;

}

@Bean

public CommandBus commandBus() {

SimpleCommandBus commandBus = new SimpleCommandBus();

commandBus.setHandlerInterceptors(Arrays.asList(new BeanValidationInterceptor()));

return commandBus;

}

Xml bean declaration:

<bean class=”org.axonframework.commandhandling.annotation.AnnotationCommandHandlerBeanPostProcessor”>

<property name=”commandBus” ref=”commandBus”/>

</bean>

Using Aaxon Namespace:

<axon:command-bus id=”commandBus”/>

 

Where we are using commandBus bean, which is optional if the applicationContext having single commandBus, by default Aaxon Framework will create SimpleCommandBus, if the applicationContext having more than one commandBus then we must need to bind the instance.

 

Configuring CommandBus, EventBus using Aaxon Namespace:

Aaxon namespace is providing annotation-config tag will do configure required beanpostprocessors.

If the applicationContext contains more than eventBus, commandBus we need to specify in annonation-config.

Eg: Default configuration:<axon:annotation-config />

Custom commandBus, eventBus configuration:

<axon:annotation-config event-bus=”eventBus” command-bus=”commandBus”/>

 

EventSourcingRepository: eventSourcingRepository will used to store events that we produced, Aaxon will provides various eventsouceRespository.

Eg: CachingEventSourcingRepository :used to cache the events based on event store.

GenericJpaRepository: used to store the events in database.

 

Event Store:

eventStore will decide where you want to store the events and which format you can store the format.

FileSystemEventStore :it used to store the events in fileSystem in given path.

JpaEventStore: it used to store the events in configured database.

 

Eg: @Bean

public EventSourcingRepository<Task> taskRepository() {

FileSystemEventStore eventStore = new FileSystemEventStore(new SimpleEventFileResolver(new File(“data/evenstore”)));

EventSourcingRepository<Task> repository = new EventSourcingRepository<Task>(Task.class, eventStore);

repository.setEventBus(eventBus());

return repository;

}

 

Configuration file for Spring integration will look like this:

 

@Configuration

public class AxonConfiguration {

 

@Bean

public AnnotationEventListenerBeanPostProcessor annotationEventListenerBeanPostProcessor() {

AnnotationEventListenerBeanPostProcessor processor = new AnnotationEventListenerBeanPostProcessor();

processor.setEventBus(eventBus());

return processor;

}

 

@Bean

public AnnotationCommandHandlerBeanPostProcessor annotationCommandHandlerBeanPostProcessor() {

AnnotationCommandHandlerBeanPostProcessor processor = new AnnotationCommandHandlerBeanPostProcessor();

processor.setCommandBus(commandBus());

return processor;

}

 

@Bean

public CommandBus commandBus() {

SimpleCommandBus commandBus = new SimpleCommandBus();

commandBus.setHandlerInterceptors(Arrays.asList(new BeanValidationInterceptor()));

return commandBus;

}

 

@Bean

public EventBus eventBus() {

return new SimpleEventBus();

}

 

@Bean

public CommandGatewayFactoryBean<CommandGateway> commandGatewayFactoryBean() {

CommandGatewayFactoryBean<CommandGateway> factory = new CommandGatewayFactoryBean<CommandGateway>();

factory.setCommandBus(commandBus());

return factory;

}

@Bean

public EventSourcingRepository<Task> taskRepository() {

FileSystemEventStore eventStore = new FileSystemEventStore(new SimpleEventFileResolver(new File (“data/evenstore”)));

EventSourcingRepository<Task> repository = new EventSourcingRepository<Task> (Task.class, eventStore);

repository.setEventBus(eventBus());

return repository;

}

 

@Bean

public AggregateAnnotationCommandHandler<Task> taskCommandHandler() {

AggregateAnnotationCommandHandler<Task> commandHandler = AggregateAnnotationCommandHandler.subscribe(Task.class, taskRepository(), commandBus());

return commandHandler;

}

}

You can download the sample project.

https://github.com/sravan4rmhyd/spring-boot-axon-sample.git

We can import the project as maven project in eclipse and run as spring boot application.

Conclusion: It provides asynchronous way to handle the command and events for more parallelization, and also using this framework we can decouple applications to independent modules, it provides integration with Spring Framework using BeanPostProcessors, and also it provides a not only separate namespace for configuring Aaxon framework, but also using annotations we can configure the Aaxon framework related beans.

Hope this blog is useful in getting idea on what is Aaxon Framework and what is command bus, event bus and how it integrate with Spring Framework in developing real time applications.

 

How to Use Axon Framework in Java to Develop Web applications
5 (100%) 1 vote

About

James Warner is a highly qualified digital marketing highly qualified digital marketing and entrepreneurship. I'm a contributing editor of NexSoftsys for many years in a various role including editor Technology, Health & Media editor and also working as a freelance. You can contact me on Facebook or Follow me on Twitter or add your circle Google+

Check Also

amazon db 1

How to use Amazon DynamoDB for Java Enterprise Development

You will learn basics of Amazon DynamoDB used by java enterprise development experts for projects. …

Leave a Reply