0

December 17

Grizzly Comet - what’s grizzling with comet?? - Part 1

Posted by Ajay

I have been working on implementing an interesting application to manage all the  photographs (I will talk more about this project in a later post). In the process, I have been thinking about a feature I want to add to my application which involves alerting users using my web application to be alerted as and when new additions are made to my photo repository. I came across Grizzly Comet and felt an inane desire to try this out on my application, so here is my technical knowledge sharing about this experience

Let’s talk NIO

Before we talk about Grizzly and Comet, let’s try to understand where this whole concept started. Java 1.4 introduced a collection of Java programming API’s called New I/O with features for intensive I/O operations and was developed as part of JSR 51. The main purpose of NIO is to have an implementation that would allow use of the more efficient underlying platform implementation of I/O. A single thread can operate on a bunch of connections instead of having one thread per connection, which provides high degree of performance. This API provides variety of features that include buffers for primitive data types, character set encoders and decoders, pattern matching features, channels. Details about NIO is probably for another post.

Introducing the “Comet” philosophy

Another interesting concept is what is called Comet application model. The basic idea is that a web server would push HTTP data to a browser without the browser requesting the data. The Comet approach typically uses Ajax with long polling to achieve it’s task.

A simple diagram to illustrate the Comet methodology as opposed to the web application model is as below

Web applications have evolved through various stages, as depicted in the following diagram

In the Page by page model, that most web applications started off from and a model that many still follow, every new browser page would send a request to the web server. So essentially we had to refresh the whole page every time a small bit of data on the page changed, which started to become a real pain.

Thus began the era of Ajax where any new information would take the form of Ajax requests to the server to update the web page asynchronously. Web apps would poll the servers periodically for new information, but still the point of the matter is that users have to periodically hit the server to request information.

Finally Netscape introduced the concept called web server push that has evolved into the comet programming model.  Essentially imagine having the benefit of server to client messaging without issues of fat clients.

Imagine a real time event like a baseball game or the stock market scenario, when users would want to be kept informed of events happening during the game or certain stock market events. Such scenarios cannot be implemented using traditional web application methodologies. This is where Comet or Reverse Ajax methodologies fit in.

Comet is a collection of technologies that provides the functionality of web server push through persistent HTTP connection. Comet can be implanted using Streaming where the browser opens a single persistent connection to the server for all server events or long polling.  Many applications use the comet model, some of them include Meebo, Gmail chat, Jotspot, ICEFaces JSF framework. The Comet approach involves a departure from the usual web based platform approach. It may involve storing some kind of state information about the clients who wish to receive notifications on the server., similar to messaging systems. This is the reason most Comet based approaches rely on custom adapted application servers. In Java Jetty and Grizzly have support for Comet based approaches. This type of design is also being introduced through the continuation concept in other platforms. But how does the browser itself stay in contct with the server applications? Some approaches include long polling, dynamic script tags and Iframes which are all non standards. Many client side Comet designs rely on frameworks to iron away incompatibilities, Dojo for example is an Ajax/Comet implemntation

What is Grizzly?

Writing scalable server application has been a big task always. Threading issues caused issue with scaling. This prompted Grizzly to make it’s appearance. It is a HTTP Connector  based on NIO that ships with Glassfish. It is designed to replace Apache Tomcat’s Coyote connector. All Java based web connectors have scalability limited by the number of available threads.  This is where Grizzly improves by providing plug ability of any kind of thread pool.

Grizzly essentially is based on a task based architecture where each task represents an operation. Every task executes on it’s own thread pool or a shared one. The main entry point is Pipeline which has nothing in common with the Catalina Pipeline. The Grizzly Pipeline is essentially a Thread Pool Wrapper and is responsible to execute a task.  The SelectorThread is another important component where the NIO selector is created.  When processing a request, the SelectorThread will create Task instances and pass it on to the Pipeline. There are three types of Tasks - Accept Task - to handle NIO OP_ACCEPT, ReadTask/AsyncReadTask/ReadBlockingTask - to handle OP_READ, Processor Task - to handle OP_WRITE. This SelectorThread can either create one Pipeline per Task or share a Pipeline among tasks.

Grizzly has introduced Comet support and is implemented on top of the Asynchronous Request Processing extension of Grizzly.

So thats that for my technical sharing session for today, in my next post I will give a brief intro to the Comet API and talk about how I used it in my project. Ciao, for now!

References

http://alex.dojotoolkit.org/2006/03/comet-low-latency-data-for-the-browser/
http://weblogs.java.net/blog/jfarcand/archive/2005/06/grizzly_an_http.html
http://www.pathf.com/blogs/2006/06/infrastructure_/
http://searchsoa.techtarget.com/tip/0,289483,sid26_gci1301487,00.html
http://weblogs.java.net/blog/jfarcand/archive/2006/01/introduction_to.html

Tags: , ,

 
0

December 3

An Introspection of the JSR 135 Specification

Posted by Ajay

Servlets in Brief

A Servlet is a Java technology that allows a container to generate dynamic content. These containers are called servlet containers. Tomcat is one good example of such a servlet container. The basic Servlet interface defines a service() method to handle client requests. The web container handles concurrent requests to the same servlet by concurrent execution of the service method on different threads. A servlet will also have a variety of methods like doGet, doPost, doPut, doDelete and doHead which is a specialized version of doGet returning only a header. The use of a SingleThreadModel interface will guarantee that only a single thread will execute in a given servlet’s service method.

Most Java web frameworks build on the Servlet API and require configuration of Servlets, Filters and Listeners. The JSR 315 will be a part of Java EE 6

Some of the proposed changes

  1. Support for continuations. The general idea of continuations is that you can save the current state of an object and return back to the saved state at a later point of time in your execution lifecycle. It is almost like taking a hiatus from your hectic work schedule, going on a trip around the world and coming back to find that everythingis as you left, no catching up, no making up work, just consitnue on as you were before your hiatus, how cool would that be. This is a concept that was popularized by Scheme and also is a major part of Jetty as Jetty Continuations. Now with JSR 135, the request object will now have a suspend() and resume() to suspend and resume execution from a later thread.
  2. Plugability - Today all deployment artifacts are defined in web.xml, which can at times just become large and unmanageable. So multiple frameworks can lead to having us define all the servlet mappings in this web.xml file. But imagine if each framework coems withit’s own web.xml that defines only the components that framework needs, and if we leave the job of assembling the components together to the container, our task would be so much easier, and our project would be so much easier to maintain. This spec introduces web fragements that does exactly this task. In addition to this kind of plug ability the new JSR also provides a feature where servlets can be inserted into the container dynamically at runtime using the servletContext.addServlet() and servletContext.addServletMapping(). One drawback of this feature is that there could be potential security risks since JARS can register their own servlets.
  3. Annotations - I am sure many of you are familiar with annotations, a technique to try to eliminate configuration files by introducing the configuration details in the source code file. JSR 135 introduces POJOS for servlets, so no more extending from HttpServlet. Just use the annotation @Servlet(urlmapping = {’mapping‘}, name=“abc” } and then use @GET, @POST, @PUT, @DELETE, @HEAD. Servlet Filters are defined using @ServletFilter and @FilterMapping. Unfortunately this Annotations methodology is not as hunky dory and cool as it looks, there are lots of potential problems that Roy van Rijn has mentioned on his post on theserverside.com which include potential issues with having two methods with the same annotations like @GET, there are no IDEs that are setup to detect Servlet using annotations. Also with annotations what we lose is that previously we could just override the methods, now you cannot do that.
  4. Programmatic entry - Potentially there might be support for programmatic login and logout as part of the HttpServletRequest and HttpSession.
  5. HttpOnly Cookies - Ability to prevent client side scripting code from getting access to cookies by defining HttpOnly cookies.

References:
http://www.theserverside.com/tt/articles/articles.tss?l=PonderingAboutJSR135
http://weblogs.java.net/blog/mode/archive/2008/04/servlet_30_jsr_1.html

Tags: ,

 
0

December 2

A GIS story - Introducing caching for GIS map rendering

Posted by Ajay

Once upon a map…

I have been working on geoserver for the past few days and it has been an amazing experience. Let me tell you the circumstances that led me to investigate geoserver.

We have a system that had web mapping capabilities. We provide decision support service and our GIS map enables us to visualize the service that we provide to the customers. Users of our product will be able to see multiple layers on the map, visualize many diffrent layer features and use this to make certain decisions for managing emergency. Our technical setup involved using PostGRES/PostGIS to serve spatial data, a custom built WMS/WFS server that would consume the spatial data and render geotagged PNGs, and a Mapbuilder/OpenLayer based client that would take the WMS/WFS data and render a map on the front end web page. Our setup looked something like this:

One fine day our client wanted us to design some really cool basemaps, and so there we were in the next step of the evolution of our application, We got our GIS designers to produce awesome looking geotiffs, all looking nice and sexy. But one thing about these geotiffs files was that they were huuuuge!!. Some of them at our highest level of resolution were running into 4-5 GB. Then came the next step of setting our WMS/WFS server to render these geotiffs. Using a GeoTiffReaderWriter, we were able to successfully accomplish this, but with a great drop in performance when rendering the images on the UI. So the think tank got together to solve this problem, and a brilliant young engineer (oh thats right that was me!!) of course with some help from others came up with an interesting solution to cache the WMS/WFS server.

We decided to use some of the functionality of a great open soruce caching APi called GeoWebCache. We could not use the API directly since our WMS/WFS server could not send request into geowebcache in the format that it supported (This basically tells you that we were not following the WMS/WFS specification…how cruuude!! …this of course will be remedied in a few weeks when we switch to geoserver..but thats a story for another day)

Who dat wanna cache some maps!!

Web caching is a concept used to improve server performance. The general idea of GIS web caching is to cache extents that would be executed very often, and which entitles high overhead in terms of network bandwidth, and then retrieve these extents from the cache instead of a WMS query to the server. Web caching can be implemented only for layers that do not change often. If they do change, a cache expiration policy can be implemented.

Geowebcache (http://geowebcache.org/trac) is a WMS tiling caching client that works to cache WMS data. In essence what geowebcache does is that it acts as a proxy between the client and the server helping to divide dynamic maps into static tiles. It also takes requests for each of the tile , checking to see if a particular tile that is requested is already stored on disk (the cache) if so it returns that instead of hitting the server. GeoWebCache not only turns any dynamic mapping server in to a high performance cache for the WMS-C recommendation, but the tiles are also instantly available for direct use in Google Maps, Virtual Earth and as Super-Overlays in Google Earth.

Other features include pluggable caching backends, like JCS and a pure disk cache, and support for ‘MetaTiling’ for better labels across tiles.

It works somewhat like this:

  1. GET request is intercepted and the layer requested is extracted and checks to see if the layer supports the request.
  2. The bounding box or envelope of the current request is extracted and an internal grid of the form (x,y,z) is created. This grid forms the index. Any further requests, if they fall i n the range of this grid, would make it a cache hit.
  3. Once the grid is created a cache key is generated for the grid. If this cache is empty, the WMS server is hit to populate the cache, if not the image is rendered straight off the cache.

Now to visualize it,

So all in all our experience with geowebcache was incredible. We got an almost halving of our rendering time for our basemaps, and let me tell you, our clients were Haaappyyy!!.

Some good references

http://cholmes.wordpress.com/2008/03/05/letting-everyone-remix-web-maps/

Tags: ,

 
0

November 23

transactions in a Spring context - Take 1

Posted by Ajay

Spring as a framework has made a lots of things much simpler for developers. Transactions is one of the area that Spring has simplified a lot. Have you ever wondered how the nitty gritty details of transaction worked in Spring? Well let me share some of what I learnt

What is a transaction?
A Transaction is defined as an indivisible unit of work. It always follows the ACID properties.

  • Atomicity- all or none principle - complete the transaction in it’s entirety or do none of it.
  • Consistency – do a drew brees (:-P ) - as you go from one state to another, data integrity should be maintained
  • Isolation – transactions should not effect each other
  • Durability – persistent state should be maintained.

A transaction can either end as a commit or a rollback.

Transaction models

There are many different transaction models available: Local transaction model, programmatic transaction model, declarative transaction model.

  • Local transactions are resource specific, for example a transaction associated with JDBC. The local resource manager handles these transactions. With this model the developer will manage the connections to the transaction manager. JDBC transactions are controlled by DBMS’s transaction manager. The Connection object supports transaction demarcation. JDBC has auto commit turned on by default, so commit is done after each SQL statement executes. The problems with this model is that it introduces developer errors when coding the connection logic.
  • Programmatic transaction model leverages JTA. The developer writes code to manage the transaction and not the connection as in the local transactions. The UserTransaction interface  comes into play in EJBs and uses the begin(), commit(), rollback() functions. Spring uses TransactionTemplate to perform the same task with the execute() method that takes a TransactionCallback object. The main disadvantage of this method is that lot of responsibility rests on the shoulders of developers to make sure they handle the transaction semantics properly.
  • Declarative transaction model is commonly known as Container Managed Transaction in the EJB world. The general idea behind this is that the container will manage the commit or rollback of the transaction, the developers will only to tell the container how to manage the transaction through configuration parameters – ejb-jar.xml in case of EJBs or ApplicationContext.xml in Spring.  In Spring we use the TransactionProxyFactoryBean to implement this transaction model. EJB3.0 introduces the use of annotations for this transaction model. We also need to tell the container how to manage the transaction. This is done using a TransactionAttributeSource bean associated with a transaction attribute which has 6 different settings- Required, Mandatory, RequiresNew, Supports, NotSupported, Never

When do we use programmatic transaction model?

Usually programmatic transactions are useful when user we have client initiated transactions. If a client is making multiple remote service calls for a request, then transaction is maintained on the client side.

Another reason to use this methodology is if using JTA is very expensive in terms of performance. In such cases where we might need to squeeze every ounce of performance, so we might decide to use JTA only when absolutely required. This is called localized JTA transactions.

Another scenario is when there are long running transactions. In EJB’s these are handled using stateless session beans, where you might have multiple stateless session bean methods as a part of a single transaction.

For all other situation is is advisable to use the declarative transaction model.

JTS and JTA

Now we need to understand the difference between JTA and JTS. JTA is the interface developers use to manage transactions. The JTS or Java Transaction Service on the other hand is an underlying service that implements JTA and provides that to application servers. So essentially JTA provides the interface and JTS is the implementation similar to the relation between JDBC and database drivers.

Tags:

 
0

November 11

Apache Con 2008 - Take 2

Posted by Ajay

Another great session in the conference was a session on the way Apache’s community is organized. Any software community has the following different approaches

  • benevolent dictator approach - the basic idea here is that one person develops most of the product, providing some degree of control to other users, but all code submissions goes through the single person. Linux development follows this model. It provides quick focussed development, there is no problem with bike shedding ( a concept where infighting on development methodologies can break up a group ), lower committer pool, new input and ideas are restricted by the dictator’s interest.
  • going it alone - This provides each developer complete control, crystal clear development methodology. It has a higher chance of abandonement and burnout. Sourceforge uses this methodology.
  • meritocracy - Apache Software Foundation uses this methodology. It follows a principle based on Rome. Very few people are citizens by birth. Most achieve citizenship through a merit based model, by performance in society like being a soldier or gladiator. This methodology focusses more on community and voting, allowing developers to get more responsibility based on their performance in the community.

The second day also had couple of interesting sessions on OSGI. One session was on how the Tuscany project used the felix framework to convert it to an OSGI based model. The session was informative in general, but it gave a general idea that there is too much of dependency hell that accompanies the conversion to OSGI. This could be a limiting factor in complete adoption of Felix.

Another very interesting session was that given by Sam Ramji about Microsoft’s  path into Open Source. It started off with a funny crack on how Berkeley is known as the People’s Republic of Berkeley, simply because it is a huge melting pot. Microsoft has been instrumental in many open source projects, some of them include POI - Poorly Obfuscated Implementation, HBase - Hadoop Database, AMQP - Advance Message Queing Protocol, NetMon, Azure - Microsoft’s cloud computing alternative and a language called M. Microsft’s Cliff Schmidt has also been involved in a social project called Literacry Bridge project to help poor children to learn without a laptop. They have come with an idea of a talking book to introduce certain concepts.  This project can be checked out at http://literacybridge.org. This was an interesting session but still raises the question as to whether Microsoft is truly open source by heart?

Tags:

Copyright © 2010 “An Image of My Life” blog series All rights reserved. Theme by Laptop Geek.

Total hits: 59981