Skip to main content

Posts

Showing posts from July, 2008

Reentrant Locks - Synchronization on multiple Conditions

After looking at a way you can do a waited lock on a synchronised monitor in java, it needs a much more object oriented way of handling multiple scenarios.

The sychronised-wait-notify paradigm works if you wait and notify on the monitor based on one condition (wait before borrow if not available). The Give back doesnot wait ever, It just gives back. The pool kills any over flow. What if you need to put a conditional wait on that too? something like (give back only if object pool is not full and WAIT until space is available)

Here is where you have Java 5's Lock - Conditions paradim comes handy. A lock is objectification of the monitor, Threads that need to be synchronised on one monitor is can go through Locks instead. However, Locks are more useful if you have multiple conditions on which you what these threads to wait and be notified.

Take the above scenario, which is the case of a Bounded buffer where there are two conditions on which threads need to wait, empty and full. If b…

Public and Private JRE

This one is basic. Newbies in Java are overwhelmed with too many techy terms. Most know JRE, but some find it hard to understand the notation of a Public and Private JRE.

A JRE is nothing but a folder that contains JVM executables, setting files, required lib files, and extensions. You can install multiple JREs on your system Sun generally installs versions in C:\Program Files\Java as jre eg.jre1.6.0_06.

A Public JRE is available to all Java programs, Browsers, and the libs in this JRE folder are available to applications started in command line like
java com.sarathonline.cli.HelloWorld
This JRE is also registered in path, and Browser plugin.

A Private JRE is something that is installed in the system by is not refered to by default. This could be a copy of the JRE / JDK folder from another installed directory. If you see a my earlier post:  starting eclipse with jre 1.6 where your environment is in 1.4, The JRE1.6 is used only by Eclipse, This is a private JRE. While the JAVA_HOME po…

Java Object - wait - notify - a practical example

Object is the super class of anything in Java. But, Most people fear the wait and notify methods in Object. The common java practitioner might not need to use these for all practical purposes. However, the fear is not because they are hard to understand, but because of the assumption and false notion (generally pushed from the *experts* to the newbies) that these are for *super users*. The easiest way to understand this is by example. Most examples include Produce Consumer, But in practice where is this applied?

Some time back I wrote about Commons Pooling. Ever wonder how an Object pool Implemention, like this one, would time out on a blocking call without creating a new thread on its own? It uses a wait - notify[All] paradigm.

To understand that Look at the source code of the said GenericObjectPool.

The borrowObject method (line 911 - so apt??) which optionally waits for a maxWait long secs, is used like for (;;) { synchronized (this) { // Some Logic try { if (_maxWait…

Using Java 1.4? Can't Use Webtools in Eclipse Ganymede?

At our new client, we still use Java 1.4 and I found that there is a problem installing Web tools in Eclipse 3.4 (Ganymede). The P2 Installer shows WST is installed, but I cannot see Server profiles, J2EE perspective, anything related to it.

Here is what I installed, as shown in P2


But I cannot see J2EE related stuff in Perspectives, Preferences or New windows


To resolve this, I installed a private 1.6 JRE. Started Eclipse with
eclipse -vm c:\java\jre1.6\bin\javaw

And it works!

Inspired by Randy Pausch

Yesterday's Google Blog had posted
[Randy Pausch, a professor of computer science at Carnegie Mellon University and a good friend of Google, passed away last night. In addition to being recognized as a pioneer in virtual reality research, he became widely known as a gifted teacher and a mentor to many. Millions of people saw his inspiring "Last Lecture" on YouTube. Read more about Randy and his contributions on our Research Blog.]
So I watched the video. And the related movies. Very Inspiring! I made a small playlist. Just play this, It will loop to finish with the actual lecture!

R.I.P Dr. Pausch

OpenSessionInView Filter, Sessions, Transactions and Connections

By now, I have worked enough on OpenSessionInViewFilter, Hibernate and Spring in some projects.

Hibernate 3 uses Lazy Loading by default. So, We use for maintaining lazy loaded collections to display in view. But, lately, as I mentioned in earlier posts, we were having some issues with connections that were not returned to pool.

To understand where connections were held up we need to see how OSIVfilter, Session, HibernateTransactionManager and Spring work together - Where and when connections are picked and released. OSIVFilter guarentees that each request will have one and only one Hibernate Session. A Session is a lightweight abstraction of data in the database pulled into vm converted as objects. So for a session to fetch data from db, it needs connections. This connection is available from the datasource through a TransactionManager. In general, if you have one database, You would use Springs HibernateTransactionManager.

A Session generally contains one Transaction. However, In OSIV…

Resource Contention in JEE for a layman

In terms of scalability, a frequent term used is "Resource Contention". Most commonly this means, there is a shared object, file handle, db connection, or similar for which Threads are trying to get ownership. Until they obtain this ownership they either wait or exit on a timeout mechanism. In normal flow, they simply wait - This state is called blocked state. In an asynchronous mode (generally meaning a spawned child thread), there can be a Time-Out flow that can be defined to control the maximum length of time the thread can take to complete a task.

In a blocked state, There may be "Resource Contention" in one thread that is avoiding other threads to proceed. In a JEE environment, This Resource Contention is caused commonly by Transactions(, Synchronized blocks, Locks). Since User Threads are discouraged in JEE, Synchronized blocks and Locks are less often encountered as issues.

That's theory. If you dint understand much, Dont feel despair. I come from the ver…

Spring Security Advisory

Many people consider this is not new. But there is a security advisory out for spring users.

http://www.springsource.com/securityadvisory

Quote:
For applications that bind request data to presentation-layer "form models", this is unlikely to be a problem since there is a one-to-one correspondence between a form backing object and a set of allowed request parameters. This issue is only relevant for applications that bind directly to a domain model which exposes properties that should not be updated by the client.

To understand it more clearly, look at an Example:
http://www.celerity.nl/blog/2008/04/security-implications-of-the-spring-databinder/

This is however an advanced usecase. If you are directly binding model to form objects you have a major design issue.

If you observe carefully, There is another case that we dont often remember. Most Web apps have CRUD jsps and the Update jsp will generally include the id of the model that is being edited. Most times, The only security is th…

Bright Side of High Gas prices

Look ma, finally the world understands - Its easier better to cruise than break hard after a speed rampage.

For all the better things, Look at the green side of high gas prices. People are talking thinking about mass commute or Car pooling. Resulting in Less Traffic. Lesser Accidents. And Much lesser pollution. Car manufacturing companies making cars not for *speed* but for *green*. Revolution and research in alternative fuels. Good Excuse for using your old bike to ride locally :). In turn improving your health!

As they say, Nature does take its course;

Moving On - A Consultant's journey

I am moving on to a new client. Consulting, by nature, is a good thing. You get to learn a lot. Having said that, Projects get over, You move forward to other things. Its hard some times, emotionally. You get attached to people around you. and Then You see you have to go. I am strongly attached to this team.

This a consultants life. I know. Even though I understand the practical ramifications, I still feel a little bad about *going away*. I am gonna miss the team for sure. So as a consultant I am supposed to reach an agreement with this fact. I am single. I can. But What when I get married? How will my fiance take it? How about when I have kids? I cannot dare imagine what would be going through my kid's mind if I do this to him.

So My advice is, Keep Consulting until you get married. You can consult part time after, but stick to a good old full time job.

My consolation for now is that I can be looking forward to better things. And I hope there will be. This gets me excited at the …

Using Commons Pooling for Objects in JEE

A little more than 3 weeks ago, I was asked to rewrite a Multi-threading component that *pooled* connections to Mainframe HTTP Gateway. There was an existing implementation with synchronized array of connections which was initialization with a configurable size. The problem was this implementation was home grown way back in history, and the confidence level in that code was very low. Especially because its going into a JEE container and code-managed threads were not scaling atone to the container.

My first instinct was to use Apache Commons pooling. For one, this would have been much more QA-ed than what I would have implemented. The primary logic would be approximately the same, create-pool > borrow-object > return object. Everything could be done with commons pooling, using the GenericObjectPool.

On a finer level, I need to manage the life of the objects themselves - make sure the encapsulated connection is not stale etc, in which case invalidate it
A sample example (as in JavaDo…

Maven - Is it worth the hype?

I am glad I entered a (somewhat) heated argument with a friend of mine about maven and its' usefulness. My stand was that maven saves developers' life. His stand was otherwise. The points I had were, quick start, arch types, version-ing in repo and a good M2 eclipse plugin. My friend however still believes ant is the best thing ever happened to Java community. He would rather deal with the jars himself than excluding every conflicting jar manually. That lead me researching.

For me the selling points in maven are
With Maven archetypes, You can create a project (template) in one command. And voila you are good to go. But this is not a very big selling point, because most times, you just create once. Maven also gives you a way to create a project for different IDEs automatically. This one is very nice, coz you need not check in the classpath and project (config) files into your source control. Also, A new user does not need a setup help since the project structure is familiar and I…

Firebug dom css come Default

Having installed FF3, the initial migration was hard on me. Firebug which is a daily life saver, was missing.

It was just days, I got Firebug 1.2.03beta. Life is back to normal. Infact a lot better, because Now, just to walk the dom, look at css, or edit html&css on the fly, I donot need to *enable* firebug, These three features come standard.


Console, Script and Net features require you to *enable* them seperately. This drastically improves performance of my FF. Now I don't need to make FF heavy during designing css.


Love you Firebug!

YUI Compressors

Easy things first! As you know, I have a back log of posts that I have to dump out of my brain into this blog. I will start with the easiest.

YUI Compressor. This is a freeware from YUI. I was looking at the MultiThreading in JS post in infoq.com and found that this Concurrent.Thread.js is around 500KB. Which is quite big for a js file. And imagine the poor vistor of my site from Bugaria on a dialup with 56 Kbps line, This page is as good as a download! So, What can be done? 1. Compress the JS, 2. Then Gzip the data transfer. Well The second option might not be in *your* hands as a developer, the first is always your decision.

Using YUI is absolutely easy. Download the jar file and run
java -jar yuicompressor.jar myjsfile.js

And you got a compressed and obfuscated file (would you not want this! The users can no longer steal your js code!). You can also do this to your CSS files. There are other compressors like dojo, JsMin but this one beats them all.

Google Bookmarklets

Some Times It is easier to just click a bookmark and *put it in google*. Bookmarklets are small javascripts that run to *do something* which would other wise take user intervention. These are a few google bookmarklets I use. I will make a set of features in sarathonline.com to have these too!! To learn a more about bookmarklets - see this wiki

Note Book - Note This
Bookmarks - Bookmark
Translate to English - Translate
Calender - Add event
Maps - Map This
Define a word - Define This

Org post, continued

Back from time off.

I just came back from a personal time off, I have been researching on so many things, just dint find time to blog it.

Here is what I have been looking into and so much still left to read. In the coming weeks I will wrap these readings. Concept of Actor Model. Scala, its eclipse plugin, its web frame work lift. Multi Threading in Java Script. YUI Compressor. Scrum Agile and XP.

I have also been improving contacts in my linkedIn profile. If I missed you, Please add me to your network.