[英]SpringBoot Tomcat Embedded Global JNDI Resource
I know there are a lot of questions about JNDI Resources in tomcat embedded, but I tried all the solutions I found without success. 我知道在tomcat嵌入式系统中有很多有关JNDI资源的问题,但是我尝试了所有发现的解决方案,但均未成功。
I have an application that expose Rest API for my clients. 我有一个为我的客户公开Rest API的应用程序。 Inside this app, we have a async solution using JMS and Amazon SQS.
在此应用程序内部,我们有一个使用JMS和Amazon SQS的异步解决方案。 The App uses third-part libs, that uses JNDI to get sql.Datasource, because of that, I need use JNDI Datasource.
该应用程序使用第三方库,该库使用JNDI获取sql.Datasource,因此,我需要使用JNDI Datasource。
The problem is, when the App does a call to this libs in the same thread of Rest Controller, the JNDI Lookup works, and the datasource is getted. 问题是,当应用程序在Rest Controller的同一线程中对此lib进行调用时,JNDI查找工作,并且数据源被获取。
When my @JmsListener calls this libs, I get a NamingNotFoungException. 当我的@JmsListener调用此库时,我得到一个NamingNotFoungException。
I've used context.list("java") in 2 points of my code and confirmed that, inside JmsListener, there is no JNDI Context. 我在代码的两点中使用了context.list(“ java”)并确认在JmsListener内没有JNDI Context。
My tomcat factory class: Configuration public class CustomTomcatEmbeddedServletContainerFactory { 我的tomcat工厂类:配置公共类CustomTomcatEmbeddedServletContainerFactory {
@Value("${spring.log.datasource.jndiName}")
private String logJndiName;
@Value("${spring.log.datasource.password}")
private String logPassword;
@Value("${spring.log.datasource.url}")
private String logUrl;
@Value("${spring.log.datasource.username}")
private String logUsername;
@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
return new TomcatEmbeddedServletContainerFactory() {
@Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {
tomcat.enableNaming();
return super.getTomcatEmbeddedServletContainer(tomcat);
}
@Override
protected void postProcessContext(Context context) {
// LogDS
context.getNamingResources()
.addResource(
getContextResource(logJndiName, logUrl, logUsername, logPassword)
);
ContextResourceLink contextResourceLink = new
ContextResourceLink();
contextResourceLink.setGlobal(logJndiName);
contextResourceLink.setName(logJndiName);
contextResourceLink.setType("javax.sql.DataSource");
context.getNamingResources().addResourceLink(contextResourceLink);
}
private ContextResource getContextResource(
final String name
, final String url
, final String username
, final String password
) {
ContextResource resource = new ContextResource();
resource.setName(name);
resource.setType(DataSource.class.getName());
resource.setProperty("factory", "com.zaxxer.hikari.HikariJNDIFactory");
resource.setProperty("jdbcUrl", url);
resource.setProperty("dataSource.user", username);
resource.setProperty("dataSource.password", AESCrypto.decrypt(password));
resource.setScope("Sharable");
return resource;
}
};
}
}
Any idea of this problem? 对这个问题有想法吗?
-------Update--------- -------更新---------
When I use the code below, the context in JMSListener solve but my RestController doesn't answer anymore, a 404 http status happen. 当我使用下面的代码时,JMSListener中的上下文解决了,但我的RestController不再应答,出现了404 http状态。
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {
tomcat.enableNaming();
TomcatEmbeddedServletContainer container = super.getTomcatEmbeddedServletContainer(tomcat);
for (Container child : container.getTomcat().getHost().findChildren()) {
if (child instanceof Context) {
ClassLoader contextClassLoader = ((Context) child).getLoader().getClassLoader();
Thread.currentThread().setContextClassLoader(contextClassLoader);
break;
}
}
return container;
}
-------Update2--------- My problem is fixed. -------更新2 ---------我的问题已解决。 Instead of returning "container", like I said above, I was returning super.getTomcatEmbeddedServletContainer(tomcat);
而不是像我上面所说的那样返回“容器”,而是返回super.getTomcatEmbeddedServletContainer(tomcat);。 The manual GlobalContext in my first update works well!
我的第一次更新中的手册GlobalContext很好用!
My problem is fixed. 我的问题已解决。 Instead of returning "container", like I said above, I was returning super.getTomcatEmbeddedServletContainer(tomcat);
而不是像我上面所说的那样返回“容器”,而是返回super.getTomcatEmbeddedServletContainer(tomcat);。 The manual GlobalContext in my first update works well!
我的第一次更新中的手册GlobalContext很好用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.