RMI服务器判断调用它的客户端的主机名

时间:2015-08-25 06:17:49

标签: java rmi

我有以下程序的Spring RMI,其中客户端发送其端口号在服务器端收到并正在显示,我正在使用spring rmi来实现我现在想要的任何方式,因为我想要现在只需客户端调用servre RMI服务器并且RMI服务器应该足够聪明地判断客户端主机名,请在下面的程序中告知f我停止从客户端传递主机名,删除硬编码是客户端不会发现它的主机名那么服务器将如何足够聪明地判断客户端主机名本身

计算接口应该有一个将String作为参数的方法。

public interface Calculation {  
void print(String text );  
void registerIpAddress(String ip);
} 

public class CalculationImpl implements Calculation {

@Override
void print(String text) {
    System.out.println(text);
}
    @Override
    void registerIpAddress(String ip){
            List.add(ip);//or whatever you want
}

客户端和主机的xml文件都是正确的ApplicationContext.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>

client-beans.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="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://localhost:1099/CalculationService"></property>
<property name="serviceInterface" value="com.javatpoint.Calculation"></property>
</bean>
</beans>

创建一个新的applicaionContext实例,以使其正常工作

class server {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    }
}

需要运行您的客户端

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");
    Calculation calculation = (Calculation) context.getBean("calculationBean");
    calculation.print("Copied&Pasted!");

    try {
        calculation.registerIpAddress(InetAddress.getLocalHost().getHostAddress());
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
在谷歌上搜索我可以看到方法

public static String getClientHost()
                            throws ServerNotActiveException

现在为此我需要扩展远程服务器类,我可以在spring rmi本身实现相同的功能

0 个答案:

没有答案