配置RMI主机,以便它知道哪个客户端端口正在访问它

时间:2015-08-22 12:16:33

标签: java spring rmi

我一直致力于Spring RMI,我已经建立了一个主机和一个客户端。

以下是我的主机的代码。我想以这样一种方式修改它,即主机应该知道哪个客户端正在访问它,所以换句话说服务器应该知道哪个客户端端口正在访问它。

如何在Spring RMI中实现这一目标?

界面: -

package com.javatpoint;  

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

班级: -

package com.javatpoint;  

public class CalculationImpl implements Calculation{  

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

}  

最后是主机配置 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>  

以下是我使用的课程

界面: -

package com.javatpoint;

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

实施班级: -

public class CalculationImpl implements Calculation{

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

}

主要课程

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");

    }
}

现在请建议如何添加方法获取客户端主机提前感谢

2 个答案:

答案 0 :(得分:2)

您可以通过多种方式解决问题。

1)您可以在远程接口中添加一个方法,如registerClient(String clientId,Object clientInfo)。您可以在此方法的其他参数中传递其他相关信息。 RMI服务器可以简单地将此信息放在ConcurrentHashMap等数据结构中。

2)在调用registerClient之后,您可以通过将clientId作为其中一个参数传递给RMI服务器中的其他远程方法,例如cube(String clientId,int num);

3)如果您只需要客户端IP,则Java已在getClientHost()中提供RemoteServer API,其中提供了您要查找的信息。

答案 1 :(得分:2)

  

配置RMI主机,以便它知道哪个客户端端口正在访问它

  1. 没有&#39;配置&#39;将提供该信息的RMI主机。

  2. 客户端端口信息完全没用。

    • 每位客户并不是唯一的。
    • 每个客户都没有修复。
    • 它可以而且会改变客户的生活。
  3. Remote Session Pattern给我听起来像是个案。