我知道嵌入了tomcat中的JNDI资源有很多问题,但是我尝试了我发现的所有解决方案而没有成功。
我有一个应用程序,可以为我的客户介绍REST API。在此应用程序中,我们使用JMS和Amazon SQS提供了异步解决方案。该应用使用第三部分的libs,使用jndi获取sql.datasource,因此,我需要使用jndi datasource。
问题是,当应用程序在同一rest控制器的线程中对此lib进行调用时,jndi查找工作起作用,并且数据源已得到。
当我的@jmslistener称此libs时,我会得到namingnotfoungexception。
我在我的代码的2点中使用了context.list(" java"),并确认在JMSListener内部没有JNDI上下文。
我的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;
}
};
}
}
对此问题有任何想法吗?
-------更新---------------
当我使用下面的代码时,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;
}
----------------------------------------------------------------------------------------------------我的问题是解决的。我没有像上面说的那样返回"容器",而是返回Super.getTomcatembedservletContainer(Tomcat);我的第一个更新中的手册GlobalContext运行良好!
我的问题是解决的。我没有像上面说的那样返回"容器",而是返回Super.getTomcatembedservletContainer(Tomcat);我的第一个更新中的手册GlobalContext运行良好!