- What is the Java Message Service?
Answer: The Java Message Service (JMS) API is an API for accessing enterprise messaging systems.It is part of the Java 2 Platform, Enterprise Edition (J2EE).
The Java Message Service makes it easy to write business applications that asynchronously send and receive critical business data and events.
The Java Message Service defines a common enterprise messaging API that is designed to be easily and efficiently supported by a wide range of enterprise messaging products.
The Java Message Service supports both message queueing and publish-subscribe styles of messaging.
- Is the Java Message Service another mail API?
Answer: No.The term messaging is broadly defined in computing.It is used for describing various operating system concepts; it is used to describe email and fax systems; and with the JMS API, it is used to describe asynchronous communication between enterprise applications.
JMS messages are asynchronous requests, reports, or events that are consumed by enterprise applications, not humans.They contain vital information needed to coordinate these systems.They contain precisely formatted data that describe specific business actions.Through the exchange of these messages, each application tracks the progress of the enterprise.
- Is the Java Message Service a product?
Answer: No, the Java Message Service is the specification of a common API for enterprise messaging.A JMS provider supplied by an enterprise messaging vendor is required to use it.
- Where can I find the Java Message Service specification?
Answer: The Java Message Service 1.0.2 specification is available at http://java.sun.com/products/jms/docs.html.A link to the online JMS API documentation is at the same location.
- What is the use of BytesMessage?
Answer: 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 use of StreamMessage?
Answer: 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 TextMessage?
Answer: 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 ObjectMessage?
Answer: 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 MapMessage?
Answer: 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 difference between BytesMessage and StreamMessage?
Answer: BytesMessage stores the primitive data types by converting them to their byte representation.Thus the message is one contiguous stream of bytes.While the StreamMessage maintains a boundary between the different data types stored because it also stores the type information along with the value of the primitive being stored.BytesMessage allows data to be read using any type.Thus even if your payload contains a long value, you can invoke a method to read a short and it will return you something.It will not give you a semantically correct data but the call will succeed in reading the first two bytes of data.This is strictly prohibited in the StreamMessage.It maintains the type information of the data being stored and enforces strict conversion rules on the data being read.