Wednesday, 20 February 2013

Writing Performance Effective Java Code



Writing Performance Effective Java Code

Based on my prior experience and/or learning's, here are few tips I suggest every Java programmer to follow for writing performance effective Java Code.

  1. Use ‘StringBuilder’ instead of String concatenation: Always prefer the usage of ‘StringBuilder’ when you have requirement of concatenating large number of string values. 
  1. Do not prefer to use immutable class constructors; instead use static factory methods if available.  [eg: use of String constructor class for literals. Instead prefer the usage of String literals.] 
i.e. Prefer String s1=”abc”;    instead of     String s1=new String(“abc”);
      Use      Integer.valueOf(String)    instead of     new Integer(String);
       
  1. Prefer the use of primitive types instead of boxed primitive or wrapper types: 
Eg: Integer sum=0;
       for(int i=0; i<1000; i++)
             sum+=i;

The above program with wrapper type is much slower than this one:
int sum=0;
      for(int i=0; i<1000; i++)
             sum+=i; 

  1. Prefer the use of primitive data comparisons to String value comparisons.
    1. In primitives, prefer this comparison order. i.e. use boolean, byte, char, short, int, long instead of ‘float’ and ‘double’ comparisons 
  1. Minimize the usage of synchronization and synchronized utility classes in Java API’s wherever possible. 
eg. Prefer use of ‘StringBuilder’ to ‘StringBuffer’, ‘ArrayList’ to ‘Vector’, ‘HashMap’ to ‘Hashtable’ etc.
  
  1. Minimize the mutability of classes: Always declare the member variables or objects with least possible access specifier and make them ‘final’ when there is guarantee that it’s not changed. 
    1. Try to have all the member variables or objects as ‘private’ unless other access specifier is required
    2. Make all the unchanged primitive variables ‘final
    3. Make all the arrays or collections whose references should remain same as ‘final
    4. Make all the methods ‘final’ when you are sure that the method definition should not be changed.

  1. Keep re-usable code in static final methods: If there is any business logic, or functionality which does not belong to any of your classes or entity, move the methods into a utility class and make all of methods static and final. 
  1. Minimize the scope of local variables: Declare local variables only when it is used for first time. Do not declare them in the very beginning; and use it some where in the middle. By the time reader would have forgot the initial value of variable if any defined.  
  1. Use static block initializers to initialize or construct the expensive objects instead of having them in the methods which gets called many times. 
  1. Prefer the usage of interfaces to reflection: when you follow reflective way of instantiating your class i.e. (with use Class.forName(classname-to-load)), you 
    1. Lose all benefits of compile-time type checking
    2. Suffer with slow performance in method invocation.

Appreciate for taking time and reading through the different tips. If you would like to share your feedback or add on another tip, feel free to comment on the post.

Tuesday, 12 February 2013

Displaying jGrowl message at custom location


Displaying jGrowl message at a custom location:

If you are displaying jGrowl messages in your website, there are five default placement options available for you; top-left, top-right, bottom-left, bottom-right, center. This must be changed in the defaults before the startup method is called.

To do this, you'll have something like
    <script type="text/javascript">
           $.jGrowl.defaults.position = 'top-right';
    </script>

However, jGrowl does not let you directly specify your own positioning of message in web page. 
Solution Approach: Add your own position in the jGrowl CSS file:
To do this, you will have to add one CSS position entry or override any of the default CSS definitions provided by jGrowl in 'jquery.jgrowl.css' file like shown below:
 div.jGrowl.YourDisplayArea {
position: absolute;
left: 3%;
top: 20%;
}

Similarly, you could also position with respect to 'right', 'bottom' attributes available in CSS.

And you can use the new position in your jGrowl messages which will bring up the message in the location as you desired.


$.jGrowl("sample JGrowl message displayed in 'YourDisplayArea '",
{
sticky: true,
position: 'YourDisplayArea'
});


Monday, 11 February 2013

jGrowl messages at different positions


Displaying different jGrowl messages at different positions within the same page


In normal scenario, if you want to display only one jGrowl message in any part of the web site, you will just do it so by creating the messaging and specifying the position accordingly as given in this example (i.e. with any of values for positions --> top-left, top-right, bottom-left, bottom-right, center)

eg: 

          $.jGrowl('This is sticky message which appears on bottom right corner of your website page', 
                        {
sticky: true,
header: 'Simple jGrowl message',
position: 'bottom-right'
});

This displays the simple sticky jGrowl message as expected in 'bottom-right' of page.

However, think about if you want to have one more message displayed; say, in 'top-left' corner; For this, you may try to accomplish this by adding one more similar jGrowl message with position as 'top-left' like one below:

 $.jGrowl('This sticky message should appear on top left', 
                        {
sticky: true,
header: 'Simple jGrowl message',
position: 'top-left'
});

But it doesn't work; instead the jGrowl will display this message just below to the first one (kind of attached to it); This is because by default jGrowl displays all of the messages using default container i.e. default <div> section it uses and hence it will group all of your messages together (if you have multiple); with 'close-all' button to close all of these jGrowl messages at a time.

Solution (Custom containers):

To display jGrowl messages at different locations, you would need to create your own custom containers; This is as simple as creating your own HTML '<div>' section any where in your web page with an 'id' value. You can have the '<div> elements any where in <body> section of HTML or webpage code.

Lets create two DIV's for our examples:

<div id="jgrowlMsg1" class="bottom-right"></div>
<div id="jgrowlMsg2" class="top-left"></div>

And now we just say, 
 
 $('jgrowlMsg1').jGrowl('This is sticky message which appears on bottom right corner of your website page', 
                        {
sticky: true,
header: 'Simple jGrowl message',
position: 'bottom-right'
});



$('jgrowlMsg2').jGrowl('This sticky message should appear on top left', 
                        {
sticky: true,
header: 'Simple jGrowl message',
position: 'top-left'
});


This will bring up messages in different locations as desired in 'bottom-right' and 'top-left' of the webpage respectively.


Wednesday, 30 January 2013


WebSphere ESB interview questions

What kind of questions might be asked when you go for in WebSphere ESB (or) other ESB' developer interviews?

ESBs really play crucial role in the SOA world today and jobs in this area have grown rapidly in the last few  years. So, when you plan to appear for an ESB developer or consultant positions, you might worry what kind of questions the interviewer might ask for.

In this blog, here I have come up with the list of topics or questions where interviewers (including myself) mostly touch upon for testing the candidate’s capability. This is my initial draft. I would add further questions as and when I remember… 

Any suggestions or feedback to improve the content is appreciated. 


Questions on Architecture, Foundations and/or Pre-requisites:

·         Explain or tell about SOA architecture.
·         How do you differentiate between WebServices and SOA?
·         Compare and contrast different approaches of implementing WebServices; JAX-RPC, JAX-WS, REST (JAX-RS).
·         What is the need of an ESB in SOA architecture? What role does it play?
·         List out the capabilities of ESB
·         Explain SCA architecture. How does it vary from SOA architecture?
o       What is service messaging object? How is it different from data object?
o       Briefly explain the service message object structure.


Questions on Development, Tooling Features:

    
  • What kind of projects can you create in WID that run on WebSphere ESB runtime?
  • What is an Integration Solution? Can you deploy it on server runtime?
  • What is mediation module?
  • What is an assembly diagram? What all does an assembly diagram will have?
  • List out the different bindings available in WID
  • Which is the default binding?
  • Which binding will you use when you want to communicate between mediation modules?
  • What are different categories of primitives available?
  • Difference between XSLT and BO Transforms? If you want the transformation in specific order, which one would you use?
  • Difference between Service Invoke and Callout
  • If you want to invoke another service within a mediation module and retrieve the response within the same flow, which primitive would you use?
  • Difference between Fail and Stop primitives? When do you use each?
  • Which primitives have this attribute? “Use dynamic endpoint set in message header”
  • How do you dynamically change the web service endpoint address?
  • What is the correlation context? How is it different from transient context?
  • What is the purpose of shared context? When do you use it?
  • When do you think you go for custom primitive? How many input and output terminals can custom primitive have? How do you fire the output through output terminals?
  • When do you use a data handler? Name the data handlers that you would have used. How to create your own data handler.
  • How do you perform serialization and de-serialization of SMO objects to/from native objects?
  • When do you use FanIn & Fanout primitives?
  • What are shared libraries? How do you implement them? What are advantages.


EAI patterns:
·         What are the service mediator patterns you worked with?
·         Explain service gateway pattern, selector pattern, aggregation pattern etc…

Questions on WebServices:
·         What is purpose of WSDL in WebServices?
·         Explain WSDL structure
·         What are different WSDL binding styles? When do you use each? How does SOAP message structure change in each of scenario? Which is commonly used?
o       Document literal
o       Document literal wrapped
o       Document encoding
o       Rpc literal
o       Rpc encoding
·         Can you refer another WSDL inside a WSDL? If so, are there any challenges.
·         Explain SOAP message structure.
·         How does a SOAP message with attachments look like?
·         Have you worked with XOP or MTOM? Please explain.
·         When do you use SOAP handlers? What are methods to be implemented if you were implementing it?
·         What are different development approaches in WebServices? Top-down vs bottom-up.
·         Compare JAX-RPC vs JAX-WS vs REST. Which one do you normally use?
·         What are advantages of JAX-WS over JAX-RPC?
·         List the files which get generated at client side and/or server side when you work using
o       JAX-RPC approach
o       JAX-WS approach

Questions on JMS/MQ:

·         What are different types of queues?
·         Tell about different messaging styles in JMS. When do you use each?
    o Point-to-point
    o Publish-subscribe
·         Differences between Topics and Queues
·         Tell me the difference between local and remote queues
·         What is an ActivationSpec? When do you use it?
·         How do you invoke an MDB using JMS?
·         How do you configure your local or remote queues in Websphere?

Security:
·    How do you implement a basic authentication in WebServices Import/Export?
·    Have you worked with SSL security i.e. digital certificates?
·    Explain the usage of WS-Security Policy Sets. What are advantages of using Policy Sets
·    Explain the different steps you perform when implementing any one of the above security mechanisms in WebSphere ESB server?

Other Questions:
    • What are differences between WID 6.2 and WID 7.0 or IID? How are mediation modules deployed in v6.2 vs v7.0 etc…
    • Have you faced any difficulties during your development & how did you overcome them.