|
| 1 | ++++ |
| 2 | +date = "2015-03-19T12:53:26-04:00" |
| 3 | +title = "JNDI" |
| 4 | +[menu.main] |
| 5 | + parent = "Sync Connecting" |
| 6 | + identifier = "Sync JNDI" |
| 7 | + weight = 30 |
| 8 | + pre = "<i class='fa'></i>" |
| 9 | ++++ |
| 10 | + |
| 11 | +## Java Naming and Directory Interface (JNDI) |
| 12 | + |
| 13 | +The driver includes a [JNDI](http://docs.oracle.com/javase/8/docs/technotes/guides/jndi/index.html) ObjectFactory implementation, |
| 14 | +[`MongoClientFactory`]({{< apiref "com/mongodb/client/jndi/MongoClientFactory" >}}), that returns `MongoClient` instances based on a |
| 15 | +[connection string](http://docs.mongodb.org/manual/reference/connection-string/). |
| 16 | + |
| 17 | +## Examples |
| 18 | + |
| 19 | +The configuration of the `MongoClientFactory` differs depending on the application server. Below are examples of a few popular ones. |
| 20 | + |
| 21 | +### Wildfly (formerly JBoss) |
| 22 | + |
| 23 | +1. In a [Wildfly](http://wildfly.org/) installation, create a new module for MongoDB at `modules/system/layers/base/org/mongodb/main`. |
| 24 | + |
| 25 | +2. Copy the mongo-java-driver jar file into the module. |
| 26 | + |
| 27 | +3. Add the following module.xml file into the module: |
| 28 | + |
| 29 | + <module xmlns="urn:jboss:module:1.3" name="org.mongodb"> |
| 30 | + <resources> |
| 31 | + <resource-root path="mongo-java-driver-3.3.0.jar"/> |
| 32 | + </resources> |
| 33 | + <dependencies> |
| 34 | + <module name="javax.api"/> |
| 35 | + <module name="javax.transaction.api"/> |
| 36 | + <module name="javax.servlet.api" optional="true"/> |
| 37 | + </dependencies> |
| 38 | + </module> |
| 39 | + |
| 40 | + |
| 41 | +4. Add a binding to JBoss's naming subsystem configuration that references the above module, the `MongoClientFactory` class, and the |
| 42 | +connection string for the MongoDB cluster. |
| 43 | + |
| 44 | + <subsystem xmlns="urn:jboss:domain:naming:2.0"> |
| 45 | + <bindings> |
| 46 | + <object-factory name="java:global/MyMongoClient" module="org.mongodb" class="com.mongodb.client.jndi.MongoClientFactory"> |
| 47 | + <environment> |
| 48 | + <property name="connectionString" value="mongodb://localhost:27017"/> |
| 49 | + </environment> |
| 50 | + </object-factory> |
| 51 | + </bindings> |
| 52 | + <remote-naming/> |
| 53 | + </subsystem> |
| 54 | + |
| 55 | +A MongoClient instance will be accessible via the JNDI name `java:global/LocalMongoClient`. |
| 56 | + |
| 57 | +### Tomcat |
| 58 | + |
| 59 | +1. In a [Tomcat](http://tomcat.apache.org/) installation, copy the mongo-java-driver jar file into the lib directory. |
| 60 | + |
| 61 | +2. In context.xml of a web application, add a resource that references the `MongoClientFactory` class, and the connection string for the |
| 62 | +MongoDB cluster: |
| 63 | + |
| 64 | + <Resource name="mongodb/MyMongoClient" |
| 65 | + auth="Container" |
| 66 | + type="com.mongodb.MongoClient" |
| 67 | + closeMethod="close" |
| 68 | + factory="com.mongodb.client.jndi.MongoClientFactory" |
| 69 | + singleton="true" |
| 70 | + connectionString="mongodb://localhost"/> |
| 71 | + |
| 72 | +3. In web.xml of a web application, add a reference to the above resource: |
| 73 | + |
| 74 | + <resource-ref> |
| 75 | + <res-ref-name> |
| 76 | + mongodb/MyMongoClient |
| 77 | + </res-ref-name> |
| 78 | + <res-type> |
| 79 | + com.mongodb.MongoClient |
| 80 | + </res-type> |
| 81 | + <res-auth> |
| 82 | + Container |
| 83 | + </res-auth> |
| 84 | + </resource-ref> |
| 85 | + |
| 86 | +A MongoClient instance will be accessible via the JNDI name `mongodb/MyMongoClient` in the `java:comp/env` context. |
0 commit comments