Currently trying to learn Java EE 8 (Workplace demand)
Made a separate maven project named “Shared”, that contains one @Statefull EJB
to preserve, Messages.
MessageRepository.java
@Stateful
public class MessageRepository {
public List<Message> messages = new ArrayList<>();
public String saveMessage(Message message){
messages.add(message);
return "Message Sent!";
}
public List<Message> fetchMessages() {
return messages;
}
}
Message.java
public class Message implements Serializable {
private String sender;
private String content;
private LocalDateTime timeStamp;
// getters & setters
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>shared</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>ejb</packaging>
<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/jakarta.platform/jakarta.jakartaee-api -->
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>10.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
After this, I created a jar
file of “Shared”, and inserted the <group>, <artifact> & <version>
as dependency
inside two other maven project’s pom.xml
, named App-A & App-B. I’ve also deployed this shared.jar in Wildfly 33.
Now, the intention is:
1. App-A & App-B will send message to shared. Shared will save that message
2. App-A can see it's sent messages as well as App-B's messages.
3. App-B can see it's sent messages as well as App-A's messages.
Here is App-A’s classes:
@Path("/messages")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApplicationScoped
public class App_AControllers {
@Inject
App_AService appAService;
@GET
@Path("/receive")
public Response getMessages() {
List<Message> messages = appAService.getMessages();
return Response.status(Response.Status.OK).entity(messages).build();
}
@POST
@Path("/send")
public Response sendMessage(SendMessageDto sendMessageDto) {
String response = appAService.sendMessage(sendMessageDto);
return Response.status(Response.Status.OK).entity(response).build();
}
}
public class SendMessageDto {
private String sender;
private String content;
public SendMessageDto() {}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
@ApplicationScoped
public class App_AService {
@Inject
MessageRepository messageRepository;
public String sendMessage(SendMessageDto sendMessageDto) {
Message message = new Message();
message.setSender(sendMessageDto.getSender());
message.setContent(sendMessageDto.getContent());
message.setTimeStamp(LocalDateTime.now());
messageRepository.saveMessage(message);
return "Message Sent!";
}
public List<Message> getMessages() {
return messageRepository.fetchMessages();
}
}
App-B’s classes are similar, just name changes.
Issue:
1. App-A & App-B can send & receive their own messages.
2. App-A's messages aren't appearing in App-B's /receive, neither the vice-versa.
I tried implementing a Webclient approach, like inside App-A a client to call App-B’s /receive, but that failed to deploy.
I’m not very sure, how to keep messages of both apps, in “Shared”.
If in case a detailed look on the code is required here is the github repo – https://github.com/sandeepRoy/Learning-Java-EE/tree/main/Inter%20App
Please consider me a beginner in Java EE, as I’m from a Spring boot background.
1