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

烂漫一生 提交于 2019-11-27 08:30:07

问题


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:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/32156010/configuring-rmi-host-so-that-it-should-know-which-client-port-is-accessing-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!