Friday, September 6, 2013

Setup Eclipse for mongoDB and WebSphere Application Development Environment


Install JDK

Download jdk from http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase6-419409.html
# cd /opt
# <path to jdk binary>/jdk-6u38-linux-x64.bin
# cd /opt/jdk1.6.0_38

set environment variables for users accordingly
set JAVA_HOME=/opt/jdk1.6.0_38
and set PATH=$JAVA_HOME/bin:$PATH

Install mongoDB

Download mongoDB from http://www.mongodb.org/downloads
Extract the binaries in /opt/mongoDB
Verify Installation
# cd /opt/mongoDB/bin
Start Server
# ./mongod

Start client
# ./mongo

Install WebSphere Application Server

please refer to IBM WebSphere Application Server Installation

Install Eclipse

Download Eclipse from http://www.eclipse.org/downloads/
Eclipse Juno is used in this scenario.
extract the binary into /opt/tools
$ cd /opt/tools
$ tar -zxf eclipse-jee-juno-SR2-linux-gtk-x86_64.tar.gz
$ mv eclipse mongodbDev

Configuring Eclipse with WebSphere Application Server

Help -> Eclipse Marketplace -> Search
type in "Websphere application server" in Find field, and click Go
In the results panel, Install "IBM WebSphere Application Server V8.0 Developer Tools for Eclipse Juno"
Followin instructions in screen to finish the installation.
Restart Eclipse

Goto Windows -> Preferences -> Server -> Runtime Environments
Remove the existing one "Web Preview Server Runtime".
Add -> IBM -> WebSphere Application Server v8.0
Check "Create a new local server", click Next
Point "Installation Dictionary" to your WebSphere Application Server installation location, i.e. /opt/IBM/WebSphere/AppServer
Provide information accordingly in the following screens, and click Finish.
Goto Windows -> Preferences -> Server -> WebSphere Application Server to verify the settings.

Download MongoDB Java Driver from http://api.mongodb.org/java/
we use version of 2.11.2 in this scenario, and put it in your CLASSPATH.

Create First Application, mongodbHelloWorld

Goto File -> New -> Dynamic Web Project
Project Name: mongodbHelloWorld
Click Finish

In project of mongodbHelloWorld, goto File -> New -> Servlet
Java package: ca.goweekend.mongo
Class name: HelloMongodb
Click Finish
paste below code in HelloMongodb.java, and you will see many errors.

Now let's include Mongodb Java Driver into CLASSPATH to resove those errors.
right click on mongodbHelloWorld project, and choose properties -> Java Build Path -> Libraries -> Add External Jars, add the Java driver you downloaded before. Click OK, and the errors should be all gone.

Goto Windows -> Preferences -> Java -> Build Path -> Classpath Variables -> New
Name: Mongodb Java Driver
Path: /apps/mongodb/mongo-java-driver-2.11.2.jar

HelloMongodb.java

package ca.goweekend.mongo;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ibm.ws.webcontainer.servlet.ServletConfig;
import com.mongodb.MongoClient;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;

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

        /**
         * @see HttpServlet#HttpServlet()
         */
        public HelloMongodb() {
                super();
        }

        public void init(ServletConfig config) {
                System.out.println("Inititiating Database Connection.");
        }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
         *      response)
         */
        protected void doGet(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, IOException {

                MongoClient mongleClient = new MongoClient("db.goweekend.ca",
                                27017);

                DB db = mongleClient.getDB("HelloMongodb");

                DBCollection coll = db.getCollection("client");

                try {
                        BasicDBObject doc = new BasicDBObject();

            String yourIPAddress = request.getRemoteAddr();
            String hostname = request.getRemoteHost();
            int portNumber = request.getRemotePort();
                        doc.put("hostname", hostname);
                        doc.put("ipaddress", yourIPAddress);
                        doc.put("portnumber", portNumber);

                        coll.insert(doc);

                } catch (Exception e) {
                        e.printStackTrace();
                }

                DBCursor cursor = coll.find();

                try {
                   while(cursor.hasNext()) {
                           response.getWriter().print(cursor.next());
                           response.getWriter().print("\n");
                   }
                } finally {
                   cursor.close();
                }
        }
}

right click on HellowMongodb.java in Enterprise Explorer Panel, and chose Run As -> Run on Server, and follow the instructions on screen, and you will see the result. Open the URL on different machine, you will see different machine entries added in MongoDB.

To use ant to build ear file, please refer to http://feijiangnan.blogspot.ca/2013/09/build-ear-file-with-ant-for-mongodb.html

No comments:

Post a Comment