Showing posts with label mongodb. Show all posts
Showing posts with label mongodb. Show all posts

Friday, January 25, 2013

Realization of dream of multitenancy with Google App Engine and MongoDB on cloud.

Multitenancy and the problem of managing the data

Multitenancy refers to a principle in software architecture where a single instance of the software runs on a server, serving multiple client organizations (tenants). Multitenancy is contrasted with a multi-instance architecture where separate software instances (or hardware systems) are set up for different client organizations. With a multitenant architecture, a software application is designed to virtually partition its data and configuration, and each client organization works with a customized virtual application instance. [WIKI]

What if i want to migrate my existing system to any cloud engine?
Move all of code/business logic to app engine, perfect, no problem, but what if my database is already on another physical infrastructure(and which is shredded,etc)?


Of-course app engine/GAE gives you the ability to scale, security, high availability, etc. but with their own terms.You need to use the database which is supported only by the app engine.
Which is one of the major drawback for the GAE and similarly other cloud engines.

I wanted to use MongoDB with Google App Engine(GAE) and could not see the possibility because we can't run MongoDB on GAE and supposed to use Google datastore.

There are few more issues as GAE's highly restricted sandbox. As GAE's docs says your application can only access other computers on the internet through the provided URL fetch and email services (and fewer more ways). Other computers can only connect to the application by making HTTP/HTTPS requests on the standard port. If you want to open a socket from your business logic, its not allowed and GAE will raise an exception.

Few good things happened in this direction, when google annoucned back in 2011 about Google Cloud SQL webservice to support MySQL - https://developers.google.com/cloud-sql/
Some of the other cloud engines have provided more options such as Amazon RDS supports MySQL, Oracle or Microsoft SQL Server database engine. This means that the code, applications, and tools you already use today with your existing databases can be used with Amazon RDS.

I was adamant to move from MongoDB and to use my application with the GAE and mongolab became the savior. Why i want to use my own choice of database because i have already have my own hosted database servers because i want to manage data on my own (security reason, business reason, etc)

Here is the steps how can we use GAE with MongoDB, when your MongoDB instance is hosted on DB cloud.

1) Signup, login and create a database on mongolab


This is going to create mongodb as the database.


2) Create a collection
    Creation of collection (table) can be done by going to database and click on "Add".

3) Open collection which is currently empty and open the API view.
    Connection information on the top of the "Collections" tab is the url which we are going to use interact
    with "mongodb" database created in step1, mongolab has provided REST based methods for database
    interactions.



    At this point we are done with our database part.

    Lets move to GWT(Google Web Toolkit) to create a project.

4) Create a Web Application Project and make sure that "Use Google App Engine" is checked.


    Create a GWT based application, i have used the almost same application which i have created earlier,
    you can refer the steps for the application creation here GWT RPC - Server communication with 
    MongoDB

    The only change here is that the mongodb is hosted on some cloud, and because of that implementation
    for is going to change.

5) Insert a recod and retrive records from cloud database.
    Inserting a record into the databased using the apache's HTTPClient to connect to mongodb hosted on
    cloud, which interacts with the REST based webservice.



    Get all available users from the database.


   As i mentioned earlier about the sandbox security of GAE, i could not use HttpClient directly to invoke
   webserivce and get the data, and had to tweak it bit to make it happen. Shortly going to update about the
  problem as well in this post, which will be describing about GAEConnectionManager.

6) In order to deploy the developed application on cloud, create an an application identifier on app engine.  
    After creation you can see the application as follows:


    Update in /war/WEB-INF/appengine-web.xml and place it under "application" tag.



7) Login to google account from eclipse(you can find it in the bottom left) and deploy the app on the app
    engine.




   And we are done!,  app is available and hosted.
   You can access it from here: http://mongocloudapp.appspot.com/

    I would like to say that overall it is a very nice experience with the app development with GAE and
    without loosing the my own preferred database.

Tuesday, July 24, 2012

GWT RPC - Server communication with MongoDB

Further to the client tutorial in the previous blog, here i am going to present how client communicates with the server with the help of GWT RPC (Remote Procedure Call).
Using RPC which works asynchronously only specific part of the client components can be updated without updating the complete client.














Pre-Requisite:
Java, Eclipse with GWT plugin, MongoDB.



In this example i have extended the previous client to
  a) save data i.e. user data to database and
  b) authentication user

Client can send/receive serializable objects to/from the server. In this example server is going to use that data and insert in into DB, which is MongoDB in our case.


Create Modal
As the client can send serializable java objects over HTTP. Lets create a modal first.


User.java



Create Service
Now we need to have a service which a client can use.
In our case we name it as MongoDBService and it implements RemoteService


MongoDBService.java



@RemoteServiceRelativePath("dbservice") is annotation used for the service identification and calling, which should match with the service path defined in web.xml for servlet-mapping tag.

We need to create Asynchronous service for our MongoDBService, client is actually going to call RPC through MongoDBServiceAsync. All methods of MongoDBService will have extra parameter which is of type AsyncCallback. Client call will be notified when any asynchronous service call completes.

MongoDBServiceAsync.java



Implement service at the server side
Create class MongoDBServiceImpl which extends RemoteServiceServlet and implements MongoDBService. Basically MongoDBServiceImpl is a servlet which is extending from RemoteServiceServlet rather than the HttpServlet directly.



Updating web.xml with servlet details
make sure that all the service which are created with the annotation RemoteServiceRelativePath are added properly for each servlet.

web.xml



url pattern tag path should be formed using module/service.
In this case module name is gwtwebappdemo ( see in GwtWebAppDemo.gwt.xml for rename-to value) and service in dbservice.


Client Service Call
calling service from client





Running application

1) Start MongoDB
    In this case i have started DB without authentication and on default port.
    Default port is 27017 which we have used to connect to database in the code. See DBUtil.java in the
   example.



2) Add user into database.
    Client calls asynchronous call to server, server starts the processing. Once request processing is finished it calls back with the call back handler provided in the request. Client gets call in onFailure in case of failure and onSuccess in case of request processing successful.


    In the server side, server received the User object and sets the data into User collection which is in the mymongodb database using BasicDBObject.

MongoDBServiceImpl.java


3) Check the collection ( table) creation in dbs (database)
    In this example mymongodb is the database and User is the collection which we are using.
    Initially both database and collection is not present, when the first save happens. Both gets automatically
    created.



4) Check User Authentication with wrong input.



Download complete example from here in GwtWebAppDemo_server.rar file.

Wednesday, March 28, 2012

Power of Script writing in the world of JavaScript.

When i was intern, i got a change to choose the technology on which i could develop a tool. PHP or Java. I choose Java, why.... hmmm... its easy to learn, use, understand, scalable (a myth), etc, etc, etc.
Later understood facebook uses php a lot.

Browsed few perl scripts few years back and did not even like it.
Later understood perl developed is easiest in many ways and faster. Faster in execution too.

JavaScript........ahhhh, what is this, i did not like it. Some kind of client side work, which looks like always messy. :-(
Later understood that its not only the client side script any more.

So suddenly my thinking about the script writing, understanding, acceptance has changed. Script programming is amazing.

First got to know about MONGODB. Oh its amazing, it understands java script. We can do all crud
operations with java script syntax. Its a document oriented storage with "NOSQL".

Than checked which mapper can be used and found node.js -> mongoose.


 And tried some hands on with node.js + mongodb + express + mongoose + jade (a template engine). Jade for view, remaining things for model and controller.

 And commonality in all these things are java script is everywhere. Commonality, an important factor for a software product with quality. Of-course there are many point and which i do not want to touch now.
So the point is: 
Server side java script. 
Client side java script. 
Database operations in java script. 
Oh, its heaven --> IT IS THE WORLD OF JAVA SCRIPT.

I am going to take you through some steps which can be useful to start with server side java script. You can download the project from my git repository at


 https://github.com/deepaksinghvi/nodejssample

1) Download Install node from http://nodejs.org/. This installs node and npm ( node package manager).
    npm is used to install all the dependent modules, like mongoose, express, jade, stylus, etc.
2) Next install all the dependent modules:
    cmd prompt> npm install mongoose express jade stylus
      or you can do one by one also.
3) Now lets write few java script files
     a) load all the required libraries which we have just installed and create an application server.


     b) create model and controller scripts.

         Creating a model (models/model.js).
The controller part normally known in the node.js development worlds as routing, i have kept in the main script file only ( which is app.js).
Routing of creation of a new user. This creates a new user and saved it into the database.

User clicks on "Create User" button and it request gets routed to above mentioned script.

Once user gets added than listing of users shown from the table ( known as collection in the world of mongodb) or it can be shown if user clicks on "Show Users" link in the above image.


4) Enjoy the sample application.
    To start the application:
    a) Start database
         mongodb/bin>mongod --dbpath /data/db 
       
    b) Start sample application
         nodejssample> node app.js


I tried to provide a very simple sample to start with node.js and mongodb. Its a perfect match for a rapid web based development. 
WebSocket also looks very promising, a lot has been written about it. I would like to try some of the exercise on that and will be sharing that as well in the next blog (hopefully).



Heroku Custom Trust Store for SSL Handshake

  Working with Heroku for deploying apps (java, nodejs, etc..) is made very easy but while integrating one of the service ho...