在WAB内运行Liberty的MongoDB的JNDI查找期间发生ClassCastException [英] ClassCastException during JNDI lookup for MongoDB, inside WAB, running Liberty

查看:101
本文介绍了在WAB内运行Liberty的MongoDB的JNDI查找期间发生ClassCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行Liberty Profile的WAB/OSGi内对MongoDB使用JNDI查找.

I want to use JNDI lookup for MongoDB, inside WAB/OSGi, running Liberty Profile.

import com.mongodb.DB;

@WebServlet("/MongoServlet")
public class MongoServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;  

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {
            DB db = (DB) new InitialContext().lookup("java:comp/env/mongodb");
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
}

JNDI查找给出以下异常.

The JNDI lookup gives below exception.

[ERROR   ] SRVE0777E: Exception thrown by application class 'com.osgi.jndi.web.MongoServlet.doGet:57'
java.lang.ClassCastException: com.mongodb.DBApiLayer incompatible with com.mongodb.DB
com.mongodb.DBApiLayer is subclass of com.mongodb.DB. 

我相信ClassCastException是由于我的应用程序和OSGi bootclassloader使用了不同的类加载器.但是,我不知道如何解决它.

I believe that the ClassCastException is because of different classloaders being used by my application, and OSGi bootclassloader. But, I don't know how to troubleshoot it.

推荐答案

当前无法在OSGi应用程序中使用mongodb-2.0功能.

Currently there is no way to use the mongodb-2.0 feature with an OSGi application.

查找com.mongodb.DB资源将使您返回DBApiLayer的实例,但这不是问题(因为DBApiLayer扩展了DB).即使这样做,您仍然会收到ClassCastException:

Looking up a com.mongodb.DB resource will return you back an instance of DBApiLayer, but that's not the issue (since DBApiLayer extends DB). You would still get a ClassCastException even if you did this:

DBApiLayer db = (DBApiLayer) new InitialContext().lookup("java:comp/env/mongodb");

会抛出:

java.lang.ClassCastException: com.mongodb.DBApiLayer incompatible with com.mongodb.DBApiLayer

此限制的原因是mongodb-2.0功能将使用Liberty中的Classloader来加载com.mongodb类,并且OSGi应用程序将使用单独的Classloader来加载com.mongodb类,无论您如何配置

The reason for this limitation is that the mongodb-2.0 feature will use a Classloader from Liberty to load com.mongodb classes, and an OSGi application will use a separate Classloader to load com.mongodb classes, no matter how you configure things.

普通的Java EE应用程序可以执行以下操作:

Normal Java EE applications can do things like this:

<library id="MongoLib">
    <file name="${server.config.dir}/lib/mongo-java-driver-2.11.4.jar"/>
</library>

<application name="myApp">
    <classloader commonLibraryRef="MongoLib"/>
</application>

但是,<osgiApplication>元素不具备该功能.不幸的是,这里唯一的解决方案是与IBM建立RFE(如果对此已经存在RFE,则向上投票).

However, <osgiApplication> elements do not have that capability. Unfortunately, the only solution here is to open an RFE with IBM (or upvote one if an RFE already exists for this).

这篇关于在WAB内运行Liberty的MongoDB的JNDI查找期间发生ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆