ContextNotActiveException with Quarkus and Jenetics

512 Views Asked by At

First time using Quarkus, it's probably a noob question but I have no idea how to resolve. I'm trying to set up an endpoint which should run a Genetic Algorithm (made with Jenetics) and return the result. This is the endpoint definition:

@Path("/items")
public class ItemResource {

    @Inject
    ItemService service;

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

}

The endpoint demands the execution to the service class below:

@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);
    }

}

and finally this is the mapper class:

@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());
    }

}

When I run the code above, I get the following exception:

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

The code runs flawlessly in a standard Java application, but not in a web service. Any idea?


Here you can find the stack trace.

1

There are 1 best solutions below

4
On

You can try to set a different execution service.

final Executor executor = Executors.newFixedThreadPool(10);
final Engine<EnumGene<WayPoint>, Double> engine = Engine.builder(...)
    .executor(executor)
    .build();