【问题标题】:sending parameter from client to server through RMI通过 RMI 从客户端向服务器发送参数
【发布时间】:2015-08-24 06:08:32
【问题描述】:

我必须通过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>

【问题讨论】:

  • 您的问题有点不清楚,您是否希望远程对象能够接收示例'test1111'的字符串并打印它?!
  • 是的,请告知如何实现这一点,我正在尝试远程对象能够接收字符串

标签: java spring rmi


【解决方案1】:

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

【讨论】:

  • core ...非常感谢,请您告知我们如何发送端口号,因为客户端是多个并且它们必须在不同的端口中运行,因此服务器应该知道请求来自哪个客户端所以要知道客户端端口是必要的谢谢提前
  • 如果您说的是在 serviceUrl 属性中指定的端口,那么所有客户端都必须使用相同的端口才能获取已在服务器注册表中设置的远程对象。跨度>
  • 感谢您的回复,不,我说的是不同的端口,即客户端在不同的端口上运行我想获取客户端的端口
  • 我不确定我是否明白每个客户端将在不同端口上运行的意思,但请检查this 问题,它可能会对您有所帮助!
  • 谢谢你的回复,我只想举个例子,假设上面的客户端运行在不同的机器上,有不同的 ip 现在我只想让那个客户端发送他们各自的 ip 到服务器程序现在仪式他们正在发送文本,请告知如何实现这一目标
猜你喜欢
  • 1970-01-01
  • 2020-12-27
  • 2019-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 1970-01-01
  • 2014-06-06
  • 1970-01-01
相关资源
最近更新 更多