1

我正在尝试开发一个小型 spring rmi 示例。

我创建了远程服务接口和服务如下,

计算.java

package com;

public interface Calculation {
  String getName(String name);

}

CalImp:

package com;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CalImp implements Calculation {
    static void init(){
        ApplicationContext context = new
                 ClassPathXmlApplicationContext("applicationContext.xml");
        context.getBean("calculationBean");
    }
    String names;
    public String getName(String name){
        return names;
    }
}

我的应用程序上下文文件是

<?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.CalImp"></bean>

    <bean class="org.springframework.remoting.rmi.RmiServiceExporter" destroy-method="destroy">
        <property name="serviceName" value="Calculation"></property>
        <property name="service" ref="calculationBean"></property>
        <property name="serviceInterface" value="com.Calculation"></property>
        <property name="replaceExistingBinding" value="true"></property>
        <property name="registryPort" value="1199"></property>
    </bean>
</beans>

然后我的客户类如下,

客户端-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="easyClient" class="com.tutorialspoint.Client">
         <property name="easyServer" ref="calculationBean"/>
    </bean>

    <bean id="calculationBean" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
        <property name="serviceUrl" value="rmi://192.168.2.39:1199/Calculation"></property>
        <property name="serviceInterface" value="com.Calculation"></property>
    </bean>
</beans>

客户端.Java:

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.Calculation;

public class Client {
private Calculation easyServer;
public void setEasyServer(Calculation easyServer) {
    this.easyServer = easyServer;
 }



 public String  getdata(){
     ApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");
     easyServer =(Calculation) context.getBean("easyClient");
     String name=easyServer.getName("praveen");
     return name;
 }
}

我尝试使用以下测试实用程序对其进行测试,

1) 测试.java

package com;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Registry registry;
        String serverAddress="localhost";
        String serverPort="1199";
        String text="Hi";
        System.out.println("sending "+text+" to "+serverAddress+":"+serverPort);
        try{
        // get the “registry”
        registry=LocateRegistry.getRegistry(serverAddress,(new Integer(serverPort)).intValue());
        registry.lookup("Calculation") ;

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }   
}

当我尝试这个时,我得到了以下异常,即使尝试使用我的 ip "192.#.#.#",但仍然没有运气。

sending Hi to localhost:1199
java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: 
    java.net.ConnectException: Connection refused: connect
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
    at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
    at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
    at sun.rmi.server.UnicastRef.newCall(Unknown Source)
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
    at com.Test.main(Test.java:20)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
    ... 6 more

有人可以在这个问题上帮助我吗?

4

0 回答 0