通过RMI从客户端向服务器发送参数

时间:2015-08-24 06:08:33

标签: java spring rmi

我必须通过Spring RMI将参数传递给客户端到服务器请告知如何实现以下是我编辑过的类

package com.javatpoint;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.remoting.support.RemoteInvocation;

@SuppressWarnings("serial")
public class CalculationImpl extends RemoteInvocation implements Calculation {

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


    public CalculationImpl (MethodInvocation methodInvocation) {
        super();

        // Invoked in superclass
        this.addAttribute("awer", "test1111");
    }


    private void addAttribute(String string, String string2) {


    }
}

和客户端的 xml

<beans>

    <bean id="calculationBean" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
        <property name="remoteInvocationFactory"  ref="invocationFactory"/>
        <property name="serviceUrl" value="rmi://localhost:1099/CalculationService"></property>
        <property name="serviceInterface" value="com.javatpoint.Calculation"></property>

    </bean>

    <bean id="invocationFactory" class="com.javatpoint.CalculationImpl"/>
</beans>

现在请建议如何自定义rmiservice导出器,以便它应该接收从客户端发送的值,如上所示,在客户端xml中,参数awer的值是test1111,现在我想自定义我的rmiservice导出器 所以它应该收到这个值并显示它

下面是我的 rmi服务导出器。

<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="registryPort" value="1099"></property>

    </bean>

1 个答案:

答案 0 :(得分:0)

在您的计算界面中,应该有一个将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();
    }
}