简体   繁体   中英

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM