I am using axonframework 2.3.1 , for unit testing the application there is a Aggregate class which contain some event handlers. now i want that before inovking the commandhandler method contained in the Aggregate class i want to apply aop tracing @Before and @After those handler methods.
I am using FixtureConfiguration interface and apply newGivenWhenThenFixture to the aggregate class,As the wiring of the axon configured classes is done by axon framework.
I have configured the aop configuration in another xml file and i load that xml file before running the test cases.How can i integrate the aop tracing with the axon wired aggregate class.
Thanks
I used this example at http://www.axonframework.org/axon-2-quickstart-guide/#step1 in this example i want that i should be able to log before/after trace messages for class ToDoEventHandler
every method called.
Below is similar code where i have written some aggregates and aspect to configure. I have one aggregate class
public class ToDoItem extends AbstractAnnotatedAggregateRoot{
@AggregateIdentifier
private String id;
@CommandHandler
public ToDoItem(CreateToDoItemCommand command) {
apply(new ToDoItemCreatedEvent(command.getToDoId(), command.getDescription()));
}
@CommandHandler
public void markCompleted(MarkCompletedCommand command){
apply(new ToDoItemCompletedEvent(id));
}
public ToDoItem(){
}
@EventHandler
public void on(ToDoItemCreatedEvent event){
this.id=event.getTodoid();
}
}
and one EventHandler class
public class ToDoEventHandler {
@EventHandler
public void handle(ToDoItemCreatedEvent event) {
System.out.println("We are starting a task: "
+ event.getDescription() + " (" + event.getTodoid() + ")");
}
@EventHandler
public void handle(ToDoItemCompletedEvent event) {
System.out.println("We've completed a task: " + event.getTodoid());
}
}
the configuration file spring-axon as follows
<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-2.0.xsd">
<axon:command-bus id="commandBus" />
<axon:event-bus id="eventBus" />
<axon:event-sourcing-repository id="toDoRepository"
aggregate-type="com.my.axon.ToDoItem" />
<axon:aggregate-command-handler id="toDoItemHandler"
aggregate-type="com.my.axon.ToDoItem" repository="toDoRepository"
command-bus="commandBus" />
<axon:filesystem-event-store id="eventStore"
base-dir="events" />
<bean
class="org.axonframework.commandhandling.gateway.CommandGatewayFactoryBean">
<property name="commandBus" ref="commandBus" />
</bean>
<axon:annotation-config />
<bean class="com.my.axon.ToDoEventHandler" />
<bean class="com.my.axon.AopConfigurator"></bean>
now i want that before/after ToDoEventHandler class every method called i should be able to log before and after aspects so i created an aspect and configured it.
/**
* This class is used to configure the AOP related beans
* instead of doing the entries the beans are configured here and the
* same effect is achieved using @EnableAspectJAutoProxy annotation.
* @author anand.kadhi
*
*/
@Configuration
@EnableAspectJAutoProxy
public class AopConfigurator {
@Bean
public AspectRunner aspectOperation()
{
return new AspectRunner();
}
}
and aspect
@Aspect
public class AspectRunner {
/**
* This pointcut will call respective before and after method execution
* points
*/
@Pointcut("execution(* com.my.axon.ToDoEventHandler.*(..))")
public void logging(){};
@Before("logging()")
public void entering(JoinPoint joinPoint)
{
System.out.println("After completing Class : "+joinPoint.getTarget().getClass().getName() +" and method : "+joinPoint.getSignature().getName());
}
@After("logging()")
public void exiting(JoinPoint joinPoint)
{
System.out.println("After completing Class : "+joinPoint.getTarget().getClass().getName() +" and method : "+joinPoint.getSignature().getName());
}
}
and there is a main class
public class ToDoItemRunner {
private CommandGateway commandGateway;
public ToDoItemRunner(CommandGateway commandGateway) {
this.commandGateway = commandGateway;
}
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-axon.xml");
ToDoItemRunner runner = new ToDoItemRunner(applicationContext.getBean(CommandGateway.class));
runner.run();
}
private void run() {
final String itemId = UUID.randomUUID().toString();
commandGateway.send(new CreateToDoItemCommand(itemId, "Need to do this"));
commandGateway.send(new MarkCompletedCommand(itemId));
}
}
I want that before/after ToDoEventHandler class every method called i should be able to log before and after aspects .
Thanks in advance.