代码之家  ›  专栏  ›  技术社区  ›  Benjamin

没有为命令订阅处理程序。轴突3.3

  •  1
  • Benjamin  · 技术社区  · 6 年前

    我正在尝试通过教程创建基本的Axon/Spring应用程序,但遇到了奇怪的错误,比如:nohandlerForCommandException:没有为命令订阅任何处理程序。似乎Axon看不到@commandHandler注释。

    这里是我的档案:

    骨料

    @Aggregate
    @NoArgsConstructor
    public class Account {
    
        @AggregateIdentifier
        private UUID accountId;
        private Double balance;
    
        @CommandHandler
        public Account(CreateAccountCommand command) {
            apply(new AccountCreatedEvent(command.getAccountId(), command.getName()));
        }
    
        @EventSourcingHandler
        protected void on(AccountCreatedEvent event) {
            this.accountId = event.getAccountId();
            this.balance = 0.0;
        } 
    }
    

    事件

    @AllArgsConstructor
    @Getter
    public class AccountCreatedEvent {
    
        private UUID accountId;
        private String name;
    }
    

    命令

    @Getter
    public class CreateAccountCommand {
    
        @TargetAggregateIdentifier
        private UUID accountId;
        private String name;
    
        public CreateAccountCommand(UUID accountId, String name) {
            this.accountId = accountId;
            this.name = name;
        }
    }
    

    弹簧靴配置

    @SpringBootApplication
    public class App {
    
        @Configuration
        public static class TestConfiguration {
            @Bean
            public EventStorageEngine inMemoryEventStorageEngine() {
                return new InMemoryEventStorageEngine();
            }
        }
    
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    }
    

    这是我发送命令的方式

    @Component
    public class AppLoader {
    
        @Autowired
        private CommandGateway cmdGateway;
    
        @PostConstruct
        public void init() {
            cmdGateway.send(new CreateAccountCommand(UUID.randomUUID(), "test"));
        }
    }
    

    我的建筑

    buildscript {
        ext {
            springBootVersion = '2.0.5.RELEASE'
        }
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        }
    }
    
    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    
    group = 'io.yourpoint.nettnews'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = 1.8
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        testCompile group: 'org.axonframework', name: 'axon-test', version: '3.3.5'
        compile group: 'org.axonframework', name: 'axon-spring-boot-starter', version: '3.3.5'
        compile('org.springframework.boot:spring-boot-starter-webflux')
        compileOnly('org.projectlombok:lombok')
        testCompile('org.springframework.boot:spring-boot-starter-test')
        testCompile('io.projectreactor:reactor-test')
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Steven    6 年前

    很有可能 CommandGateway#send() 打电话 @PostConstruct 对轴突来说是太早了。

    目前,所有命令、事件和查询处理程序通常都会注册 之后 所有bean都已初始化。 搬家 commandgateway发送() 在REST端点后面应该有足够大的时间框架,以确保所有处理程序都已注册。

    所以简而言之,这是一个时间问题,为什么你得到 NoHandlerForCommandException .

    我们(在Axoniq)看到了在所有处理程序注册后立即发送某种应用程序事件的好处。不过,这是未来的事情。

    希望这对你有帮助@Benjamin!