首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有Quarkus和Jenetics的ContextNotActiveException

具有Quarkus和Jenetics的ContextNotActiveException
EN

Stack Overflow用户
提问于 2021-03-18 00:20:42
回答 1查看 119关注 0票数 0

第一次使用Quarkus,这可能是一个新手问题,但我不知道如何解决。我正在尝试设置一个端点,它应该运行一个遗传算法(用Jenetics制作)并返回结果。这是终结点定义:

代码语言:javascript
复制
@Path("/items")
public class ItemResource {

    @Inject
    ItemService service;

    @GET
    public List<Item> getItems() {
        return service.getItems();
    }

}

终结点要求执行以下服务类:

代码语言:javascript
复制
@ApplicationScoped
public class ItemService {

    @Inject
    ItemMapper mapper;

    @Transactional
    public List<Item> getItems() {
        int numOfItems = Math.toIntExact(Item.count());
        IntegerChromosome chromosome = IntegerChromosome.of(0, numOfItems - 1, 14);
        Factory<Genotype<IntegerGene>> factory = Genotype.of(chromosome);
        Engine<IntegerGene, Double> engine = Engine
                .builder(this::fitnessFunction, factory)
                .build();
        Genotype<IntegerGene> result = engine.stream()
                .limit(100)
                .collect(EvolutionResult.toBestGenotype());
        return mapper.toItems(result);
    }

}

最后是mapper类:

代码语言:javascript
复制
@ApplicationScoped
public class ItemMapper {

    public List<Item> toItems(Genotype<IntegerGene> genotype) {
        List<Item> items = Item.listAll();
        return genotype.chromosome().stream()
                .map(IntegerGene::intValue)
                .map(items::get)
                .collect(Collectors.toList());
    }

}

当我运行上面的代码时,我得到了以下异常:

Error handling 0d80baf3-12da-49ec-b8d0-e48472c801c9-1, org.jboss.resteasy.spi.UnhandledException: java.util.concurrent.CancellationException: javax.enterprise.context.ContextNotActiveException

代码可以在标准Java应用程序中完美运行,但不能在web服务中运行。有什么想法吗?

Here you can find the stack trace.

EN

回答 1

Stack Overflow用户

发布于 2021-03-19 03:58:26

您可以尝试设置不同的执行服务。

代码语言:javascript
复制
final Executor executor = Executors.newFixedThreadPool(10);
final Engine<EnumGene<WayPoint>, Double> engine = Engine.builder(...)
    .executor(executor)
    .build();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66677103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档