Inter App communication using a Remote EJB

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật