Spring 批处理 ScriptItemProcessor

Spring Batch ScriptItemProcessor

我想在我的批处理程序中使用网络抓取 python 脚本,但我在网络上找不到任何相关示例。因此,可以将 python 脚本与 ScriptItemProcessor?

一起使用

这是我用来测试的简单代码:

@Bean
public ScriptItemProcessor<Company,Company> scriptItemProcessor() {
    return new ScriptItemProcessorBuilder<Company,Company>().language("python").scriptSource("print('hello')").build();
}

更新 1

添加所需的 jar 后,异常发生了变化,所以现在可以识别它,但新的堆栈跟踪根本不是很描述性的。

    java.lang.IllegalStateException: Script engine with name 'python' failed to initialize
        at org.springframework.scripting.support.StandardScriptUtils.retrieveEngineByName(StandardScriptUtils.java:64) ~[spring-context-5.3.3.jar!/:5.3.3]
        at org.springframework.scripting.support.StandardScriptEvaluator.getScriptEngine(StandardScriptEvaluator.java:174) ~[spring-context-5.3.3.jar!/:5.3.3]
        at org.springframework.scripting.support.StandardScriptEvaluator.evaluate(StandardScriptEvaluator.java:143) ~[spring-context-5.3.3.jar!/:5.3.3]
        at org.springframework.batch.item.support.ScriptItemProcessor.process(ScriptItemProcessor.java:64) ~[spring-batch-infrastructure-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.core.step.item.SimpleChunkProcessor.doProcess(SimpleChunkProcessor.java:134) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.core.step.item.SimpleChunkProcessor.transform(SimpleChunkProcessor.java:319) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:210) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:77) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:407) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:331) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140) ~[spring-tx-5.3.3.jar!/:5.3.3]
        at org.springframework.batch.core.step.tasklet.TaskletStep.doInChunkContext(TaskletStep.java:273) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:82) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:375) ~[spring-batch-infrastructure-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215) ~[spring-batch-infrastructure-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:145) ~[spring-batch-infrastructure-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:258) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:208) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:68) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:68) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:137) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:320) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:147) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) ~[spring-core-5.3.3.jar!/:5.3.3]
        at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:140) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) ~[spring-aop-5.3.3.jar!/:5.3.3]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) ~[spring-aop-5.3.3.jar!/:5.3.3]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.3.3.jar!/:5.3.3]
        at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:128) ~[spring-batch-core-4.3.1.jar!/:4.3.1]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.3.jar!/:5.3.3]
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) ~[spring-aop-5.3.3.jar!/:5.3.3]
        at com.sun.proxy.$Proxy73.run(Unknown Source) ~[na:na]
        at org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner.execute(JobLauncherApplicationRunner.java:199) ~[spring-boot-autoconfigure-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner.executeLocalJobs(JobLauncherApplicationRunner.java:173) ~[spring-boot-autoconfigure-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner.launchJobFromProperties(JobLauncherApplicationRunner.java:160) ~[spring-boot-autoconfigure-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner.run(JobLauncherApplicationRunner.java:155) ~[spring-boot-autoconfigure-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner.run(JobLauncherApplicationRunner.java:150) ~[spring-boot-autoconfigure-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:785) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) ~[spring-boot-2.4.2.jar!/:2.4.2]
        at com.stockanalyzer.stockanalyzer.StockAnalyzerApplication.main(StockAnalyzerApplication.java:136) ~[classes!/:0.0.1-SNAPSHOT]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[stock-analyzer-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:107) ~[stock-analyzer-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[stock-analyzer-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) ~[stock-analyzer-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
Caused by: java.lang.NullPointerException: null
        at org.python.core.PySystemState.<init>(PySystemState.java:214) ~[jython-slim-2.7.2.jar!/:na]
        at org.python.util.PythonInterpreter.threadLocalStateInterpreter(PythonInterpreter.java:80) ~[jython-slim-2.7.2.jar!/:na]
        at org.python.jsr223.PyScriptEngine.<init>(PyScriptEngine.java:27) ~[jython-slim-2.7.2.jar!/:na]
        at org.python.jsr223.PyScriptEngineFactory.getScriptEngine(PyScriptEngineFactory.java:85) ~[jython-slim-2.7.2.jar!/:na]
        at org.springframework.scripting.support.StandardScriptUtils.retrieveEngineByName(StandardScriptUtils.java:60) ~[spring-context-5.3.3.jar!/:5.3.3]
        ... 57 common frames omitted

最终更新

我通过在 pom.xml...

中添加此依赖项使其工作
        <dependency>
            <groupId>org.python</groupId>
            <artifactId>jython-slim</artifactId>
            <version>2.7.2</version>
        </dependency>

... 并添加了 requiresUnpack 标签中的内容:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
                <requiresUnpack>
                    <dependency>
                        <groupId>org.python</groupId>
                        <artifactId>jython-slim</artifactId>
                    </dependency>
                </requiresUnpack>
            </configuration>
        </plugin>
    </plugins>
</build>

PD:记住需要在StepBuilder中配置项目处理器。

ScriptItemProcessor 将项目处理委托给 org.springframework.scripting.ScriptEvaluator。从 v5.3 开始,Spring 框架提供了 ScriptEvaluator 接口的三个实现:BshScriptEvaluatorGroovyScriptEvaluatorStandardScriptEvaluator.

默认情况下,如果你不指定使用哪个ScriptEvaluatorScriptItemProcessor将使用一个StandardScriptEvaluator,参见Javadoc of setScriptEvaluator. Now this StandardScriptEvaluator is based on JSR -223 javax.script,所以你需要确保默认ScriptEngine支持你要使用的语言。在您的情况下,默认情况下 python 不是受支持的语言,因此会出现错误。

如果你想通过JSR-223使用python,你可以使用Jython(将Jython的jar添加到classpath中应该就可以了,参见Calling Python from Java through scripting engine (jython)?