提交 971bc350 编写于 作者: C chenjianxing

fix(接口测试): 无法导入内置python包

上级 d4c762bf
...@@ -490,6 +490,15 @@ ...@@ -490,6 +490,15 @@
<outputDirectory>src/main/resources/jmeter/lib/ext</outputDirectory> <outputDirectory>src/main/resources/jmeter/lib/ext</outputDirectory>
<destFileName>ApacheJMeter_functions.jar</destFileName> <destFileName>ApacheJMeter_functions.jar</destFileName>
</artifactItem> </artifactItem>
<artifactItem>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.0</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>src/main/resources/jmeter/lib/ext</outputDirectory>
<destFileName>jython-standalone.jar</destFileName>
</artifactItem>
</artifactItems> </artifactItems>
<outputDirectory>${project.build.directory}/wars</outputDirectory> <outputDirectory>${project.build.directory}/wars</outputDirectory>
<overWriteReleases>false</overWriteReleases> <overWriteReleases>false</overWriteReleases>
......
...@@ -11,7 +11,7 @@ import org.apache.jmeter.save.SaveService; ...@@ -11,7 +11,7 @@ import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.util.JMeterUtils; import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.visualizers.backend.BackendListener; import org.apache.jmeter.visualizers.backend.BackendListener;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import org.python.core.Options;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -34,10 +34,6 @@ public class JMeterService { ...@@ -34,10 +34,6 @@ public class JMeterService {
JMeterUtils.setJMeterHome(JMETER_HOME); JMeterUtils.setJMeterHome(JMETER_HOME);
JMeterUtils.setLocale(LocaleContextHolder.getLocale()); JMeterUtils.setLocale(LocaleContextHolder.getLocale());
//解决无法加载 PyScriptEngineFactory
Options.importSite = false;
try { try {
Object scriptWrapper = SaveService.loadElement(is); Object scriptWrapper = SaveService.loadElement(is);
HashTree testPlan = getHashTree(scriptWrapper); HashTree testPlan = getHashTree(scriptWrapper);
...@@ -51,7 +47,7 @@ public class JMeterService { ...@@ -51,7 +47,7 @@ public class JMeterService {
} }
} }
private String getJmeterHome() { public String getJmeterHome() {
String home = getClass().getResource("/").getPath() + "jmeter"; String home = getClass().getResource("/").getPath() + "jmeter";
try { try {
File file = new File(home); File file = new File(home);
......
package io.metersphere.listener; package io.metersphere.listener;
import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.service.ScheduleService; import io.metersphere.service.ScheduleService;
import org.python.core.Options;
import org.python.util.PythonInterpreter;
import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -12,12 +16,16 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve ...@@ -12,12 +16,16 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve
@Resource @Resource
private ScheduleService scheduleService; private ScheduleService scheduleService;
@Resource
private JMeterService jMeterService;
@Override @Override
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) { public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
System.out.println("================= 应用启动 ================="); System.out.println("================= 应用启动 =================");
initPythonEnv();
try { try {
Thread.sleep(3 * 60 * 1000); Thread.sleep(3 * 60 * 1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
...@@ -25,6 +33,24 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve ...@@ -25,6 +33,24 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve
} }
scheduleService.startEnableSchedules(); scheduleService.startEnableSchedules();
}
/**
* 解决接口测试-无法导入内置python包
*/
private void initPythonEnv() {
//解决无法加载 PyScriptEngineFactory
Options.importSite = false;
try {
PythonInterpreter interp = new PythonInterpreter();
String path = jMeterService.getJmeterHome();
System.out.println("sys.path: " + path);
path += "/lib/ext/jython-standalone.jar/Lib";
interp.exec("import sys");
interp.exec("sys.path.append(\"" + path + "\")");
} catch (Exception e) {
e.printStackTrace();
LogUtil.error(e.getMessage(), e);
}
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册