spring批处理scriptitemprocessor

eufgjt7s  于 2021-07-15  发布在  Java
关注(0)|答案(1)|浏览(405)

我想在我的批处理程序中使用一个web抓取python脚本,但是我在web上找不到任何这样的例子。因此,可以将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$2.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$1.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中配置。

k0pti3hp

k0pti3hp1#

这个 ScriptItemProcessor 将项目处理委托给 org.springframework.scripting.ScriptEvaluator . 从v5.3开始,spring框架提供了 ScriptEvaluator 接口: BshScriptEvaluator , GroovyScriptEvaluator 以及 StandardScriptEvaluator .
默认情况下,如果不指定 ScriptEvaluator 使用 ScriptItemProcessor 将使用 StandardScriptEvaluator ,请参阅setscriptevaluator的javadoc。现在这个standardscriptevaluator基于jsr-223 javax.script ,因此您需要确保默认支持要使用的语言 ScriptEngine . 在您的例子中,默认情况下python不是受支持的语言,因此会出现错误。
如果希望通过jsr-223使用python,可以使用jython(添加 Jython 到类路径应该足够了,请参阅通过脚本引擎(jython)从java调用python

相关问题