JMS Java
JMS stands for Java Message Service. It is a Java API (Application Programming Interface) provided by Java EE (Java Platform, Enterprise Edition) for creating, sending, receiving, and processing messages between different components of a distributed application. The primary goal of JMS is to facilitate communication and integration between various software components in a loosely coupled and asynchronous manner.
Key concepts of JMS:
Messaging Models: JMS supports two messaging models – point-to-point (P2P) and publish/subscribe (pub/sub).
Point-to-Point (P2P): In this model, messages are sent from a single sender to a specific receiver. The receiver consumes the message and removes it from the queue. Only one receiver can consume a message.
Publish/Subscribe (Pub/Sub): In this model, messages are sent to multiple subscribers. Subscribers receive a copy of the message without removing it from the topic.
JMS Providers: JMS is a specification, and to use it, you need an implementation provided by a JMS provider. Examples of JMS providers include Apache ActiveMQ, IBM MQ, and JBoss Messaging.
JMS Components:
ConnectionFactory: It is used to create JMS connections to the JMS provider.
Destination: It represents the destination where messages are sent and received, either a queue (for P2P) or a topic (for pub/sub).
Producer: It is responsible for sending messages to a destination.
Consumer: It receives messages from a destination.
Message Types: JMS supports various message types, such as TextMessage (for sending text-based messages), ObjectMessage (for sending Java objects), MapMessage (for sending key-value pairs), and more.
Message-Driven Beans (MDBs): In Java EE, you can use Message-Driven Beans to consume messages asynchronously in a managed environment.
Here’s a simple example of using JMS in Java:
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JmsExample {
public static void main(String[] args) {
try {
// Create a connection factory
ConnectionFactory factory = new ActiveMQConnectionFactory(“tcp://localhost:61616”);
// Create a connection
Connection connection = factory.createConnection();
connection.start();
// Create a session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create a destination (queue or topic)
Queue queue = session.createQueue(“exampleQueue”);
// Create a producer to send messages to the queue
MessageProducer producer = session.createProducer(queue);
// Create a message
TextMessage message = session.createTextMessage(“Hello, JMS!”);
// Send the message
producer.send(message);
// Close the resources
producer.close();
session.close();
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
Keep in mind that to run this example, you need to have a JMS provider, such as Apache ActiveMQ, installed and running. The example above uses ActiveMQ as the JMS provider.
JMS is a powerful tool for building distributed and decoupled applications, enabling reliable messaging between different components.
Demo Day 1 Video:
Conclusion:
Unogeeks is the No.1 Training Institute for Java Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Java Training here – Java Blogs
You can check out our Best in Class Java Training details here – Java Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks