JMS (Java
Messaging Service) is an API for messaging (the exchange of information between
two separate and independent network entities. It sets a number of interfaces
for sending and receiving messages.
JMS was
developed by Sun Microsystems to provide a way for Java programs to access an
enterprise messaging system, also known as message oriented middleware (MOM).
MOM provides a mechanism for integrating applications in a loosely coupled,
flexible manner by providing asynchronous delivery of data between applications
in an indirect way through an intermediary.
JMS
Messaging Domains-
Point-to-Point Messaging Domain
In the point-to-point messaging
domain the application is built on the basis of message queues, senders and
receivers. Each and every message is addressed to a particular queue. Queues
retain all messages sent to them until the messages are consumed or expired.
There are some characteristics of PTP messaging:
1)
There is only one client for
each message.
2)
There is no timing dependency
for sender and receiver of a message.
3)
The receiver can fetch message
whether it is running or not when the sender sends the message.
Publish/Subscribe Messaging Domain
In publish/subscribe messaging
domain, only one message is published which is delivered to all clients through
Topic which acts as a bulletin board. Publishers and subscribers are generally
anonymous and can dynamically publish or subscribe to the topic. The Topic is
responsible to hold and deliver messages. The topic retains messages as long as
it takes to distribute to the present clients.
Some of the characteristics
are:
1)
There can be multiple
subscribers for a message.
2)
The publisher and subscribe
have a timing dependency. A client that subscribes to a topic can consume only
messages published after the client has created a subscription, and the
subscriber must continue to be active in order for it to consume messages.
JMS participating objects
JMS application has some basic
building blocks, which are:
1)
Administered objects –
Connection Factories and Destination
2)
Connections
3)
Sessions
4)
Message Producers
5)
Message Consumers
JMS Administered Objects
Administered objects are
pre-configured JMS object created by administrator for the use of client. JMS
application provides two types of administered objects:
1)
Connection Factories
2)
Destinations
Destination and Connection
factories are the best maintained administratively rather than pro grammatically.
Administered objects are
created by JMS system administrator in JMS Provider by using the Application
Server the admin console. These both objects are stored in Application server
JNDI Directory or JNDI Registry.
Management of these objects
belongs with MOM administrative tasks that vary from provider to provider.
Type of Message Acknowledgement
Acknowledge initiated by JMS
provider or by the client, depending on the session acknowledgement mode.
1)
Auto mode: When a session uses auto mode, the messages sent
or received from the session are automatically acknowledged. This is the
simplest mode and expresses JMS's power by enabling once-only message delivery
guarantee.
2)
Duplicates okay mode: When a session uses duplicates okay mode, the
messages sent or received from the session are automatically acknowledged just
like auto mode, albeit lazily. Under rare circumstances, the messages might be
delivered more than once. This mode enables at-least-once message delivery
guarantee.
3)
Client mode: When a session uses client mode, the messages sent
or received from the session are not acknowledged automatically. The
application must acknowledge the message receipt. This mode gives the
application (rather than the JMS provider) complete control over message
acknowledgement, at the cost of increased code complexity.
For details acknowledgement mode
please refer
https://docs.tibco.com/pub/businessevents-express/5.2.1/doc/html/GUID-0FE17DCF-554E-4860-8490-980FDBC94D15.html
https://docs.tibco.com/pub/businessevents-express/5.2.1/doc/html/GUID-0FE17DCF-554E-4860-8490-980FDBC94D15.html
Transaction options:
1)
Transacted session: An application can participate in a transaction by
creating a transacted session (or local transaction). The application
completely controls the message delivery by either committing or rolling back
the session.
2)
Message-driven beans with CMTD: An MDB can participate in a container transaction
by specifying CMTD in the XML deployment descriptor. The transaction commits
upon successful message processing or the application can explicitly roll it
back.
3)
Message-driven beans with BMTD: An MDB can choose not to participate in a
container transaction by specifying BMTD in the XML deployment descriptor. The
MDB programmer has to design and code programmatic transactions.
Type of Message supported by JMS
JMS Support following types of
messgaes
1)
Stream Messages
2)
Text Messages
3)
Map Messages
4)
Byte Messages
5)
Object Messages
If you acknowledge a message all previously received
message will also be acknowledged, but if we want to acknowledge a message
without affecting previously acknowledged message, use javax.jms.QueueBrowser
to review queued message.
QueueBrowser has getEnumeration which “Get an
enumeration for browsing the current messages in the order they would be
received”
Active
MQ Example-
Producer -
import
org.apache.activemq.ActiveMQConnectionFactory;
public class Producer implements Runnable {
public void run() {
try {
ActiveMQConnectionFactory
connectionFactory = new
ActiveMQConnectionFactory("tcp://localhost:61616");
Connection
connection = connectionFactory.createConnection();
connection.start();
Session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination
destination = session.createQueue("TestQueue");
MessageProducer
producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
String
text = "Hello
world!";
TextMessage
message = session.createTextMessage(text);
producer.send(message);
session.close();
connection.close();
}
catch (Exception e) {
System.out.println("Caught:
" +
e);
e.printStackTrace();
}
}
}
Consumer
import javax.jms.Connection;
import
javax.jms.Destination;
import javax.jms.Message;
import
javax.jms.MessageConsumer;
import javax.jms.Session;
import
javax.jms.TextMessage;
import
org.apache.activemq.ActiveMQConnectionFactory;
public class Consumer implements Runnable {
public void run() {
try {
ActiveMQConnectionFactory
connectionFactory = new
ActiveMQConnectionFactory("tcp://localhost:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
Session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination
destination = session.createQueue("TestQueue");
MessageConsumer
consumer = session.createConsumer(destination);
Message
message = consumer.receive(1000);
if (message instanceof TextMessage) {
TextMessage
textMessage = (TextMessage) message;
String
text = textMessage.getText();
System.out.println("Received:
" +
text);
}
else {
System.out.println("Received:
" +
message);
}
consumer.close();
session.close();
connection.close();
}
catch (Exception e) {
System.out.println("Caught:
" +
e);
e.printStackTrace();
}
}
}
public class TestMessage {
public static void main(String[] args) {
Producer
producer = new Producer();
Consumer consumer = new Consumer();
Thread threadProducer = new Thread(producer);
threadProducer.start();
Thread threadConsumer = new Thread(consumer);
threadConsumer.start();
}
}
uid/pwd – admi/admin default credential
Here we can se the Message publish on TestQueue
very usefull in full details thanks
ReplyDelete