This sample contains a server definition that includes the MongoDB, JNDI, and Servlet
features. The application, mongoDBApp.war, uses the Liberty Extended Profile's
ability to configure a MongoDB database to inject a com.mongodb.DB
instance
into a class to perform a number of insert/find operations. The DB object is injected using
a @javax.annotation.Resource
annotation using the following code:
@WebServlet("/")
public class MongoServlet extends HttpServlet {
??????????@Resource(name = "mongo/sampledb")
??????????protected DB db;
...
}
This sample assumes that a MongoDB instance is running on
localhost:27017
(default mongo settings).
This sample can be installed onto runtime versions 8.5.5.0 and later.
To run the MongoDB application:
java -jar mongoDBSample.jar
localhost:27017
or update the server.xml
file to point to your MongoDB instance
see notes for more infomation on
doing this.
bin
directory in the Liberty profile installation:
server run mongoDBSample
CWWKZ0001I: Application mongoDBApp started in XX.XX seconds.
com.mongodb.DB
resource was injected if you see the following
message:HelloMongo!
bin/server stop mongoDBSample
'.Example output from a successful run :
HelloMongo!
[count] collection count = 0 from [sampledb]
[inserting] 10
[dump entire db] :
{ "_id" : { "$oid" : "51684c16ecd91696ca5176a1"} , "user" : 0}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a2"} , "user" : 1}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a3"} , "user" : 2}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a4"} , "user" : 3}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a5"} , "user" : 4}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a6"} , "user" : 5}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a7"} , "user" : 6}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a8"} , "user" : 7}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176a9"} , "user" : 8}
{ "_id" : { "$oid" : "51684c17ecd91696ca5176aa"} , "user" : 9}
Troubleshooting :
localhost:27017
, you can change the location via the mongo
configuration element in the server.xml file.'[WARNING ] Server seen down: localhost/127.0.0.1:27017 - java.io.IOException - message: Connection refused: connect'.The solution to this problem is to start a MongoDB process at the default port on the local machine.
SRVE0187E
message, you most likely have a problem with your shared
library. Ensure that the library element in the server.xml
points to a
valid Mongo Java Driver jar file.The table below shows the dependencies that are required for: Derby pre-requisite libraries.
Target install path | Source URL |
---|---|
shared/resources/MongoDBSampleLibs/mongo-java-driver-2.11.1.jar | http://driver-downloads.mongodb.org/java/2.11.1/mongo-java-driver-2.11.1.jar |
For further information and resources for developers using IBM WebSphere Application Server, please visit wasdev.net.