Java
JMS
RMI
Programming Languages
Software Development

What is the difference between Java RMI and JMS?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Java RMI (Remote Method Invocation) and JMS (Java Messaging Service) are both integral parts of Java-based distributed computing, but they serve distinct purposes and operate under different paradigms. This distinction is especially crucial for software developers and system architects who need to choose the appropriate communication mechanism depending on the requirements of their applications.

Java RMI (Remote Method Invocation)

Java RMI is a mechanism that allows one Java Virtual Machine (JVM) to invoke methods on an object residing in another JVM. RMI facilitates the development of distributed Java applications, where the components can interact over a network. The objects communicating via RMI can pass and return primitives or serializable Java objects.

Key Operation Steps in RMI:

  1. Define the remote interfaces: These are Java interfaces that declare the methods that can be invoked remotely.
  2. Implement the remote interfaces: These implementations must extend UnicastRemoteObject and implement the remote interface.
  3. Compile and deploy the application: RMI requires generating stubs and skeletons; however, post Java 5, this process is automated.
  4. Use the Naming class for registry service: RMI uses a registry service to obtain references of remote objects.

Example of RMI:

java
1import java.rmi.Naming;
2
3public class Server {
4    public static void main(String args[]) {
5        try {
6            CalculatorImpl calc = new CalculatorImpl();
7            Naming.rebind("rmi://localhost:1099/CalculatorService", calc);
8            System.out.println("Calculator Service is ready.");
9        } catch (Exception e) {
10            System.out.println("Server failed: " + e);
11        }
12    }
13}

Java Messaging Service (JMS)

JMS is a Java API for sending messages between two or more clients. It is an integral part of the Java EE platform. JMS supports both message queueing and publish-subscribe styles of messaging. Unlike RMI, JMS is inherently asynchronous and loosely coupled, meaning the producer and consumer of the message do not need to be available at the same time.

Key Components of JMS:

  1. JMS Provider: An implementation of the JMS interface for message-oriented middleware (MOM).
  2. JMS Producer/Publisher: Sends messages to the JMS provider.
  3. JMS Consumer/Subscriber: Receives messages from the JMS provider.
  4. JMS Message: An object that encapsulates data in a format understandable to both the sender and receiver.

Example of JMS:

java
1import javax.jms.*;
2
3public class JmsProducer {
4    public static void main(String[] args) {
5        try {
6            QueueConnection connection = // Obtain connection
7            QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
8            TextMessage message = session.createTextMessage("Hello World");
9            QueueSender sender = session.createSender(queue);
10            sender.send(message);
11            connection.close();
12        } catch (Exception e) {
13            System.out.println("JMS Producer failed: " + e);
14        }
15    }
16}

Comparison Table

FeatureJava RMIJMS
Communication StyleSynchronousAsynchronous
CouplingTightLoose
ArchitectureMethod invocation on objectsProducer-Consumer messaging
Registry RequiredYes (RMI Registry)No (Broker-based, e.g., Apache ActiveMQ)
Use CaseReal-time systems, distributed objectsDecoupled systems, various subscribers
ReliabilityDependent on network connectionProvides delivery modes, durable subscriptions

Conclusion

Choosing between Java RMI and JMS largely depends on the specific needs of your application. For tightly coupled, synchronous interactions where method calls need to be made on remote objects, Java RMI is suitable. In contrast, for applications requiring high levels of decoupling, reliability, and asynchronous processing, JMS is preferable. Understanding these technologies and their suitable use cases ensures the design of more efficient and robust distributed systems.


Course illustration
Course illustration

All Rights Reserved.