1

I have been working on spring RMI and I have set up a host and one client.

Below is the code of my host. I want to modify it in such a way that host should know which client is accessing it, so in other words server should know which client port is accessing it.

How can I achieve this in Spring RMI?

interface :-

package com.javatpoint;  

public interface Calculation {  
int cube(int number);  
}  

class :-

package com.javatpoint;  

public class CalculationImpl implements Calculation{  

    @Override  
    public int cube(int number) {  
        return number*number*number;  
    }  

}  

and finally the host configuration xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
http://www.springframework.org/schema/beans/spring-beans.xsd">  

<bean id="calculationBean" class="com.javatpoint.CalculationImpl"></bean>  
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">  
    <property name="service" ref="calculationBean"></property>  
    <property name="serviceInterface" value="com.javatpoint.Calculation"></property>  
    <property name="serviceName" value="CalculationService"></property>  
    <property name="replaceExistingBinding" value="true"></property>  
    <property name="registryPort" value="1099"></property>  
</bean>  
</beans>  

following are my classes that are used

interface :-

package com.javatpoint;

public interface Calculation {
int cube(int number);
}

implementation class :-

public class CalculationImpl implements Calculation{

    public int cube(int number) {
        return number*number*number;
    }

}

and the main class

package com.javatpoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Host
{
    public static void main(String[] args)
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("Waiting for requests");

    }
}

now please advise how to add the method get client host Thanks in advance

  • 1
    Why? It isn't fixed. It doesn't convey any useful information. – user207421 Aug 22 '15 at 23:04
  • @EJP Well there is a scenario in which many different clients are accessing my rmi service and that clients are running on different ports itself so iu just want to know which client from which port is accessing my service – tuntun82828282 hdjscd Aug 23 '15 at 03:04
  • also please advise can i implement my own custom interceptor which will catch the port that is the info tht which client is accessing the rmi service – tuntun82828282 hdjscd Aug 23 '15 at 03:06
  • Those clients are running on *random* ports, which can and will change over the life of the client, and can accidentally be the same for different clients on different hosts. It's pointless. If you need to distinguish among clients, have them login and provide them with a token, or use a separate remote session object per client. – user207421 Aug 23 '15 at 20:56

2 Answers2

3

configuring RMI host so that it should know which client port is accessing it

  1. There is no 'configuration' of the RMI host that will deliver that information.

  2. The client port information is completely and utterly useless.

    • It isn't unique per client.
    • It isn't fixed per client.
    • It can and will change over the life of the client.

It sounds like a case for the Remote Session Pattern to me.

Community
  • 1
  • 1
user207421
  • 305,947
  • 44
  • 307
  • 483
2

You can solve the problem in multiple ways.

1) You can add one method like registerClient(String clientId, Object clientInfo) in remote interface. You can pass other relevant information in additional parameters of this method. RMI server can simple put this info in data structure like ConcurrentHashMap.

2) After invoking registerClient, you can invoke other remote methods in RMI server by passing clientId as one of the parameters in Remote methods like cube(String clientId, int num);

3) If you just need only client IP, Java already provides getClientHost() API in RemoteServer which provides the information you are looking for.

user207421
  • 305,947
  • 44
  • 307
  • 483
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
  • Thanks a lot can you please advise how can I add this method get client host(), will I get this method I remote service exporter class of spring, I will be really thankful to you if you can please show or you can even modify my above program itself that will be really helpful Thanks in advance – tuntun82828282 hdjscd Aug 24 '15 at 00:02
  • CalculationImpl will have access to this method. This method was defined in RemoteServer. Any object,which is instance of RemoteServer will have access to it. – Ravindra babu Aug 24 '15 at 00:58
  • I have added the core classes please advise how to add the method get client host() now in my above ojo thanks in advance – tuntun82828282 hdjscd Aug 24 '15 at 04:00
  • Err, this is Spring RMI. The methods of `RemoteServer` do not apply. – user207421 Aug 24 '15 at 04:30
  • @EJP yeah Please advise how to overcome from this Thansk in advance – tuntun82828282 hdjscd Aug 24 '15 at 06:20
  • A quick short cut for you. String address = InetAddress.getLocalHost().getHostAddress(); // Add this in your client . Pass this address in calc method as a parameter – Ravindra babu Aug 24 '15 at 08:14
  • If you are medium to advanced java developer, you can look at EJP proposed answer : Remote Session Pattern – Ravindra babu Aug 24 '15 at 08:19