Skip to content

Commit

Permalink
Allow configuration of the LockFactory through a bean name
Browse files Browse the repository at this point in the history
Add a lockFactory() field to the @aggregate annotation that references a
 LockFactory bean name. Adjust the SpringAxonAutoConfigurer accordingly
 to attach a LockFactory bean with that name with the
 AggregateConfigurer

#1490
  • Loading branch information
smcvb committed Oct 29, 2021
1 parent 3ca8b90 commit 99e77e9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,13 @@ private <A> void registerAggregateBeanDefinitions(Configurer configurer, BeanDef
aggregateConfigurer.configureCache(c -> beanFactory.getBean(cacheBeanName, Cache.class));
}

String lockFactoryBeanName = aggregateAnnotation.lockFactory();
if (nonEmptyBeanName(lockFactoryBeanName)) {
aggregateConfigurer.configureLockFactory(
c -> beanFactory.getBean(lockFactoryBeanName, LockFactory.class)
);
}

if (AnnotationUtils.isAnnotationPresent(aggregateType, "javax.persistence.Entity")) {
aggregateConfigurer.configureRepository(
c -> GenericJpaRepository.builder(aggregateType)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2020. Axon Framework
* Copyright (c) 2010-2021. Axon Framework
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -90,4 +90,16 @@
* created, unless explicitly configured on the referenced repository.
*/
String cache() default "";

/**
* Sets the name of the bean providing the {@link org.axonframework.common.lock.LockFactory}. If none is provided,
* the {@link org.axonframework.modelling.command.Repository} implementation's default is used, unless explicitly
* configured on the referenced repository.
* <p>
* Note that the use of {@link #repository()}, or adding a {@link org.axonframework.modelling.command.Repository}
* bean to the Spring context with the default naming scheme overrides this setting, as a Repository explicitly defines
* the snapshot trigger definition. The default name corresponds to {@code "[aggregate-name]Repository"}, thus a
* {@code Trade} Aggregate would by default create/look for a bean named {@code "tradeRepository"}.
*/
String lockFactory() default "";
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2020. Axon Framework
* Copyright (c) 2010-2021. Axon Framework
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,8 @@
import org.axonframework.commandhandling.SimpleCommandBus;
import org.axonframework.commandhandling.callbacks.FutureCallback;
import org.axonframework.common.caching.Cache;
import org.axonframework.common.lock.LockFactory;
import org.axonframework.common.lock.PessimisticLockFactory;
import org.axonframework.config.EventProcessingConfiguration;
import org.axonframework.config.EventProcessingConfigurer;
import org.axonframework.config.EventProcessingModule;
Expand Down Expand Up @@ -180,6 +182,10 @@ public class SpringAxonAutoConfigurerTest {
@Autowired
private TagsConfiguration tagsConfiguration;

@Autowired
@Qualifier("myLockFactory")
private LockFactory myLockFactory;

@Test
void contextWiresMainComponents() {
assertNotNull(axonConfig);
Expand Down Expand Up @@ -348,6 +354,19 @@ public void testAggregateCaching() {
);
}

@Test
void testAggregateLockFactory() {
String expectedAggregateId = "someIdentifier";

FutureCallback<Object, Object> commandCallback = new FutureCallback<>();
commandBus.dispatch(asCommandMessage(
new Context.CreateMyCachedAggregateCommand(expectedAggregateId)), commandCallback
);
commandCallback.getResult();

verify(myLockFactory).obtainLock(expectedAggregateId);
}

@AnnotationDriven
@Import({SpringAxonAutoConfigurer.ImportSelector.class, AnnotationDrivenRegistrar.class})
@Scope
Expand Down Expand Up @@ -423,6 +442,12 @@ public Cache myCache() {
return mock(Cache.class);
}

@Bean
@Qualifier("myLockFactory")
public LockFactory myLockFactory() {
return spy(PessimisticLockFactory.usingDefaults());
}

@Aggregate(type = "MyCustomAggregateType", filterEventsByType = true)
public static class MyAggregate {

Expand Down Expand Up @@ -521,7 +546,7 @@ public MyCachedAggregateCreatedEvent(String id) {
}
}

@Aggregate(cache = "myCache")
@Aggregate(cache = "myCache", lockFactory = "myLockFactory")
public static class MyCachedAggregate {

@AggregateIdentifier
Expand Down

0 comments on commit 99e77e9

Please sign in to comment.