wtorek, 9 października 2012

Some examples from my daily tasks part 1


I have to design and implement mechanism which handle such situation:
There are two applications, let's say App1 and App2 and OpenMQ Broker between those applications. Furthermore there are two queues on this Broker - ControlQueue and DataQueue.
Under normal conditions (when both App1 and App2 are alive) App1 sends some real time data to DataQueue and App2 receives that data. App1 caches data and has a refresh functionality in case App2 need to update all data.


  1. App2 to maintain connection every ten seconds sends PING message to ControlQueue. 
  2. App1 receives PING messages from ControlQueue and saves the time of last PING message.
  3. Every time App1 receives PING message it checks if data is sending to DataQueue. If not, it launches refresh and send cached and real time data to DataQueue. 
  4. App1 (by QueueConectionMaintainer) checks every ten seconds if time of last PING message is later than 25 seconds and if yes then it stop sending data to DataQueue. 
App1 is created with Spring Integration and use jms:inbound-channel-adapter to receive messages from ControlQueue. 

Ad.1.
 PING message is a jms object message with Properties object inside
Ad.2, Ad.3
Connection with OpenMQ - Spring Integration configuration:


Connection factory decorator:


Queue listener - Spring Integration configuration:

ControlQueueListenerActivator implementation:

Ad.4 Queue connection maintainer - Spring Integration configuration: QueueConnectionMantainer implementation:

czwartek, 4 października 2012

Into the Effective Java part. 1

Using static factory methods instead of constructors. *
 Advantages:

  • Unlike constructors they have names. 
If the parameters to a constructor do not, in and of themselves, describe the object being returned, a static factory with a well-chosen name is easier to use and the resulting client code easier to read.
  • They are not required to create a new object each time they're invoked. 
The ability of static factory methods to return the same object from repeated invocations allows classes to maintain strict control over what instances exist at any time.
  • They can return an object of any subtype of their return type. 
This gives you great flexibility in choosing the class of the returned object.
  • They reduce the verbosity of creating parameterized type instances.  
Unfortunately, you must specify the type parameters when you invoke the constructor of a parameterized class even if they’re obvious from context.
With static factory methods instead of doing: 


You can implement:

Then you could replace the wordy declaration above with this succinct alternative:

Disadvantages:
  • The main disadvantage of providing only static factory methods is that classes without public or protected constructors cannot be subclassed.
  • A second disadvantage of static factory methods is that they are not readily distinguishable from other static methods.
They do not stand out in API documentation in the way that constructors do, so it can be difficult to figure out how to instantiate a class that provides static factory methods instead of constructors.


* Effective Java Second Edition, Joshua Bloch

poniedziałek, 1 października 2012

Into the Spring part 1

How does spring simplify Java development?
Key strategies:

    • Lightweight and minimally invasive development with plain old Java objects (POJOs)
    • Loose coupling through dependency injection and interface orientation
    • Declarative programming  through aspects and common conventions 
    • Boilerplate reduction through aspects and templates
Almost everything Spring does can be traced back to one or more of these four strategies*. 





*Spring in Action 3rd edition, Craig Walls