-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Spring boot native image support (#609)
- Loading branch information
1 parent
46f3caf
commit 8462888
Showing
25 changed files
with
2,815 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...main/java/ai/timefold/solver/spring/boot/autoconfigure/TimefoldSolverAotContribution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package ai.timefold.solver.spring.boot.autoconfigure; | ||
|
||
import java.util.Map; | ||
|
||
import ai.timefold.solver.core.config.solver.SolverConfig; | ||
|
||
import org.springframework.aot.generate.GenerationContext; | ||
import org.springframework.aot.hint.MemberCategory; | ||
import org.springframework.aot.hint.ReflectionHints; | ||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution; | ||
import org.springframework.beans.factory.aot.BeanFactoryInitializationCode; | ||
|
||
public class TimefoldSolverAotContribution implements BeanFactoryInitializationAotContribution { | ||
private final Map<String, SolverConfig> solverConfigMap; | ||
|
||
public TimefoldSolverAotContribution(Map<String, SolverConfig> solverConfigMap) { | ||
this.solverConfigMap = solverConfigMap; | ||
} | ||
|
||
/** | ||
* Register a type for reflection, allowing introspection | ||
* of its members at runtime in a native build. | ||
*/ | ||
private static void registerType(ReflectionHints reflectionHints, Class<?> type) { | ||
reflectionHints.registerType(type, | ||
MemberCategory.INTROSPECT_PUBLIC_METHODS, | ||
MemberCategory.INTROSPECT_DECLARED_METHODS, | ||
MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS, | ||
MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS, | ||
MemberCategory.PUBLIC_FIELDS, | ||
MemberCategory.DECLARED_FIELDS, | ||
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, | ||
MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, | ||
MemberCategory.INVOKE_DECLARED_METHODS, | ||
MemberCategory.INVOKE_PUBLIC_METHODS); | ||
} | ||
|
||
@Override | ||
public void applyTo(GenerationContext generationContext, BeanFactoryInitializationCode beanFactoryInitializationCode) { | ||
ReflectionHints reflectionHints = generationContext.getRuntimeHints().reflection(); | ||
for (SolverConfig solverConfig : solverConfigMap.values()) { | ||
solverConfig.visitReferencedClasses(type -> { | ||
if (type != null) { | ||
registerType(reflectionHints, type); | ||
} | ||
}); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
.../src/main/java/ai/timefold/solver/spring/boot/autoconfigure/TimefoldSolverAotFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package ai.timefold.solver.spring.boot.autoconfigure; | ||
|
||
import java.io.StringReader; | ||
|
||
import ai.timefold.solver.core.api.solver.SolverFactory; | ||
import ai.timefold.solver.core.api.solver.SolverManager; | ||
import ai.timefold.solver.core.config.solver.SolverConfig; | ||
import ai.timefold.solver.core.config.solver.SolverManagerConfig; | ||
import ai.timefold.solver.core.impl.io.jaxb.SolverConfigIO; | ||
import ai.timefold.solver.spring.boot.autoconfigure.config.SolverManagerProperties; | ||
import ai.timefold.solver.spring.boot.autoconfigure.config.TimefoldProperties; | ||
|
||
import org.springframework.boot.context.properties.bind.BindResult; | ||
import org.springframework.boot.context.properties.bind.Binder; | ||
import org.springframework.context.EnvironmentAware; | ||
import org.springframework.core.env.Environment; | ||
|
||
public class TimefoldSolverAotFactory implements EnvironmentAware { | ||
private TimefoldProperties timefoldProperties; | ||
|
||
@Override | ||
public void setEnvironment(Environment environment) { | ||
// We need the environment to set run time properties of SolverFactory and SolverManager | ||
BindResult<TimefoldProperties> result = Binder.get(environment).bind("timefold", TimefoldProperties.class); | ||
this.timefoldProperties = result.orElseGet(TimefoldProperties::new); | ||
} | ||
|
||
public <Solution_, ProblemId_> SolverManager<Solution_, ProblemId_> solverManagerSupplier(String solverConfigXml) { | ||
SolverFactory<Solution_> solverFactory = SolverFactory.create(solverConfigSupplier(solverConfigXml)); | ||
SolverManagerConfig solverManagerConfig = new SolverManagerConfig(); | ||
SolverManagerProperties solverManagerProperties = timefoldProperties.getSolverManager(); | ||
if (solverManagerProperties != null && solverManagerProperties.getParallelSolverCount() != null) { | ||
solverManagerConfig.setParallelSolverCount(solverManagerProperties.getParallelSolverCount()); | ||
} | ||
return SolverManager.create(solverFactory, solverManagerConfig); | ||
} | ||
|
||
public SolverConfig solverConfigSupplier(String solverConfigXml) { | ||
SolverConfigIO solverConfigIO = new SolverConfigIO(); | ||
return solverConfigIO.read(new StringReader(solverConfigXml)); | ||
} | ||
} |
Oops, something went wrong.