Exercise -2
Aim: Lab on IPC, messaging, publish/subscribe
Purpose:
The purpose of this experiment is to understand how independent processes
communicate using message-based mechanisms and how the
publish/subscribe model enables efficient data exchange without direct
coupling between the sender and receiver.
Prerequisites: -
1) Hardware Requirements: -
Processor: Intel i3 / equivalent or higher
RAM: Minimum 6 GB
Hard Disk: Minimum 100 GB free space
2) Software Requirements: -
Operating System: Windows 10 / Windows 11
Java Development Kit: Adoptium JDK 17
Apache Maven
Spring Boot Framework
Visual Studio Code
Postman
Procedure: -
1. Install and configure Java, Maven, and Spring Boot on the system.
2. Create two separate Spring Boot applications:
i. One for Publisher
ii. One for Subscriber
3. Develop REST endpoints in the Publisher application to publish
messages.
4. Implement a Subscriber application to receive and process messages.
5. Run both applications as separate processes.
6. Use Postman to send messages from the Publisher.
7. Observe message reception at the Subscriber application, demonstrating
IPC using messaging and publish/subscribe model.
1. Install and configure Java, Maven, and Spring Boot on the system.
Install JDK17 from Adoptium and set JAVA_HOME system Variable, Install
Maven from Apache Maven binary zip file and set MAVEN_HOME system
variable and edit path system variable to accommodate up to bin for both
JDK and Maven, Customize the SpringBoot Frame work for both Publisher
and Subscriber from [Link] (Spring Initializer IO website)
2. Create two separate Spring Boot applications:
i) Steps to Create Publisher Spring Boot Application
a) Open a web browser and navigate to [Link]
b) Configure the project with the following details:
Project: Maven Project
Language: Java
Spring Boot Version: 3.2.x (latest stable)
Group: [Link]
Artifact: publisher
Packaging: Jar
Java Version: 17
c) Click on Add Dependencies, search for Spring Web, and add it to the
project.
d) Click on Generate, which downloads the project as a zip file (e.g.,
[Link]).
e) Extract the downloaded zip file into the
Users→Documents→SpringBootProjects folder and open it in Visual
Studio Code.
ii. Steps to Create Subscriber Spring Boot Application
a. Open a web browser and navigate to [Link]
b. Configure the project with the following details:
Project: Maven Project
Language: Java
Spring Boot Version: 3.2.x (latest stable)
Group: [Link]
Artifact: subscriber
Packaging: Jar
Java Version: 17
c. Click on Add Dependencies, search for Spring Web, and add it
to the project.
d. Click on Generate, which downloads the project as a zip file
(e.g., [Link]).
e. Extract the downloaded zip file into the
Users → Documents → SpringBootProjects folder and open it in
Visual Studio Code.
Note:- Open Two different VS code Panels for easy running of Publisher and
Subscriber.
3. Develop REST endpoints in the Publisher application to publish messages.
i. Open publisher Folder in VS Code.
ii. In src/main/java/com/example/publisher (package path), create a new
Java class called [Link]
Source Code: -
iii. Publisher - REST Controller (This publishes the Message)
Filename: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@RestController
public class MessagePublisherController {
@PostMapping("/publish")
public String publishMessage(@RequestBody String message) {
// RestTemplate for IPC communication
RestTemplate restTemplate = new RestTemplate();
// Subscriber URL (different process)
String subscriberUrl = "[Link]
// Send message to Subscriber
[Link](subscriberUrl, message, [Link]);
return "Message published successfully: " + message;
}
}
4. Implement a Subscriber application to receive and process messages.
i. Open subscriber Folder in VS Code.
ii. In src/main/java/com/example/subscriber (package path), create a new Java
class called [Link]
Subsciber - REST Controller (This recieves the Message)
Filename: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
@RestController
public class MessageSubscriberController {
@PostMapping("/subscribe")
public String receiveMessage(@RequestBody String message) {
[Link]("Message received by subscriber: " + message);
return "Message received successfully";
}
}
5. Run both applications as separate processes.
i) Run Subscriber first (run it in subscriber VSCode pannel)
mvn spring-boot:run
Note: - Subscriber is running on port 8080
Run Publisher on port 8081 for that Set Publisher port to 8081
Open in subscriber project:
src/main/resources/[Link]
Add: [Link]=8081
Save it
ii) Run Publisher Next (run it in publisher VSCode pannel)
mvn spring-boot:run
6) Use Postman to send messages from the Publisher.
i. create a POST request in POSTMAN with
URL: [Link]
ii. Choose raw option in body and give the message
for eg: Hello Anusha from Publisher!
and Send
iii. You will get the response