Archive for April, 2009
Sunday, April 26th, 2009
Spring is often times considered a web application library similar to the EJB 3.x stack. However, Spring is so much more and can live in standalone applications as well. Today I am going to touch on using Spring in standalone applications to handle dependency or configuration management. I do not mean configuration management in the sense of CMS, but in the sense of handling and creating inner-dependencies between business objects, especially in the aspect of singletons. One of the biggest issues with applications is high coupling by having to have objects create other objects, assign the relationships, and manage those dependencies. However, one of the primary goals of any application is loose coupling, which in turn reduces maintenance. Some of the traditional ways of handling these types of scenarios include:
- Factories that create the instances and setup the dependencies. While this abstracts the configuration to a factory, it still requires high coupling in the factory to talk to other factories and know about the objects and their inner dependencies. It also requires static classes for the factories typically, which reduces testability. My general rule of thumb is to reduce static classes in order to improve pluggability and testability.
- Custom XML configuration to setup the dependencies and objects. This pattern abstracts away all the coupling, but requires yet another XML format to be defined, a custom parser to be written, etc. This can be a tedious operation depending on how fine grained and customizable the format is.
Spring offers a much better approach to this. It allows you to use either annotations or XML configuration to setup the dependencies. The difference with Spring’s XML configuration is that it is highly dynamic and flexible and offers several capabilities. Further, Spring offers other support including AOP in order to separate and handle cross cutting concerns. For example, you can easily add security or logging around any method invocation without the associated class having to manage that relationship. In the end, your objects only deal with the objects they must directly deal with resulting in very loose coupling and high testability.
(more…)
Tags: Annotations, AOP, Configuration Management, Dependency Management, DI, Java, Spring
Posted in CSS, Java, Software, Usability | No Comments »
Thursday, April 23rd, 2009
As promised in prior articles, I will demonstrate how to create a custom pipeline factory in Netty that provides some custom features including:
- Auto-discovery of annotated handlers (including pluggable annotation provider)
- Auto-provisioning of stateful and stateless handlers
- Auto-pooling of stateful handlers (including pluggable pooling provider)
(more…)
Tags: Annotation, Auto Discovery, Channel Handlers, Netty, performance, Pooling
Posted in Java, Software | 2 Comments »
Tuesday, April 21st, 2009
Before I start, let me say that I am in no way affiliated with JBoss or Netty (although I may choose to submit future code as part of the open source community). Therefore, all comments, code samples, etc are mine or a derivative of examples provided by Netty. I apologize up front for any possible discrepancies or errors (please let me know though, so I can fix and update them). I believe this documentation to be completely accurate, though.
In Netty, the main injection point into your code or business logic is through the use of handlers. Handlers follow the interceptor pattern similar to filters in a traditional servlet-based web application. For more information, see Netty documentation. Handlers provide an event model that allows an application to monitor incoming/outgoing data, modify the data, convert the data, act upon the data, etc. In essence, they allow you to completely abstract separate concerns into separate classes without needing to have class A know about class B. As an example, you can add a log handler to listen for incoming data and print it out. This handler can be added and removed without modifying any other class. Handlers, however, are more often used to build protocol stacks in which one filter acts as a codec to decode/encode a stream of bytes into higher level objects which are then acted upon by another handler such as a business logic handler.
(more…)
Tags: Handlers, Netty, NIO, Protocols
Posted in Java, Software | 5 Comments »
Monday, April 20th, 2009
Now that I have narrowed by server down to using JBoss Netty, it’s time to define protocols. This involves two things. First, we need to select the actual protocol itself and second, we need to define how to construct the handlers. Let’s start by defining the handlers. Netty is built upon the principle of handlers, which are more or less the same idea as filters in web applications. This allows you to define functional handlers that perform specific tasks such as compression, security, and business logic. In this manner, one handler does not need to concern itself with another handlers and handlers can be added/removed without affecting the system. However, order and dependency does matter in certain cases. For example, if you have a handler that depends on a translation of bytes into a POJO, then the handler performing the translation must be in the pipeline and occur prior to the application logic. Generally, a semi-complex, yet real world application, for a protocol stack would consist of:
- Encryption Handlers via SSL processing, handshakes, etc. These handlers take the data stream, decrypt it, and pass the decrypted bytes down the stream
- Compression Handlers via GZip processing for instance. These handlers take the data stream, uncompress it, and pass the uncompressed bytes down the stream
- Transport Protocol Handlers for converting transport protocol data in protocol messages such as HTTP. Subsequent handlers use the protocol-specific messages (ie: HTTP message) for interacting with the protoocol data
- Security Handler for performing security in terms of authentication and authorization checks. These handlers read a header or portions of the data stream and validate the data as being both a valid, authentication user as well as being permissible or authorized or view/modify the associated data
- Application Protocol Handlers for controlling an actual underlying application/business protocol. These differs from the transport protocols in that they specify the actual business data and commands to perform on the business data. Often times, these handlers parse out a given business command, convert to a business message or POJO, and invoke a business delegate object or handler to process the data
- Application/Business Handlers for processing business logic related to a specific protocol message.
This type of stack allows easy extensibility and customization. Future handlers can be added in as needed and future application messages can be added in through custom delegates. It also allows the business logic code to only contain business logic without concern for security, compression, etc. The business logic deals solely with business objects and the business protocol. Further, because of this abstraction and because of the abstraction of the transport layers in Netty, supporting other transport protocols such as UDP, serial, USB, etc is an easy task.
(more…)
Tags: Netty, NIO, performance, Protocols, Scalable
Posted in Java, Software, Standards, Usability | 3 Comments »
Friday, April 17th, 2009
Just like software, user interface design or user centered design has patterns as well. The following are a collection of various patterns with examples to sites that deploy them. This is a very good collection of web-based patterns for your current and future web applications.
Quince
Tags: Patterns, Quince, UCD, User Centered Design, user interface, User Interface Design, User Interface Designs
Posted in Usability, Web Design | No Comments »
Monday, April 13th, 2009
We have now analyzed various open source NIO servers for performance and memory consumption. Per my quick, initial testing, only Grizzly, Mina, and Netty were comparable. Now, let’s analyze features and how each of these frameworks use them. For my purposes, I am going to be looking into the following features that I personally value most important for my project:
- Intercepting Pattern (ie: Filters)
- Access to high level, yet effcient, buffers rather than lower level byte buffers
- Protocol independence and abstraction
- Socket independence and abstraction
- Custom protocol support
- POJO support for encoding/decoding
- Custom thread model support
- HTTP support
- User documentation (user guide, javadoc, source code, examples
(more…)
Tags: Grizzly, Java, Mina, Netty, NIO
Posted in Java, Software | 3 Comments »
Thursday, April 9th, 2009
As my ongoing investigation into picking a suitable and highly scalable server for my future applications, today I will be talking about memory. Yesterday, we discussed performance and saw in general how Netty and Grizzly compared to one another. Today, we will further extend that by comparing memory usage footprint at start, over time, and how the memory usage compares as a trend with respect to garbage collection. As GC is one of the biggest culprits of performance, the more efficient the server at conserving memory, the better the performance. So, without further ado, let’s get to the testing.
(more…)
Tags: Garbage Collection, Grizzly, Java, memory, Mina, Netty, NIO, performance, xSocket
Posted in Java, Software | 2 Comments »
Tuesday, April 7th, 2009
As a continuation of evaluating an NIO server for my iPhone game, I started with looking at pure performance. First, the following links provide already existant benchmarks:
I took my own samples and quickly ran some basic tests as well to just get a really rough idea. Note that these metrics should be taken with a large grain of salt as they do not attempt to optimize any of the test libraries and the clients are all run in traditional threads on non-server class machines alongside the actual servers. Thus, these tests are quickly CPU bound on the client. The idea is to just get a very rough estimate to compare alongside the above tests.
That point aside, my test basically creates a echo server and echo client. It then starts up X simultaneous threads that push a simple ‘testing’ string back and forth to the server as fast as it can. The end result is the time it takes to send a message and receive the echo in milliseconds.
(more…)
Tags: Benchmark, Comparison, Grizzly, Java, Mina, Netty, NIO, performance, xSocket
Posted in Java, Software | 2 Comments »
Monday, April 6th, 2009
So, after spending a couple of hours trying to figure out why a particular function was so slow compared to my expectations, I realized my issue dealt with the fact that I forgot about String.concat. I had embedded in my head the past year to always use StringBuilder when possible when concatenating strings. After figuring out why another method was considerably faster, I started to dig into the performance. It is there I realized that String.concat existed and is actually faster in certain cases. Wanting to understand this more, I did a full thorough analysis and comparison. Now, I realize that several others have already undergone this and produced similar results, so you ask, why go through it all again? Well, I wanted to (A) personally verify it myself to learn and understand more (B) dig into the inner depths of the byte code and java code to understand exactly why and (C) to provide more concrete examples and use cases. That being said, continue reading below for my full analysis including my DO lists.
If you do not choose to read any further, at least read the following DO’s:
- DO use the plus operator if all operands are constants
- DO use String.concat if only concatenating 2 strings
- DO use StringBuilder within a for loop when updating values
- DO use StringBuilder if concatenating more than 2 strings
- DO use StringBuilder rather than re-assigning variables multiple times
- DO ensure StringBuilder has proper sizing
(more…)
Tags: Java, Optimization, performance, String Concatenation, StringBuilder, Strings
Posted in Java, Software | 6 Comments »
Thursday, April 2nd, 2009
As part of my new iPhone project to build a new game, I need to have a backend server to communicate with all the members in order to sync and dynamically update on the fly. Knowing the iPhone and its possibilities, I want to plan for a highly optimistic future in which thousands of concurrent connections take place. The transactions will be minimal and compressed to conserve bandwidth, but will occur frequently to monitor player actions and provide alerts to other players as they are interacted with. So, my immediate question was where do I start. I obviously have two primary choices: HTTP or proprietary. Let’s look into each option a little more in depth.
(more…)
Tags: HTTP, iphone, Java, NIO, scalable servers, scaling
Posted in CSS, Java, Software, Usability | No Comments »