Thursday, September 11, 2008

Message Bus



A message bus specializes in transporting messages between applications. A message bus contains three key elements:

    * A set of agreed-upon message schemas
    * A set of common command messages [Hohpe04]
    * A shared infrastructure for sending bus messages to recipients

When you use a message bus, an application that sends a message no longer has individual connections to all the applications that must receive the message. Instead, the application merely passes the message to the message bus, and the message bus transports the message to all the other applications that are listening for bus messages through a shared infrastructure. Likewise, an application that receives a message no longer obtains it directly from the sender. Instead, it takes the message from the message bus. In effect, the message bus reduces the fan-out of each application from many to one.

Usually, the bus does not preserve message ordering. Internal optimizations, routing, buffering, or even the underlying transport mechanism might affect how the messages travel to the receiving applications. Therefore, the order in which messages reach each receiver is nondeterministic.

See: Prescriptive Architecture Message Bus

Wednesday, September 10, 2008

Concurrent Programming in Java [reading note]

- Overriding run() in Thread vs. Using Runnable as an argument in Thread constructor
   Default strategy is to define Runnable as a separate class and supply it in a Thread constructor. Isolating code within a distinct class relieves you of worrying about any potential interactions of synchronized methods or blocks used in the Runnable with any that may be used by methods of class Thread.
   A small proof for Composition over Inheritance.

- Method Adapter patterns
   Besides adapter, sub-classing, template method, the most flexible approach to before/after control is to define a class whose entire purpose is to invoke a particular method on a particular object. In the command object pattern and its many variants, instances of such classes can then be passed around, manipulated, and ultimately executed. In some other languages, function pointers and closures are defined and used to achieve some of these effects.


interface Callable {
   Object call(Object arg) throws Exception;
}

- Collection Traveral
   Lock the whole collection and operate on each item may be time consuming. JDK's Iterator is a fast-fail iterator that throw a ConcurrentModificationException if the collection is modified in the midst of a traversal using the version approach.

Friday, August 15, 2008

relative containing block

Elements with position:relative are positioned relative to themselves. If we just set position:relative on an element, without specifying any of the edge properties, the element ends up exactly where it would have been if we had set position:static, or if we hadn't set position at all.

But there is one important usage. If we set relative positioning on div-1, any elements within div-1 will be positioned relative to div-1, not matter they are static/absolute/relative/float(width). Thus, it serves as a container (or containing block).

By definition, the containing block of an absolutely positioned element is its nearest positioned ancestor with position property is absolute, fixed or relative. So if we don't define div-1 as relative positioned, it won't be its containing block.

Note that this is a special definition for absolutely positioned element **only**. The containing block of other elements is any block box (div) surrounding it.

A floated box is laid out according to the normal flow, then taken out of the flow and shifted to the left or right as far as possible in its containing block.
(float:right doesn't work in IE? ->the element must have width defined)

1. Learn CSS Positioning in Ten Steps
2. Relatively absolute
3. Atlantic CSS ZenGarden

Tuesday, August 12, 2008

radio button


<form name="orderform">
Which one is your favorite?
  <input type="radio" name="music" value="Rock" checked="checked"> Rock
  <input type="radio" name="music" value="Reggae"> Reggae
  <input type="radio" name="music" value="Pop"> Pop
  <input type="radio" name="music" value="Metal"> Metal
  <input type="submit" onclick="get_radio_value()">
</form>

Accessing the value for a set of radio buttons is not that straight forward. document.form_name.radio_name.value does not work.
To get the associated value -

function get_radio_value() {
  for (var i=0; i < document.orderform.music.length; i++) {
     if (document.orderform.music[i].checked) {
       var rad_val = document.orderform.music[i].value;
     }
    }
}

In DOM object, the radio "music" is an array of elements. But when the form is submitted, there is only one attribute 'music' with the checked radio value.

Also, note that there are two different way to access DOM element. document.getElementById or document.<form-name>.<input-element-name>

For select drop-down, we can set the selection either by select_element.value or iterate its options array, and set selectedIndex value. Note that onchange event won't be triggered automatically.