Excited about Sonar's new features to set Architectural rules in version 2.4, and upcoming Track violations over time, differential views, manual code review in 2.5.
This surely will make my life little easier when doing daily code reviews.
http://www.sonarsource.org/roadmap/
http://www.infoq.com/news/2010/12/sonar-2.4
Thank you sonar source
Thursday, December 30, 2010
Wednesday, December 22, 2010
Java Autoboxing - Auto-unboxing
Java 1.5 onward auto-boxing and auto-unboxing were added. This allows you to covert primitives and boxed types primitives back and forth with simple assignment vs calling special functions or constructors.
Lets look at some examples.
Here are some tips based on reading from Effective Java 2nd edition.
Lets look at some examples.
int primitiveInt = 1000; //primitive int
Integer wrapperInt = new Integer(1000); // wrapper Integer, boxed type primitive.
Integer autoBoxedWrapperInteger = 1000; // Autoboxing, primitive is autoboxed to Integer, an
//Object is bing created
int autoUnboxedPrimitiveInt = wrapperInt; //Auto-unboxing, primitive is being assigned a value by auto-unoxing//Comparing a primitive to boxed type, boxed type is autoboxed to primitive so the results is
// two primitives are being compared using === as opposed to 2 Objects are being ref compared
// using ==
System.out.println("primitiveInt == wrapperInt :" +
(primitiveInt == wrapperInt ? true : false));
//Two object references are being compared using ==, therefore the result is false
System.out.println("wrapperInt == autoBoxedWrapperInteger :" +
(wrapperInt == autoBoxedWrapperInteger ? true : false));
System.out.println("primitiveInt == autoBoxedWrapperInteger :" +
(primitiveInt == autoBoxedWrapperInteger ? true : false));
System.out.println("primitiveInt == autoUnboxedPrimitiveInt :" +
(primitiveInt == autoUnboxedPrimitiveInt ? true : false));
Output
primitiveInt == wrapperInt :true
wrapperInt == autoBoxedWrapperInteger :false
primitiveInt == autoBoxedWrapperInteger :true
primitiveInt == autoUnboxedPrimitiveInt :true
Here are some tips based on reading from Effective Java 2nd edition.
- Prefer primitives over boxed primitives.
- When you mix primitives and boxed primitive in single opeartion - boxed-primitive is auto-unboxed.
- When mixing primitives and wrapper types in expression watch out for "==" comparision.
- Performance hit due to autoboxing and unboxing, a new object is created when autoboxing is performed.
- Unboxing can throw NullPointerException
Wednesday, December 15, 2010
HTTP Notes
Keep alive/Persistent Connections : HTTP 1.1 introduced keep alive to keep the connection between web client and server alive and reuse it to serve subsequent requests. In HTTP 1.0 a new connection had to be created for serving each request.
Servlet 3.0 Asynchronous Processing
Came across a great post on Servlet 3.0 asynchronous processing at http://www.javaworld.com/javaworld/jw-02-2009/jw-02-servlet3.html?page=1
Following are some notes based on the post.
Important thing to note is that it is not a fire and forget mechanism, as in put a request on a queue and return the response right away. The web client has to wait however long the request processing take place.
It is a way to free up request handling server threads from long running code. Instead the work in offloaded to be handled by manually created thread(s) by application code.
Client response stream can be open for extended period of time, and server can write to response to implement "push" model.

Following are some of the important methods from ServletRequest API from Servlet 3.0 .
Gets the AsyncContext that was created or reinitialized by the most recent invocation of
boolean isAsyncStarted() Checks if this request has been put into asynchronous mode.
boolean isAsyncSupported() Checks if this request supports asynchronous operation.
AsyncContext startAsync() Puts this request into asynchronous mode, and initializes its AsyncContext with the original (unwrapped) ServletRequest and ServletResponse objects.
AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse)
Puts this request into asynchronous mode, and initializes its AsyncContext with the given request and response objects.
Following are some notes based on the post.
Important thing to note is that it is not a fire and forget mechanism, as in put a request on a queue and return the response right away. The web client has to wait however long the request processing take place.
It is a way to free up request handling server threads from long running code. Instead the work in offloaded to be handled by manually created thread(s) by application code.
Client response stream can be open for extended period of time, and server can write to response to implement "push" model.
Following are some of the important methods from ServletRequest API from Servlet 3.0 .
AsyncContext getAsyncContext()Gets the AsyncContext that was created or reinitialized by the most recent invocation of
startAsync() or startAsync(ServletRequest,ServletResponse) on this request.boolean isAsyncStarted() Checks if this request has been put into asynchronous mode.
boolean isAsyncSupported() Checks if this request supports asynchronous operation.
AsyncContext startAsync() Puts this request into asynchronous mode, and initializes its AsyncContext with the original (unwrapped) ServletRequest and ServletResponse objects.
AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse)
Puts this request into asynchronous mode, and initializes its AsyncContext with the given request and response objects.
Wednesday, November 17, 2010
HashMap Implementation notes
Implements Map interface
Permits null values and null key
None of the methods of HashMap are synchronized, so if you are adding or deleting keys - you must synchronize in multi threaded environment. Synchronize on some object, if external object is not available use Collections.synchronizedMap(new HashMap())
Collections.synchronizedMap(new HashMap()) synchronizes all map operations using a internal mutex. However synchronization every single method is not enough, some client side locking is also required.
Iterators of HashMap are fail-fast when underlying map is modified (add/delete key) - ConcurrentModificationException is thrown.
Ordering of keys is not guaranteed to be same over time.
Access for get and put is time constant.
Max Capacity ~ 1.07 billiion
Default Initial capacity = 16
Uses hash function to disperse keys around.--
Uses Array to store elements.
Try and provide capacity as part of initialization.
Permits null values and null key
None of the methods of HashMap are synchronized, so if you are adding or deleting keys - you must synchronize in multi threaded environment. Synchronize on some object, if external object is not available use Collections.synchronizedMap(new HashMap())
Collections.synchronizedMap(new HashMap()) synchronizes all map operations using a internal mutex. However synchronization every single method is not enough, some client side locking is also required.
Iterators of HashMap are fail-fast when underlying map is modified (add/delete key) - ConcurrentModificationException is thrown.
Ordering of keys is not guaranteed to be same over time.
Access for get and put is time constant.
Max Capacity ~ 1.07 billiion
Default Initial capacity = 16
Uses hash function to disperse keys around.--
Uses Array to store elements.
Try and provide capacity as part of initialization.
Monday, March 29, 2010
Executing Asynchronous Actions in a Web Application
Web applications need to provide instant response to end users, however not all requests can be served instantly due to a variety of constraints on infrastructure the application must live with. In an enterprise web applications there could a variety of long running tasks initiated by user request. To handle such task, the request could be queued for later processing and user is presented with a message to check status at a later time. For some requests a part of necessary processing could be done instantly and remaining could be done at a later time.
Queuing a request requires adding extra infrastructure to web applications. Request could be queued in a single instance of share data structure across the application. Application threads running in background can read the queue data structure and act on queue messages. Rather than writing this framework/infrastructure enterprise class open source libraries could be utilized. ActiveMQ provides a robust queuing solution and Apache Camel provides processing queued actions. Both libraries can be used in embedded or standalone fashion.
Architecture:
ServletContextListener initialize the ActiveMQ Broker and CamelContext, queues and routes are setup.
Have web action/controller write to ActiveMQ queue.
Write classes/camel routes to read from queue .
Queuing a request requires adding extra infrastructure to web applications. Request could be queued in a single instance of share data structure across the application. Application threads running in background can read the queue data structure and act on queue messages. Rather than writing this framework/infrastructure enterprise class open source libraries could be utilized. ActiveMQ provides a robust queuing solution and Apache Camel provides processing queued actions. Both libraries can be used in embedded or standalone fashion.
Architecture:
ServletContextListener initialize the ActiveMQ Broker and CamelContext, queues and routes are setup.
Have web action/controller write to ActiveMQ queue.
Write classes/camel routes to read from queue .
Labels:
ActiveMQ,
Apache Camel,
architecture,
asynchronous,
web application
Friday, February 19, 2010
uuid
UUID - Universally unique id is a 128 bit is a reasonably unique value. UUID was standarized by Open Software Foundation and Distributed Computing Environment. Purpose of UUID is to generate a universally unique identifier without contacting a central identifier generation system.
UUID are relavant to distributed computing, each machine can have 10 million UUID per second as per IETF REFC 4122 . Implementation for generating UUID are widely available in various programming language libraries and in DBMS.
JDK 1.5 provides classes to generate UUID.
UUID are relavant to distributed computing, each machine can have 10 million UUID per second as per IETF REFC 4122 . Implementation for generating UUID are widely available in various programming language libraries and in DBMS.
JDK 1.5 provides classes to generate UUID.
Oracle SYS_GUID() , MySQL UUID() , SQL Server NEWID()
Subscribe to:
Posts (Atom)