当前位置: 首页 > 知识库问答 >
问题:

使用AxonIq在Quarkus中无法注入CommandGateway

谷梁卓
2023-03-14

我在Quarkus有一个微服务,它使用AxonIq框架实现CQRS/事件来源。我已经用Spring Boot做了,一切都没问题。我想在Quarkus中迁移它,但我在maven编译过程中出错,可能是因为Ioc。当CDI尝试创建服务时,我认为他可以注入Axon CommandGateway和QueryGateway。

[ERROR] Failed to execute goal io.quarkus.platform:quarkus-maven-plugin:2.7.1.Final:build (default) on project domain: Failed to build quarkus application: io.quarkus.builder.BuildExce
ption: Build failure: Build failed due to errors
[ERROR]         [error]: Build step io.quarkus.arc.deployment.ArcProcessor#validate threw an exception: javax.enterprise.inject.spi.DeploymentException: Found 2 deployment problems:   
[ERROR] [1] Unsatisfied dependency for type org.axonframework.Commandhandling.CommandGateway and qualifiers [@Default]
[ERROR]         - java member: com.omb.commands.MyAggregateCommandService().commandGateway
[ERROR]         - declared on CLASS bean [types=[com.omb.commands.MyAggregateCommandService, java.lang.Object], qualifiers=[@Default, @Any], target=com.omb.commands.MyAggregateCommandService]

配置

package com.omb..configuration;

import com.omb..MyAggregate;
import com.omb..commands.MyAggregateCommandService;
import com.omb..mongo.MongoMyAggregateProjector;
import com.omb..queries.MyAggregateQueryService;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import org.axonframework.config.Configurer;
import org.axonframework.config.DefaultConfigurer;
import org.axonframework.eventsourcing.eventstore.EventStorageEngine;
import org.axonframework.extensions.mongo.DefaultMongoTemplate;
import org.axonframework.extensions.mongo.eventsourcing.tokenstore.MongoTokenStore;
import org.axonframework.serialization.xml.XStreamSerializer;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;

@ApplicationScoped
public class AxonConfiguration {

    @Produces
    public org.axonframework.config.Configuration getAxonConfiguration(MongoMyAggregateProjector MyAggregateProjector, MongoDatabase database, EventStorageEngine eventStorageEngine) {
        XStreamSerializer serializer = XStreamSerializer.defaultSerializer();

        Configurer configurer = DefaultConfigurer
                .defaultConfiguration()
                .configureAggregate(MyAggregate.class)
                .eventProcessing(conf -> conf
                        .registerTokenStore(config -> MongoTokenStore.builder()
                                .mongoTemplate(
                                        DefaultMongoTemplate.builder()
                                                // optionally choose collection names here
                                                .mongoDatabase(database)
                                                .build())
                                .serializer(serializer)
                                .build()))
                .registerEventHandler(conf -> MyAggregateProjector)
                .registerQueryHandler(conf -> MyAggregateProjector)
                .configureEmbeddedEventStore(conf -> eventStorageEngine);

        return configurer.start();
    }

    @Produces
    public MongoDatabase mongoDatabase(MongoClient client) {
        return client.getDatabase("MyAggregate");
    }

    @Produces
    @ApplicationScoped
    public MyAggregateQueryService queryService(org.axonframework.config.Configuration configuration) {
        return new MyAggregateQueryService(configuration.queryGateway());
    }

    @Produces
    @ApplicationScoped
    public MyAggregateCommandService commandService(org.axonframework.config.Configuration configuration) {
        return new MyAggregateCommandService(configuration.commandGateway());
    }
}

服务:

package com.omb..commands;

import com.omb..models.MyAggregateDTO;
import org.axonframework.commandhandling.gateway.CommandGateway;

import javax.enterprise.context.ApplicationScoped;
import java.util.concurrent.CompletableFuture;

@ApplicationScoped
public class MyAggregateCommandService {

    CommandGateway commandGateway;

    public MyAggregateCommandService(CommandGateway commandGateway) {
        this.commandGateway = commandGateway;
    }

    public CompletableFuture<String> createMyAggregate(final MyAggregateDTO MyAggregate) {
        return commandGateway.send(new CreateMyAggregateCommand(MyAggregate.id(), MyAggregate.name()));
    }

    public CompletableFuture<MyAggregateDTO> updateMyAggregate(final String MyAggregateId, final MyAggregateDTO MyAggregate) {
        if(MyAggregate.id().equals(MyAggregateId)) {
            return commandGateway.send(new UpdateMyAggregateCommand(MyAggregate.id(), MyAggregate.name()));
        } else {
            throw new IllegalArgumentException("Identifiers are not the same, does not update");
        }
    }
}

控制器:

package com.omb;

import com.omb..commands.MyAggregateCommandService;
import com.omb..models.MyAggregateDTO;

import javax.validation.Valid;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

@Path("/commands/MyAggregate")
public class MyAggregateCommandController {

    private MyAggregateCommandService MyAggregateCommandService;

    public MyAggregateCommandController(MyAggregateCommandService MyAggregateCommandService) {
        this.MyAggregateCommandService = MyAggregateCommandService;
    }

    @POST
    @Path("/create")
    public CompletableFuture<String> createMyAggregate(@Valid MyAggregateCreateInput MyAggregate) {
        MyAggregateDTO MyAggregateDTO = new MyAggregateDTO(UUID.randomUUID().toString(), MyAggregate.name());
        return MyAggregateCommandService.createMyAggregate(MyAggregateDTO);
    }

    @PUT
    @Path("/{MyAggregateId}")
    public CompletableFuture<MyAggregateDTO> updateMyAggregate(@PathParam("MyAggregateId") String MyAggregateId, MyAggregateDTO MyAggregate) {
        MyAggregateDTO MyAggregateDTO = new MyAggregateDTO(MyAggregate.id(), MyAggregate.name());
        return MyAggregateCommandService.updateMyAggregate(MyAggregateId,MyAggregateDTO);
    }

/*    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<String> handle(Exception e) {
        return ResponseEntity.badRequest().body(e.getMessage());
    }*/
}

屬地

<dependency>
            <groupId>org.axonframework.extensions.mongo</groupId>
            <artifactId>axon-mongo</artifactId>
            <version>4.5</version>
        </dependency>
        <dependency>
            <groupId>org.axonframework</groupId>
            <artifactId>axon-test</artifactId>
            <scope>test</scope>
            <version>4.5.6</version>
        </dependency>
        <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-mongodb-panache</artifactId>
    </dependency>

    <dependency>
        <groupId>org.axonframework</groupId>
        <artifactId>axon-configuration</artifactId>
    </dependency>
    <dependency>
        <groupId>org.axonframework</groupId>
        <artifactId>axon-modelling</artifactId>
    </dependency>
    <dependency>
        <groupId>org.axonframework</groupId>
        <artifactId>axon-messaging</artifactId>
    </dependency>

        <!-- Quarkus -->
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-core</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-junit5</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-resteasy-reactive-jackson</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-hibernate-validator</artifactId>
        </dependency>

共有1个答案

呼延升
2023-03-14

我有同样的问题,其中一个原因可能是你的bean是由一个依赖项带来的,要修复它,你需要在这个依赖项的main/Resources/META-INF中添加一个空beans.xml,以便Quarkus发现留档所指示的bean

相关摘录:

bean存档由以下内容合成:

>

  • 应用程序类,

    包含 bean.xml 描述符(内容被忽略)的依赖项,

    包含Jandex索引的依赖项-META-INF/Jandex.idx,

    应用程序属性中Quarkus索引引用的依赖项,

    和Quarkus集成代码。

  •  类似资料:
    • 我想在Quarkus中注入bean一个静态编程语言字段。示例文件看起来像 构造函数参数注入工作正常,但字段仍处于未初始化状态。 控制台输出: 有可能在科特林的Quarkus中使用CDI场注入吗?如果是,需要为注射设置什么才能工作? 我是从uberjar运行应用程序,而不是从本地图像运行。 jar包含生成的类org.example.InjectDemo_Bean包含一个方法的分解版本,该方法不显示任

    • 我有一个有状态的会话作用域(CDI)EJB,它保存有关用户会话的信息。 现在,我想从一个 我从webapp直接访问EJB(使用注入)到我为JSF使用的bean中(它们也是CDI管理的bean)。 我面临的问题是,似乎与JSF bean处于不同的“会话范围”。创建了两个实例;一个从JSF实例化,另一个从HttpSessionListener实例化。 我尝试通过注入实例注入bean 这个bug报告建议

    • 这是模块类 这是我的viewmodel构造函数 当我运行应用程序时,我得到这个错误,我认为不要在模块中注入所需的应用程序上下文,任何帮助都会被赞赏 C:\XYZ\XYZ\AndroidStudioProjects\CarAndroid\app\build\generate\source\kapt\debug\com\sw\car\baseclasses\CarApp_HiltComponents.

    • 问题内容: 我的测试课: 我在行上得到一个空指针异常: 在精确的给出空指针异常 如何使Junit类中的beanObject字段自动装配成为可能,以便可以使用“ BeanClass”类中的方法? 从评论中复制: 用简单的术语来说.. beanClass是具有某些方法的接口..我用 注释标记了该beanClass。.banClass 是由具有方法实现的beanClassImpl类实现的。.我需要在我的

    • 使用quarkiverse/Quarkus cxf在Quarkus中使用SOAP Web服务尝试使用此示例实现客户端 无法使用以下扩展名导入

    • 我只是在用Quarkus做实验,我在ResourceLoader上遇到了一个问题。具体来说,当我尝试使用ResourceLoader inject时,maven的干净安装出错了 这是maven的输出: 您知道如何在Quarkus中使用ResourceLoader吗,或者是否有一种等效的方法来读取类路径资源中的文件? 这是我目前的情况: 我已经在project resources文件夹中找到了这些文