2

I am trying to achieve injecting a jndi resource using CDI for wildfly 8. For this purpose i want to use a custom jnidfactory as developed in https://github.com/juanlmelo/mongo-jndi-plugin/

The problem is due to my limited knowledge in wildfly, I don't know the following.

1) how to activate/attach this jndifactory in wildfly, ofcourse I can create an object while startup and assign a jndi name to it programmatically , but want to explore custom factory feature of wildfly

2) the best practice to set the uri property needed by the object factory, i assume using System.getProperty inside the factory should suffice , as the DB uri will be different for each installation

once this is achieved I am confident I can get it injected into my classes using cdi.

I have tried my best to look for similar post, but couldn't find any, if you think this is duplicate please point me to the correct one.

Thanks,

1 Answer 1

2

If you want add custom JNDI factory to wildfly using https://github.com/juanlmelo/mongo-jndi-plugin/ you need to do few things:

1) You need to change a little bit implementation of https://github.com/juanlmelo/mongo-jndi-plugin/blob/master/src/main/java/com/mongodb/jndi/MongoClientJNDIFactory.java
- line 38 change to:
String mongoURI = (String) environment.get(MONGO_CLIENT_URI);
and comment out or delete lines 39-49

2) then run command mvn clean package and create directory eg.:
wildfly-8.1.0.Final/modules/com/mongodb/jndi/main/
copy there mongo-jndi-plugin-1.0.jar and create there module.xml file with content:

<?xml version="1.0" ?>
<module xmlns="urn:jboss:module:1.1" name="com.mongodb.jndi">
    <resources>
        <resource-root path="mongo-jndi-plugin-1.0.jar"/>
    </resources>
    <dependencies>
         <module name="com.mongodb.driver"/>
        <module name="javax.api"/>
    </dependencies>
</module>

3) add mongo driver
-create directory: wildfly-8.1.0.Final/modules/com/mongodb/driver/main
-create there file: module.xml and place there:

<?xml version="1.0" ?> 
<module xmlns="urn:jboss:module:1.1" name="com.mongodb.driver">
<resources>
    <resource-root path="mongo-java-driver-2.11.0.jar"/>
</resources>
<dependencies>
    <module name="javax.api"/>
</dependencies> 
</module>

-add to this directory mongo-java-driver-2.11.0.jar library

4) add something like this
<subsystem xmlns="urn:jboss:domain:naming:2.0"> <bindings> <object-factory name="java:global/MongoClient" module="com.mongodb.jndi" class="com.mongodb.jndi.MongoClientJNDIFactory"> <environment> <property name="mongoClientURI" value="mongodb://username:[email protected]:27017,username:[email protected]:27017"/> </environment> </object-factory> </bindings> <remote-naming/> </subsystem>

to your domain.xml or standalone.xml



voilà
After this steps you can inject MongoClient into your classes using @Resource annotation (eg. @Resource(lookup = "java:global/MongoClient"

1
  • thanks for this response, currently I made a simple Producer for this to continue working , but I will move it to JNDI driver soon and If I understand it right, this is actually an example of defining modules and exposing them as JNDI resource in WildFly server
    – suro
    Aug 18, 2014 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.