ServiceLoader在打包的Spring Boot应用中无法工作。

huangapple go评论82阅读模式
英文:

ServiceLoader do not work in packaged Spring Boot apps

问题

(copy from my GitHub issue: https://github.com/spring-projects/spring-boot/issues/22955)

我注意到在打包的Spring Boot应用程序中,Java的ServiceLoader机制无法正常工作。

背景

我尝试使用javax.script.ScriptEngineManager,它依赖于ServiceLoader。我可以成功地从集成开发环境(IDE)启动应用程序,但无法通过命令行启动。

复现

// build.gradle
plugins {
    id 'java'
    id 'org.springframework.boot' version '2.3.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
}

tasks.withType(JavaCompile) {
    options.release.set(11) // required Gradle >= 6.6
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.python:jython-slim:2.7.2'
}
// Main.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.script.ScriptEngineManager;
import java.util.Objects;

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
        var engine = new ScriptEngineManager().getEngineByName("python");
        Objects.requireNonNull(engine);
        System.out.println("success");
    }
}

可以从集成开发环境(例如我的情况下是IntelliJ)启动此应用程序,但不能通过命令行启动:

gradle bootJar && java -jar build/libs/XXX.jar

临时解决方法

可以直接使用Jython的ScriptEngine实现,而不是使用ScriptEngineManager

var engine = new org.python.jsr223.PyScriptEngineFactory().getScriptEngine();
英文:

(copy from my GitHub issue: https://github.com/spring-projects/spring-boot/issues/22955)

I noticed that Java's ServiceLoader mechanism doesn't work in packaged Spring Boot apps.

Background

I've tried to use javax.script.ScriptEngineManager which relies on ServiceLoaders. I was able to successfully launch the app from the IDE but not from the command line.

Repro

// build.gradle
plugins {
    id 'java'
    id 'org.springframework.boot' version '2.3.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
}

tasks.withType(JavaCompile) {
    options.release.set(11) // required Gradle >= 6.6
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.python:jython-slim:2.7.2'
}
// Main.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.script.ScriptEngineManager;
import java.util.Objects;

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
        var engine = new ScriptEngineManager().getEngineByName("python");
        Objects.requireNonNull(engine);
        System.out.println("success");
    }
}

It is possible to launch this from the IDE (IntelliJ in my case) but not via the command line:

gradle bootJar && java -jar build/libs/XXX.jar

Temporary workaround

Instead of using the ScriptEngineManager it is possible to directly use Jython's ScriptEngine implementation:

  var engine = new org.python.jsr223.PyScriptEngineFactory().getScriptEngine();

答案1

得分: 3

ServiceLoader 机制能够正确地找到 PyScriptEngineFactory。问题出在它尝试从中创建脚本引擎时出现了静默失败。不幸的是,当你调用 getEngineByName(String) 时,ScriptEngineManager 会吞掉 getScriptEngine() 抛出的任何异常:

try {
    ScriptEngine engine = spi.getScriptEngine();
    engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
    return engine;
} catch (Exception exp) {
    if (DEBUG) exp.printStackTrace();
}

你的解决方法对我不起作用,但这对我很有用,因为它允许我看到被 ScriptEngineManager 吞掉的异常。异常如下所示:

Exception in thread "main" java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:109)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
	at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)
Caused by: java.lang.IllegalArgumentException: URI is not hierarchical
	at java.io.File.<init>(File.java:418)
	at org.python.core.PrePy.getJarFileNameFromURL(PrePy.java:427)
	at org.python.core.PrePy._getJarFileName(PrePy.java:362)
	at org.python.core.PrePy.getJarFileName(PrePy.java:345)
	at org.python.core.PySystemState.doInitialize(PySystemState.java:1195)
	at org.python.core.PySystemState.initialize(PySystemState.java:1130)
	at org.python.core.PySystemState.initialize(PySystemState.java:1085)
	at org.python.core.PySystemState.initialize(PySystemState.java:1080)
	at org.python.core.PySystemState.initialize(PySystemState.java:1075)
	at org.python.core.PySystemState.initialize(PySystemState.java:1070)
	at org.python.core.PySystemState.<init>(PySystemState.java:207)
	at org.python.util.PythonInterpreter.threadLocalStateInterpreter(PythonInterpreter.java:80)
	at org.python.jsr223.PyScriptEngine.<init>(PyScriptEngine.java:27)
	at org.python.jsr223.PyScriptEngineFactory.getScriptEngine(PyScriptEngineFactory.java:85)
	at com.example.demo.Gh22955Application.main(Gh22955Application.java:11)
	... 8 more

PrePy 对于 jar:file: 的 URL 做了一些假设,在 Spring Boot 的 "fat jar" 中这些假设不成立。Spring Boot 为此提供了一种解决方法,允许在启动时自动从 "fat jar" 中解压出一个 jar 文件。在这种情况下,需要解压 jython-slim jar 文件。为此,将以下内容添加到你的 build.gradle 中:

bootJar {
    requiresUnpack "**/jython-slim*"
}
英文:

The ServiceLoader mechanism is correctly finding PyScriptEngineFactory. The problem is then a silent failure when it attempts to create a script engine from it. Unfortunately, when you call getEngineByName(String), ScriptEngineManager swallows any exception thrown by getScriptEngine():

try {
ScriptEngine engine = spi.getScriptEngine();
engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
return engine;
} catch (Exception exp) {
if (DEBUG) exp.printStackTrace();
}

Your work around doesn't work for me, but that's useful as it allows me to see the exception swallowed by ScriptEngineManager. It is the following:

Exception in thread &quot;main&quot; java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:109)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)
Caused by: java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.&lt;init&gt;(File.java:418)
at org.python.core.PrePy.getJarFileNameFromURL(PrePy.java:427)
at org.python.core.PrePy._getJarFileName(PrePy.java:362)
at org.python.core.PrePy.getJarFileName(PrePy.java:345)
at org.python.core.PySystemState.doInitialize(PySystemState.java:1195)
at org.python.core.PySystemState.initialize(PySystemState.java:1130)
at org.python.core.PySystemState.initialize(PySystemState.java:1085)
at org.python.core.PySystemState.initialize(PySystemState.java:1080)
at org.python.core.PySystemState.initialize(PySystemState.java:1075)
at org.python.core.PySystemState.initialize(PySystemState.java:1070)
at org.python.core.PySystemState.&lt;init&gt;(PySystemState.java:207)
at org.python.util.PythonInterpreter.threadLocalStateInterpreter(PythonInterpreter.java:80)
at org.python.jsr223.PyScriptEngine.&lt;init&gt;(PyScriptEngine.java:27)
at org.python.jsr223.PyScriptEngineFactory.getScriptEngine(PyScriptEngineFactory.java:85)
at com.example.demo.Gh22955Application.main(Gh22955Application.java:11)
... 8 more

PrePy is making some assumptions about jar:file: URLs that don't hold true in a Spring Boot fat jar. Spring Boot provides an escape hatch for this, allowing a jar file to be automatically unpacked from the fat jar when it's launched. In this case, it's the jython-slim jar that needs to be unpacked. To do that, add the following to your build.gradle:

bootJar {
requiresUnpack &quot;**/jython-slim*&quot;
}

huangapple
  • 本文由 发表于 2020年8月14日 22:36:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63414862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定