Java MongoDB Driver JNDI

875 views
Skip to first unread message

Davide Macocchi

unread,
Jun 13, 2017, 5:38:39 PM6/13/17
to mongodb-user
Hello,

I have some trouble when i'm about to include a MongoClient instance through @Reference annotation inside my java application.
I'm using wildfly version wildfly-10.1.0.Final with server mongod 3.4

I followed steps of this guide http://mongodb.github.io/mongo-java-driver/3.5/driver/tutorials/jndi/ but i cannot inject MongoClient correctly

STEP FOLLOWED
1)create of the module inside modules/system/layers/base/org/mongodb/main (copy of file mongo-java-driver-3.4.2.jar inside folder)

<module xmlns="urn:jboss:module:1.3" name="org.mongodb">
   <resources>
       <resource-root path="mongo-java-driver-3.4.2.jar"/>
   </resources>
   <dependencies>
       <module name="javax.api"/>
       <module name="javax.transaction.api"/>
       <module name="javax.servlet.api" optional="true"/>
   </dependencies>
</module>

2)add binding to standalone.xml like this

<subsystem xmlns="urn:jboss:domain:naming:2.0"> <bindings> <object-factory name="java:global/MyMongoClient" module="org.mongodb" class="com.mongodb.client.jndi.MongoClientFactory"> <environment> <property name="connectionString" value="mongodb://localhost:27017"/> </environment> </object-factory> </bindings> <remote-naming/> </subsystem>

3)inject of MongoClient like this

@Resource(lookup="java:global/MyMongoClient")
public MongoClient mongoClient;


THIS STEP DOESN'T WORK, it always gives me back exception

javax.ejb.EJBException: java.lang.IllegalStateException: WFLYEE0042: Failed to construct component instance
...
Caused by: javax.ejb.EJBException: java.lang.IllegalArgumentException: Can not set com.mongodb.MongoClient field mongodb.AbstractMongoDBService.mongoClient to com.mongodb.MongoClient


I also tried to inject it with manual lookup like

MongoClient client = InitialContext.doLookup("
java:global/MyMongoClient");

but it gives me

java.lang.ClassCastException: com.mongodb.MongoClient cannot be cast to com.mongodb.MongoClient

Can you please provide me an example of how to use this JNDI injection?

Thank you

Pronab Saha

unread,
Oct 17, 2017, 5:29:19 PM10/17/17
to mongodb-user
Please see git hub project by a user named jyemin at https://github.com/jyemin/WildflySampleWebApp. Try that project in a clean wildfly environment. If that project works, then it demonstrates that the driver set up you did in steps 1 and 2 works.

Your error seems to indicate that there is a dependency on a different version of the mongo driver in your path somewhere. This might be through transitive dependencies.

What tripped me up was the fact that I needed to put the dependency on org.mongo in my war manifest file. Without that dependency wildfly kept on saying that it is missing java.lang.NoClassDefFoundError: Lcom/mongodb/MongoClient. Now that I am writing this response, it makes me think that if I don't want to put that dependency on the manifest, I would need to use the tag "global-modules" inside of the wildfly standalone.xml config file

N EMan

unread,
Mar 23, 2018, 6:17:02 AM3/23/18
to mongodb-user
Hi,

I am facing the same issue. With wildfly 10.1 and Spring Boot PFB the pom.xml


<modelVersion>4.0.0</modelVersion>
<groupId>com.nosql.dummy</groupId>
<artifactId>demo.NoSQL</artifactId>
<version>0.0.3-SNAPSHOT</version>
<!-- This has to be added as part of the war file -->
<packaging>war</packaging>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<name>demo-NoSQL</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- <version>2.3.2</version> -->
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
</configuration>
</plugin>
<!-- This has to be added as part of the war file -->
<!-- <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> 
<configuration> <executable>true</executable> </configuration> </plugin> -->
<!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> 
<version>2.2</version> <configuration> <attachClasses>true</attachClasses> 
<classesClassifier>classes</classesClassifier> </configuration> </plugin> -->
</plugins>
</build>

<!-- This has to be added as part of the war file -->
<properties>
<java-version>1.8</java-version>
<jackson-2-version>2.8.6</jackson-2-version>
<start-class>com.nosql.dummy.mongo.application.MongoApplicationStarter</start-class>
</properties>


<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.0.2.RELEASE</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- This has to be added as part of the war file -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>



<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> 
</dependency> -->
<!-- This has to be added as part of the war file -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>

<!-- This has to be added as part of the war file -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.10.2</version>
</dependency>
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
</dependency>

</dependencies>
</project>


jboss-deployment-structure

<?xml version="1.0" encoding="UTF-8"?>


<jboss-deployment-structure>

    <deployment>

        <dependencies>

            <module name="org.mongodb" export="true"/>

        </dependencies>

    </deployment>

</jboss-deployment-structure>




Standalone.xml subsystem config:

<subsystem xmlns="urn:jboss:domain:naming:2.0">
            <bindings>
                <object-factory name="java:global/LocalMongoClient" module="org.mongodb" class="com.mongodb.client.jndi.MongoClientFactory">
                    <environment>
                        <property name="connectionString" value="mongodb://localhost:27017"/>
                    </environment>
                </object-factory>
            </bindings>
            <remote-naming/>
        </subsystem>

Can you please help guide me through subsiding this error.

nandu.ha...@6dtech.co.in

unread,
Dec 27, 2018, 7:57:50 PM12/27/18
to mongodb-user
Did you get the solution for this issue.
Reply all
Reply to author
Forward
0 new messages