errors | |
---|---|
|
通过4.2 APP加载原理概述可知,app如果需要第三方的平台功能,则将第三方的包依赖进来,在打包时会自动作为私有库打入app的jar中。
以Springboot为例,说明如何与第三方平台交互,代码可参考snest-demo项目下的feignDemo app。
调用Springboot
在app的启动事件中启动Springboot,将Springboot的ApplicationContext
缓存成静态变量,供平台模型的方法调用;在卸载事件中停止Springboot并清除。
Springboot相关的配置在app的resources下面正常配置即可。
@SpringBootApplication
public class SpringApp {
private final static Logger logger = LoggerFactory.getLogger(SpringApp.class);
private static ConfigurableApplicationContext context;
public static synchronized void start(String[] args) {
if (context != null) {
return;
}
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
// 调整类加载器为当前APP的类加载器
Thread.currentThread().setContextClassLoader(SpringApp.class.getClassLoader());
try {
context = SpringApplication.run(SpringApp.class, args);
} catch (Throwable e) {
logger.error(e.getMessage(), e);
} finally {
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
}
public static synchronized void stop() {
if (context == null) {
return;
}
try {
context.stop();
} catch (Throwable e) {
logger.error(e.getMessage(), e);
} finally {
context = null;
}
}
/**
* 通过该方法调用Springboot容器中的对象
* @return
*/
public static synchronized ConfigurableApplicationContext getContext() {
return context;
}
}
如果使用的IDE是Intellij IDEA的Ultimate版本,并报JMX相关的错误,需要调整这个配置。
Springboot调用模型
Springboot调用模型的方法需要通过Meta实例。可以在app的启动事件里缓存Meta实例,在卸载事件里清除。
具体可参考3.3.6.2 第三方线程调用模型方法