[BAEL 1209] - Java RMI Files. (#3374)

* [BAEL 1209] - Java RMI Files.

* Added parent tag and deleted dependency tag for junit.

* Added java-rmi module.

* Removed duplicate java-lite module entry.

* Deleting this file as it is covered in test class.

* Spell check.
This commit is contained in:
gautamshetty 2018-01-25 04:16:39 -06:00 committed by Grzegorz Piwowarek
parent 70e4a55256
commit 19e8849361
6 changed files with 151 additions and 2 deletions

20
java-rmi/pom.xml Normal file
View File

@ -0,0 +1,20 @@
<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.baeldung.rmi</groupId>
<artifactId>java-rmi</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -0,0 +1,36 @@
package com.baeldung.rmi;
import java.io.Serializable;
public class Message implements Serializable {
private String messageText;
private String contentType;
public Message() {
}
public Message(String messageText, String contentType) {
this.messageText = messageText;
this.contentType = contentType;
}
public String getMessageText() {
return messageText;
}
public void setMessageText(String messageText) {
this.messageText = messageText;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.rmi;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface MessengerService extends Remote {
public String sendMessage(String clientMessage) throws RemoteException;
public Message sendMessage(Message clientMessage) throws RemoteException;
}

View File

@ -0,0 +1,37 @@
package com.baeldung.rmi;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class MessengerServiceImpl implements MessengerService {
public String sendMessage(String clientMessage) {
String serverMessage = null;
if (clientMessage.equals("Client Message")) {
serverMessage = "Server Message";
}
return serverMessage;
}
public void createStubAndBind() throws RemoteException {
MessengerService stub = (MessengerService) UnicastRemoteObject.exportObject((MessengerService) this, 0);
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("MessengerService", stub);
}
public Message sendMessage(Message clientMessage) throws RemoteException {
Message serverMessage = null;
if (clientMessage.getMessageText().equals("Client Message")) {
serverMessage = new Message("Server Message", "text/plain");
}
return serverMessage;
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.rmi;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import org.junit.BeforeClass;
import org.junit.Test;
public class JavaRMIIntegrationTest {
@BeforeClass
public static void whenRunServer_thenServerStarts() {
try {
MessengerServiceImpl server = new MessengerServiceImpl();
server.createStubAndBind();
} catch (RemoteException e) {
fail("Exception Occurred");
}
}
@Test
public void whenClientSendsMessageToServer_thenServerSendsResponseMessage() {
try {
Registry registry = LocateRegistry.getRegistry();
MessengerService server = (MessengerService) registry.lookup("MessengerService");
String responseMessage = server.sendMessage("Client Message");
String expectedMessage = "Server Message";
assertEquals(responseMessage, expectedMessage);
} catch (RemoteException e) {
fail("Exception Occurred");
} catch (NotBoundException nb) {
fail("Exception Occurred");
}
}
}

View File

@ -87,8 +87,9 @@
<module>jackson</module>
<!-- <module>persistence-modules/java-cassandra</module> -->
<module>vavr</module>
<module>java-lite</module>
<module>java-vavr-stream</module>
<module>java-lite</module>
<module>java-rmi</module>
<module>java-vavr-stream</module>
<module>javax-servlets</module>
<module>javaxval</module>
<module>jaxb</module>