3

I'm a newbie and I try to start a Spring application linked to Optaplanner which will solve and place exams on a timetable. I fixed the various issues with missing jars and I started the app on main. However, it gives error:

Exception in thread "main" java.lang.ClassCastException: class org.springframework.boot.context.event.ApplicationStartingEvent cannot be cast to class org.springframework.boot.context.event.ApplicationPreparedEvent (org.springframework.boot.context.event.ApplicationStartingEvent and org.springframework.boot.context.event.ApplicationPreparedEvent are in unnamed module of loader 'app')

Here is the TimeTable class :

package models;

import java.util.List;
import org.optaplanner.core.api.domain.solution.PlanningEntityCollectionProperty;
import org.optaplanner.core.api.domain.solution.PlanningScore;
import org.optaplanner.core.api.domain.solution.PlanningSolution;
import org.optaplanner.core.api.domain.solution.ProblemFactCollectionProperty;
import org.optaplanner.core.api.domain.valuerange.ValueRangeProvider;
import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore;

@PlanningSolution
public class Timetable {

    @ValueRangeProvider(id = "PeriodeRange")
    @ProblemFactCollectionProperty
    public List<Periode> periodeList;

    @ValueRangeProvider(id = "SalleRange")
    @ProblemFactCollectionProperty
    public List<Salle> salleList;

    @PlanningEntityCollectionProperty
    public List<Examen> examenList;

    @PlanningScore
    public HardSoftScore score;
    
    public void TimeTable(List<Periode> periodeList, List<Salle> roomList,
            List<Examen> examenList) {
        this.periodeList = periodeList;
        this.salleList = roomList;
        this.examenList = examenList;
    }

    public List<Periode> getperiodeList() {
        return periodeList;
    }

    public List<Salle> getsalleList() {
        return salleList;
    }

    public List<Examen> getexamenList() {
        return examenList;
    }

    public HardSoftScore getScore() {
        return score;
    }
}

And the class that defines the solver :

package models;
import java.util.UUID;


import java.util.concurrent.ExecutionException;

import modeles.Timetable;
import org.optaplanner.core.api.solver.SolverJob;
import org.optaplanner.core.api.solver.SolverManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/Timetable")
public class TimetableController {
    @Autowired
    public SolverManager<Timetable, UUID> solverManager;

    @PostMapping("/solve")
    public Timetable solve(@RequestBody Timetable problem) {
        UUID problemId = UUID.randomUUID();
        // Submit the problem to start solving
        SolverJob<Timetable, UUID> solverJob = solverManager.solve(problemId, problem);
        Timetable solution;
        try {
            // Wait until the solving ends
            solution = solverJob.getFinalBestSolution();
        } catch (InterruptedException | ExecutionException e) {
            throw new IllegalStateException("Solving failed.", e);
        }
        return solution;
    }
}

And here is the main :

package models;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TimeTableSpringBootApp {

    public static void main(String[] args) {
        System.setProperty("spring.devtools.restart.enabled", "false");
        SpringApplication.run(TimeTableSpringBootApp.class, args);
    }
}

Any answer would be gladly appreciated. Thank you in advance.

João Dias
  • 16,277
  • 6
  • 33
  • 45
  • [The canonical spring optaplanner school timetabling example](https://github.com/kiegroup/optaplanner-quickstarts/tree/stable/spring-boot-school-timetabling) works for me. If that works for you, what changes do you have in your variant that might cause this issue? – Geoffrey De Smet Jan 06 '21 at 08:37
  • 1
    Stack trace of the exception might be helpful. – yurloc Jan 06 '21 at 11:43

1 Answers1

0

I suspect your project's dependency tree somehow has that spring class ApplicationStartingEvent twice in it's classpath (coming from different jars), which causes the class cast exception.

Try running mvn dependency:tree on your project and the optaplanner spring boot school timetabling quickstart. I suspect you're mixing spring versions in your dependency tree.

Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120