Wednesday, December 24, 2014

Installing and Getting Started with MongoDB

  1. MongoDB is an open source database that uses a document-oriented data model. MongoDB is one of several database types under the NoSQL banner. Instead of using tables and rows as in relational databases, MongoDB is built on an architecture of collections and documents.It is different from relational database. In this blog we will install MongoDB on windows7 machine and get started with mongo shell by performing some CRUD operations.
  2. Lets download the latest version of mongodb from the link-https://www.mongodb.org/dr//fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-2.6.6-signed.msi/download. Download and unzip it in any folder of your choice. In my case have extracted it in C:\mongodb folder. All the executable files are inside the bin folder. To run mongodb we will need a data folder. By default mongodb uses C:\data\db. We are not going to use the default value. To provide my own values I will be using a mongo.config file and will pass this config file as the parameter at the start-up time. I have this file created in C:\mongodb\mongo.config. Below are the contents of this file:-

  3. ##path to store data
    dbpath=C:\mongodb\data

    ##path for logfile
    logpath=C:\mongodb\log\mongo.log
Here I am providing the path for the data folder as well as where to create the log files. So lets start mongodb by going to C:\mongodb\bin directory and issueing the follwoing command:-

C:\mongodb\bin>mongod.exe --config C:\mongodb\mongo.config

This will start mongodb.We can also see entries like below in the log file:-


2014-12-24T14:30:09.702+0000 [initandlisten] MongoDB starting : pid=11892 port=27017 dbpath=C:\mongodb\data 64-bit host=MC0WAMCC
2014-12-24T14:30:09.702+0000 [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2014-12-24T14:30:09.702+0000 [initandlisten] db version v2.6.6
2014-12-24T14:30:09.702+0000 [initandlisten] git version: 608e8bc319627693b04cc7da29ecc300a5f45a1f
2014-12-24T14:30:09.702+0000 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') BOOST_LIB_VERSION=1_49
2014-12-24T14:30:09.702+0000 [initandlisten] allocator: system
2014-12-24T14:30:09.702+0000 [initandlisten] options: { config: "C:\mongodb\mongo.config", storage: { dbPath: "C:\mongodb\data" }, systemLog: { destination: "file", path: "C:\mongodb\log\mongo.log" } }
2014-12-24T14:30:09.721+0000 [initandlisten] journal dir=C:\mongodb\data\journal
2014-12-24T14:30:09.722+0000 [initandlisten] recover : no journal files present, no recovery needed
2014-12-24T14:30:09.872+0000 [initandlisten] waiting for connections on port 27017
2014-12-24T14:31:09.868+0000 [clientcursormon] mem (MB) res:54 virt:466
2014-12-24T14:31:09.868+0000 [clientcursormon]  mapped (incl journal view):320
2014-12-24T14:31:09.868+0000 [clientcursormon]  connections:0

Once mongodb is started we need to open another commandprompt to connect to mongo shell to issue commands.So open another window and go to C:\mongodb\bin> and execute the mongo.exe file:

C:\mongodb\bin>mongo.exe

We can see output like this:-


C:\mongodb\bin>mongo.exe
MongoDB shell version: 2.6.6
connecting to: test

By default it connects to the test database. But we perform CRUD opetation lets get some idea about some new terms that are introduced with mongodb and their relational database counterpart. As we are from RDBMS background its good to know these terms as they will be used frequently.

1.Databse - Database
2.Collection - Table
3.Document - Row

unlike RDBMS mongodb doesnt have the concept of schema, i.e, we dont have to define table structure to store and retrive data from it. We also dont have to create a database or table or collection. Mongodb does this for us on the fly. Mongodb uses JSON like dataformat called BSON (actually binary representation). Also mongo shell uses javascript to issue commands.

So lets create a database called tutorials for learning puropse. Issue command:- use tutorails from mongo shell and we can see the output like siwtched to tutorials without even we creating the tutorials database.Mongodb is lazy in the sense that at this point it will not be creat the databse until we insert some data into it.Databases and collections are created only when documents are first inserted.

C:\mongodb\bin>mongo.exe
MongoDB shell version: 2.6.6
connecting to: test
> use tutorials
switched to db tutorials

Now lets create a collection(table) called users and try to insert some document(rows) into it. To do this issue the below command

db.users.insert({username:"Sanjiv Kumar"})

The above command will create the collection users and will insert the document in it. We need not create the collection explicitly as mentioned earlier. We have saved our first document. Just cross check if it has been really saved or not by issuing the below command:-
db.user.find()

We can see output like this:-

{ "_id" : ObjectId("549ad320a99604bc0b1ee9b8"), "username" : "Sanjiv Kumar" }

Note that an _id field has been added to the document. We can think of the _id value as the document’s primary key. Every MongoDB document requires an _id, and if one isn’t present when the document is created, then a special MongoDB object ID will be generated and added to the document at that time. 

Lets add one more user by issuing the below command

db.users.insert({username:"Rajeev Kumar"})

Now that we have more than one document in the collection lets try some complex queries. As seen earlier db.users.find() will return all the documents from the collection. But we can also pass simple query selector to the find() method. Lets issue the below command and see the output:-

db.users.find({username:"Rajeev Kumar"})

{ "_id" : ObjectId("549ad5afa99604bc0b1ee9b9"), "username" : "Rajeev Kumar" }

Now lets try to update any existing document by issuing the below command:-

db.users.update({username:"Sanjiv Kumar"},{$set:{country:"India"}})

You can cross check by issuing the find command whether the country is added or not, as you can see from the below that it has been added:-

db.users.update({username:"Sanjiv Kumar"},{$set:{country:"India"}})

Now lets try to remove any document by issuing the below command:-

db.users.remove({username:"Sanjiv Kumar"})

To drop the collection use the below command:

db.users.drop()

Thats all for this blog. We saw how to install mongodb and performed some very basic CRUD operation to get going.

Thanks for reading.










Tuesday, December 23, 2014

Java/J2EE Blog: Load Balancing and failover with Jboss Applicaiton...

Java/J2EE Blog: Load Balancing and failover with Jboss Applicaiton...: In my previous two blogs I demonstrated how to create Jboss cluster in standalone and domain mode respectively. But there I had to manuall...

Load Balancing and failover with Jboss Applicaiton Server 7 and Apache Web Server

In my previous two blogs I demonstrated how to create Jboss cluster in standalone and domain mode respectively. But there I had to manually hop from one server url to another server url to demonstrate the clustering. But the real flavour of clustering is when we use it with any load balancer so that the user does not and should not manually hop from one server to another server. In fact the application gets connected to the load balancer which internally load balances the request and failovers the request from one server to another without the user even knowing it.

In this blog I be will using apache web server as my load balancer and will use jobss7 as my application server which will have a standalone mode of clusters of two nodes. The first step is to download and install apache web server from apache site. Use this link to download 2.2.25 installer package, download the version with openssl installer. http://olex.openlogic.com/packages/apache/2.2.25#package_detail_tabs

Once downloaded run the installer and it will install apache web server on the machine. By default apache listens to port 80. So, to check if its working properly after installation or not hit the url http://localhost:80 and you should see the output like below in the browser:-
















This means that apache is installed and working properly. It will also be installed as windows service so that we can use this service when we need to restart the apache web server.

The next step would be to download mod_cluster. User the below link to download mod_cluser1.2 final distribution.http://mod-cluster.jboss.org/downloads/1-2-0-Final. Use the windows version as per your system configuration. In my case i have used binaries windows-x-86. Once downloaded unzip the folder.

To configure apache load balancer we need to do configuration changes at both apache side as wll as jboss side. First lets do the apache configuration.

Go to the modules directory of the mod_cluster downloaded in the previous step. Inside the modules directory we can see lots of .so files. We need few of them to configure mod_cluster with apache. We will need the following files so copy the below files from the modules directory to the apachehome/modules directory.
1.mod_slotmem.so
2.mod_manager.so
3.mod_proxy_cluster.so
4.mod_advertise.so
5.mod_proxy.so
6.mod_proxy_ajp.so

Once the files are copied to the apachehome/modules folder we need to tell apache to load the modules. For that we need to edit httpd.conf file that can be found inside apachehome/conf directory. Open httpd.conf file and add the below entry:-

LoadModule slotmem_module modules/mod_slotmem.so
LoadModule manager_module modules/mod_manager.so
LoadModule proxy_cluster_module modules/mod_proxy_cluster.so
LoadModule advertise_module modules/mod_advertise.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so

Then we need to configure the virtual host. To configure the virtual host add the below entry to the same file:-

<VirtualHost 127.0.0.1:80>
<Directory />
Order deny,allow
Allow from all
</Directory>
<Location /mod_cluster-manager>
SetHandler mod_cluster-manager
Order deny,allow
Allow from all
</Location>
EnableMCPMReceive
KeepAliveTimeout 60
ManagerBalancerName mycluster
AdvertiseFrequency 5
ServerAdvertise On
</VirtualHost>

We are now done with the configuration changes at apache end. Now its time to do some configuration changes at Jboss side. The first step would be create two nodes of jboss applicaton server as demonstrated in the previous blog. For my demo I will be having two nodes running in standalone mode, so make two copies of jboss7.1.0-Final and name them node-1 and node-2. The changes that I am going to explain has to be done at both the nodes of the jboss application server. Open standalone-ha.xml file. The first entry would be to <server> tag by adding the name attribute.

<server name="standalone-node1" xmlns="urn:jboss:domain:1.2">

Next is to add the below entry if its not already there:-

<subsystem xmlns="urn:jboss:domain:modcluster:1.0">
            <mod-cluster-config advertise-socket="modcluster" proxy-list="127.0.0.1:80" sticky-session="false">
                <dynamic-load-provider>
                    <load-metric type="busyness"/>
                </dynamic-load-provider>
            </mod-cluster-config>
        </subsystem>

And finally-

<subsystem xmlns="urn:jboss:domain:web:1.1" default-virtual-server="default-host" instance-id="${jboss.node.name}" native="false">
            <connector name="http" protocol="HTTP/1.1" scheme="http" socket-binding="http"/>
            <connector name="ajp" protocol="AJP/1.3" scheme="http" socket-binding="ajp"/>
            <virtual-server name="default-host" enable-welcome-root="true">
                <alias name="localhost"/>
                <alias name="example.com"/>
            </virtual-server>
        </subsystem>

Add any missing entry from the two configuration if its not already there in your file. The proxy-list attribute should be the host:port where the apache is running. By default sticky-session is true. I am setting it to false sot that the request from one user does not go to the same server everytime.

Thats all we are done with all the configuration changes for both the ends. Remember to add user to both jobss standalone instances as done in the previous blogs. Just run the adduser.bat file and follow the steps.

Start both the jboss nodes and the apache web server after the configuration changes have been made.
If all the configuration is fine you can see something like below screen if you hit the url:- as you can see both the nodes are visible to apache mod_cluster.

















The next step would be to login to the admin console of both the nodes one by one and deploy the same default application as in the previous blogs. Once the deployment is done we can access the applicatoin by hitting the url:-http://localhost/JbossClusterWebApp/













Try refreshing the page multiple times. You can see that the node id changes on each refresh,i.e the request is load balanced on round robin basis to both the servers. Then try to bring the server down on which the request is currently running and refresh the page again. You will see that the node id has changed again. This is the example of failover. All this is being done without the user manually hopping from one server to another.

That is all for load balancing and failover with apache and jboss.

Thanks for reading.




Monday, December 22, 2014

Java/J2EE Blog: Jboss Clustering in Domain mode

Java/J2EE Blog: Jboss Clustering in Domain mode: In last blog we saw how to create jboss cluster in standalone mode. Lets do the same now for domain mode. For clustering in domain we need...

Jboss Clustering in Domain mode

In last blog we saw how to create jboss cluster in standalone mode. Lets do the same now for domain mode. For clustering in domain we need to make couple of entry in some configuration files. First of all we will make entry in domain.xml file which can be found inside jbosshome/domain/configuration/domain.xml file. First of all we will add a new server-group inside the server-groups tag and will name this group ha-server-group which would be using ha profile and ha socket binding group where ha is for cluster enabled. Below is the entry that we need to make to this file:-

<server-group name="ha-server-group" profile="ha">
            <jvm name="default">
                <heap size="64m" max-size="512m"/>
            </jvm>
         <socket-binding-group ref="ha-sockets"/>
</server-group>


Where profile: tells which type of profile is been used (i.e. web, messaging, cluster,full),socket-binding-group: tells which all type of protocols is been used (i.e. web [http,ajp], messaging, jgroups [udp, tcp], full) and server-group : tells what profile is been used and what type of sockets is been used.

After that we need to add two nodes with the name ha-server-1 and ha-server-2 which we are going to use in ha-server-group. The entry will be made in jbosshome/domain/configuraion/host.xml. Below is the entry that we need to add inside servers tag:-

<server name="ha-server-1" group="ha-server-group" auto-start="true">
            <socket-bindings port-offset="100"/>
        </server>
        <server name="ha-server-2" group="ha-server-group" auto-start="true">
        <socket-bindings port-offset="200"/>
        </server>

Next step is to configure a new user using adduser.bat file as done in the previous two blogs.

Once everything is done we need to start the server by using the domain.bat file, however we would not see that the nodes ha-server-1 and ha-server-2 are in a cluster for that you would have to deploy an application which has the distributable tag in web.xml file.

So, lets deploy the same sample application by logging to the admin console:-http://localhost:9990/console. Once the deployments is done click on the add to grops button to add the deployments to the ha-server group. Now try accessing the application on the below URLs:-
http://localhost:8180/JbossClusterWebApp and  http://localhost:8280/JbossClusterWebApp.Then bring any of the server down from the admin console and try accessing the application on the other server.

That's it for clustering in domain mode.

Thanks for reading.


Friday, December 19, 2014

Java/J2EE Blog: Java/J2EE Blog: Clustering in JbossAS 7 in Standal...

Java/J2EE Blog: Java/J2EE Blog: Clustering in JbossAS 7 in Standal...: Java/J2EE Blog: Clustering in JbossAS 7 in Standalone Mode : In my last blog I demonstrated how to get started with Jboss Application Server...

Java/J2EE Blog: Clustering in JbossAS 7 in Standalone Mode

Java/J2EE Blog: Clustering in JbossAS 7 in Standalone Mode: In my last blog I demonstrated how to get started with Jboss Application Server 7 by installing and configuring the jboss server and then ...

Java/J2EE Blog: Getting Started with Jboss Application Server 7

Java/J2EE Blog: Getting Started with Jboss Application Server 7: In this blog I will demonstrate how to get started with JBoss Applicaiton Server. I will download, install, configure and deploy a sample ...

Getting Started with Jboss Application Server 7

In this blog I will demonstrate how to get started with JBoss Applicaiton Server. I will download, install, configure and deploy a sample application to get going. I have downloaded Jboss-as-7.1.0-Final. You can download it from this Link. This will be a zip file. To install Jboss just unizp the file in any directory and the installation is done. Jboss can be run in two modes-standalone or domain. For demonstration purpose we will use the standalone mode. In the root directory i.e Jboss-as-7.1.0-Final is a bin directory. To start Jboss in standalone mode execute the standalone.bat file. This will start Jboss in standalone mode.

Next we need to add a user to access the admin console. To add user execute adduser.bat file inside the bin directory. We need to add a Management User, so input "a" and press enter. In realm leave it blank, input user name and password. New user will be added. Make a not of userid and password as this will be required to access the admin console.




Now execute the standalone.bat file to start jboss server. Once the server is started go to http://127.0.0.1:8080. This will open the web console.




Then click on the Administration Console hyperlink. This prompt you to enter the user name and password. Enter the user name and password entered during the user configuration. Once successfully logged in the Admin Console will open. Below is how the admin console looks like.



Click on deployments on the left hand corner side. Click on Manage Deployments. Then click on Add Content and browse to the location where you have the war file to upload. In my case the name of the war file is JbossClusterWepApp.Once the application is deployed click on the enable button to enable the application. Go to the webconsole and access the application. In my case the URL is -http://127.0.0.1:8080/JbossClusterWebApp/. And you can see your sample web application running.



Thats all to get started with Jboss Application Server 7.

Thanks for reading.