Thursday 19 June 2014

How to send POST Http Request with paramters and read the response ?

A POST request can be used for multiple purposes on the web. It can be used for performing the Create or Update operations for various resources. The most common usage of a POST request is in the form of a FORM on an HTML page.
The HTTP protocol doesn’t say anything about the best way to use the POST request but with the web the HTML has become the standard for issuing POST request.
One can also send POST requests from javascript (AJAX), .Net, PHP or Java based programs. Recently I had written a program to issue a POST request in Java.
If you have server listening for requests then this program code can be handy. In my case, it was a REST web service which was listening for POST requests. One can also issue these HTTP requests for servlets too.
The code follows:

Friday 13 June 2014

What is the use of MapMessage?



A MapMessage carries name-value pair as it's payload. Thus it's payload is similar to the java.util.Properties object of Java. The values can be Java primitives or their wrappers.

What is the use of ObjectMessage?



ObjectMessage contains a Serializable java object as it's payload. Thus it allows exchange of Java objects between applications. This in itself mandates that both the applications be Java applications. The consumer of the message must typecast the object received to it's appropriate type. Thus the consumer should before hand know the actual type of the object sent by the sender. Wrong type casting would result in ClassCastException. Moreover the class definition of the object set in the payload should be available on both the machine, the sender as well as the consumer. If the class definition is not available in the consumer machine, an attempt to type cast would result in ClassNotFoundException. Some of the MOMs might support dynamic loading of the desired class over the network, but the JMS specification does not mandate this behavior and would be a value added service if provided by your vendor. And relying on any such vendor specific functionality would hamper the portability of your application. Most of the time the class need to be put in the classpath of both, the sender and the consumer, manually by the developer.

What is the use of TextMessage?



TextMessage contains instance of java.lang.String as it's payload. Thus it is very useful for exchanging textual data. It can also be used for exchanging complex character data such as an XML document.

What is the use of StreamMessage?



StreamMessage carries a stream of Java primitive types as it's payload. It contains some conveient methods for reading the data stored in the payload. However StreamMessage prevents reading a long value as short, something that is allwed in case of BytesMessage. This is so because the StreamMessage also writes the type information alonwgith the value of the primitive type and enforces a set of strict conversion rules which actually prevents reading of one primitive type as another.

What is the use of BytesMessage?



BytesMessage contains an array of primitive bytes in it's payload. Thus it can be used for transfer of data between two applications in their native format which may not be compatible with other Message types. It is also useful where JMS is used purely as a transport between two systems and the message payload is opaque to the JMS client. Whenever you store any primitive type, it is converted into it's byte representation and then stored in the payload. There is no boundary line between the different data types stored. Thus you can even read a long as short. This would result in erroneous data and hence it is advisable that the payload be read in the same order and using the same type in which it was created by the sender.

What is the basic difference between Publish Subscribe model and P2P model?

Publish Subscribe model is typically used in one-to-many situation. It is unreliable but very fast. P2P model is used in one-to-one situation. It is highly reliable.

What is the use of Message object?

Message is a light weight message having only header and properties and no payload. Thus if theIf the receivers are to be notified abt an event, and no data needs to be exchanged then using Message can be very efficient.

Thursday 5 June 2014

What is the role of JMS in enterprise solution development?


JMS is typically used in the following scenarios
1. Enterprise Application Integration: - Where a legacy application is integrated with a new application via messaging.
2. B2B or Business to Business: - Businesses can interact with each other via messaging because JMS allows organizations to cooperate without tightly coupling their business systems.
3. Geographically dispersed units: - JMS can ensure safe exchange of data amongst the geographically dispersed units of an organization.
4. One to many applications: - The applications that need to push data in packet to huge number of clients in a one-to-many fashion are good candidates for the use of JMS. Typical such applications are Auction Sites, Stock Quote Services etc.

What is the difference between topic and queue?

A topic is typically used for one to many messaging i.e. it supports publish subscribe model of messaging. While queue is used for one-to-one messaging i.e. it supports Point to Point Messaging.

What are the different messaging paradigms JMS supports?

Publish and Subscribe i.e. pub/suc and Point to Point i.e. p2p.

What are the different types of messages available in the JMS API?

Message, TextMessage, BytesMessage, StreamMessage, ObjectMessage, MapMessage are the different messages available in the JMS API.

What are the advantages of JMS?


JMS is asynchronous in nature. Thus not all the pieces need to be up all the time for the application to function as a whole. Even if the receiver is down the MOM will store the messages on it's behalf and will send them once it comes back up. Thus at least a part of application can still function as there is no blocking.  

How JMS is different from RPC?


In RPC the method invoker waits for the method to finish execution and return the control back to the invoker. Thus it is completely synchronous in nature. While in JMS the message sender just sends the message to the destination and continues it's own processing. The sender does not wait for the receiver to respond. This is asynchronous behavior.

What is JMS?

JMS is an acronym used for Java Messaging Service. It is Java's answer to creating software using asynchronous messaging. It is one of the official specifications of the J2EE technologies and is a key technology.

Tuesday 3 June 2014

The Advantages and Traps of Autoboxing

What is ‘autoboxing’ some people might ask?
Autoboxing is a feature, which was added in Java 5. Autoboxing is the automatic conversion of primitive data types like int, double, long, boolean to its wrapper Object Integer, Double… and vice versa. So they can be as Object e.g. in Collections.
For example:
1
list.add(5);
In Java 1.4 you had to write for the same result:
1
list.add(Integer.valueOf(5));
(The primitive types must be converted first, because a list can only contain objects.)
Advantages?
  • Less code to write.
    • The code looks cleaner.
  • The best method for conversion is automatically chosen, e.g. Integer.valueOf(int) is used instead of new Integer(int)
Disadvantages
1. Can lead to unexpected behaviour
The usage of autoboxing can lead to difficult to recognize errors. Especially if you mix wrapper with primitives in ‘equals’/’==’.
For Example this:
1
2
3
Long l = 0L;
System.out.println(l.equals(0L));
System.out.println(l.equals(0));
Results into
1
2
true
false
2. Hiding
It hides the object creation, which can lead to a big performance loss. For example:
1
2
3
4
Integer counter = 0;
for(int i=0; i < 1000; i++) {
    counter++;
}
What does ‘counter++’?
It gets the primitive int of the Integer and adds one, that it converts back to a “new” Integer (not necessarily, if the Integer cache contains this Integer).
In Java 1.4 it would look like:
Integer.valueOf(counter.intValue() + 1)
3. Overloading
1
2
3
4
5
6
7
8
9
10
11
public static void main(String[] args) throws Exception
{
Integer l = 0;
fubar(l);
}
static void fubar(long b) {
System.out.println("1");
}
static void fubar(Long b) {
System.out.println("2");
}
The result is 1. Because there is no direct conversion from Integer to Long, so the “conversion” from Integer to long is used.
4. NullPointerException
You can get a NullPointerException, if the wrapper object is null and is unboxed. Pointing out the obvious there can’t be a NullPointer  with primitive variables, but they can have the value zero.
For example look at the code below. In this little example it seems obvious that there can be a NullPointerException. But let’s face it in real code these are only two lines in possible hundreds of lines of code.
1
2
HashMap<String, Integer> map = new HashMap<String, Integer>();
int x = map.get("hello");
Conclusion
IMHO “Autoboxing” is too unexpected in its behavior and can easily result in difficult to recognize errors. Further more the performance loss on unnecessary Autoboxing is too big to ignore. Only for the purpose of clean code this is much to ignore. I would suggest to use it are in simple JUnit tests, where you enter many hard coded numbers(You can ignore the warning with @SuppressWarnings annotation, which is one option of the eclipse hotfix). Or in simple application where you don’t handle with many data like you usually do in a JEE application.
I myself stumbled across one of these errors in an simple “if”. But I don’t know anymore, how the error looked like. A other completely independent team in our company had a similar experience and they forbid to use Autoboxing in their project, too.
You can configure eclipse in the compiler options, that using autoboxing produces warnings.
Java->Compiler->Errors/Warnings->”Potential programming problems”->”Boxing and unboxing conversions”