【问题标题】:How to integrate a Spring RMI server with a pure Java RMI client which is a non-spring Swing GUI?如何将 Spring RMI 服务器与作为非 Spring Swing GUI 的纯 Java RMI 客户端集成?
【发布时间】:2023-03-12 00:45:02
【问题描述】:

我正在将一个 J2EE EJB 应用程序迁移到 Spring 服务。它是一个桌面应用程序,它有一个 Swing GUI 并与它使用 RMI 的 J2EE 服务器进行通信。我用spring boot创建了一个简单的spring服务,它通过使用spring远程处理RMIServiceExporter来导出一个服务。该客户端是一个富客户端并且具有复杂的架构,所以我尝试对其进行最小的更改以调用 spring rmi 服务。

总之,我有一个普通的 RMI 客户端和一个 Spring RMI 服务器。我了解到 spring rmi 抽象了纯 java rmi,所以在我的情况下它们不能互操作。

我将在下面显示代码,但当前的错误是这样的。请注意,我当前的项目使用“remote://”。所以在我得到这个错误之后,我也尝试了“rmi://”。但是,在这两种情况下都会出现此错误。

javax.naming.CommunicationException: Failed to connect to any server. Servers tried: [rmi://yyy:1099 (No connection provider for URI scheme "rmi" is installed)]
                at org.jboss.naming.remote.client.HaRemoteNamingStore.failOverSequence(HaRemoteNamingStore.java:244)
                at org.jboss.naming.remote.client.HaRemoteNamingStore.namingStore(HaRemoteNamingStore.java:149)
                at org.jboss.naming.remote.client.HaRemoteNamingStore.namingOperation(HaRemoteNamingStore.java:130)
                at org.jboss.naming.remote.client.HaRemoteNamingStore.lookup(HaRemoteNamingStore.java:272)
                at org.jboss.naming.remote.client.RemoteContext.lookupInternal(RemoteContext.java:104)
                at org.jboss.naming.remote.client.RemoteContext.lookup(RemoteContext.java:93)
                at org.jboss.naming.remote.client.RemoteContext.lookup(RemoteContext.java:146)
                at javax.naming.InitialContext.lookup(InitialContext.java:417)
                at com.xxx.ui.common.communication.JbossRemotingInvocationFactory.getRemoteObject(JbossRemotingInvocationFactory.java:63)
                at com.xxx.gui.comm.CommManager.initializeSpringEJBz(CommManager.java:806)
                at com.xxx.gui.comm.CommManager.initializeEJBz(CommManager.java:816)
                at com.xxx.gui.comm.CommManager.initializeAndLogin(CommManager.java:373)
                at com.xxx.gui.comm.CommManager$2.doInBackground(CommManager.java:273)
                at javax.swing.SwingWorker$1.call(SwingWorker.java:295)
                at java.util.concurrent.FutureTask.run(FutureTask.java:266)
                at javax.swing.SwingWorker.run(SwingWorker.java:334)
                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
                at java.lang.Thread.run(Thread.java:745)

我已经搜索了如何使 spring rmi 和普通/纯 java rmi 互操作,并且我从 stackoverflow 和 web 上的类似问题中阅读了几个答案,但我找不到任何有用或适合我的情况,因为即使是最匹配的答案也说只是它不能互操作。

我想也许我需要使用 spring boot 将我的 swing gui 客户端转换为 spring,但我不能确定应用程序上下文,因为我不想破坏现有的客户端代码。所以我一直在寻找可能有类似部分 spring 上下文的东西,这样我可能只能将我的 CommManager.java 客户端代码放入其中,而 spring 只管理这个文件。

然后我想也许我需要更改我的 RMI 服务器以强制 spring 创建某种普通/纯 Java RMI 而不是默认的 spring RMI。我之所以这么说,是因为我读过一些关于 spring rmi 的文章,它解释了它是对 rmi 的抽象,我们可以强制它创建标准的 RMI 存根。

在寻找解决方案时,我遇到了 Spring Integration,但我无法真正理解它,因为它看起来像其他抽象,但它也说明了有关适配器的一些信息。因为我见过“适配器”,所以它可能用于这种集成/遗留代码迁移案例。但我不能走得更远。

客户端:

CommManager.java

private boolean initializeEJBz(String userName, String password) throws Exception {
        ...
        ri = RemoteInvocationFactory.getRemoteInvocation(user, pass);
        if (ri != null) {
            return initializeEJBz(ri);
        } else {
            return false;
        }
    }

RemoteInvocationFactory.java

package com.xxx.ui.common.communication;

import javax.naming.NamingException;

public final class RemoteInvocationFactory {
    private static final CommunicationProperties cp = new CommunicationProperties();

    public static synchronized RemoteInvocation getRemoteInvocation(
            byte[] userName, byte[] password) throws NamingException {
        String url = System.getProperty("rmi://xxx.com:1099");
        if (url != null) {
            return new JbossRemotingInvocationFactory(userName, password, url);
        }
        return null;
    }
...

JbossRemotingInvocationFactory.java

package com.xxx.ui.common.communication;

...
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
...
import java.util.Hashtable;
import java.util.concurrent.TimeUnit;

public class JbossRemotingInvocationFactory implements RemoteInvocation {
    private final byte[] userName, password;
    private final String providerURL;
    private volatile InitialContext initialContext;
    private final SecretKey secretKey;
    private static final String SSL_ENABLED = "jboss.naming.client.connect.options.org.xnio.Options.SSL_ENABLED";
    private static final String SSL_STARTTLS = "jboss.naming.client.connect.options.org.xnio.Options.SSL_STARTTLS";
    private static final String TIMEOUT = "jboss.naming.client.connect.timeout";

    private long timeoutValue;
    private final boolean startSsl;


    @SuppressWarnings("unchecked")
    public JbossRemotingInvocationFactory(byte[] userName, byte[] password, String providerURL) {
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            keyGenerator.init(128);
            secretKey = keyGenerator.generateKey();
            this.providerURL = providerURL;
            startSsl = Boolean.valueOf(System.getProperty(SSL_ENABLED));
            String property = System.getProperty("myproject.connect.timeout");
            if (property != null) {
                try {
                    timeoutValue = TimeUnit.MILLISECONDS.convert(Long.parseLong(property), TimeUnit.SECONDS);
                } catch (Exception e) {
                    timeoutValue = TimeUnit.MILLISECONDS.convert(10, TimeUnit.SECONDS);
                }
            }
            Hashtable jndiProperties = new Hashtable();
            this.userName = encrypt(userName);
            addOptions(jndiProperties);
            jndiProperties.put(Context.SECURITY_CREDENTIALS, new String(password, UTF_8));
            initialContext = new InitialContext(jndiProperties);
            this.password = encrypt(password);
        } catch (NamingException | NoSuchAlgorithmException ne) {
            throw new RuntimeException(ne);
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T> T getRemoteObject(Class<T> object, String jndiName) throws NamingException {
        if (initialContext != null) {
            T value = (T) initialContext.lookup(jndiName);
            initialContext.removeFromEnvironment(Context.SECURITY_CREDENTIALS);
            initialContext.removeFromEnvironment(Context.SECURITY_PRINCIPAL);
            return value;
        } else {
            throw new IllegalStateException();
        }
    }

    @Override
    public <T> T getRemoteObject(Class<T> object) throws NamingException {
        throw new IllegalAccessError();
    }

    ...


    private void addOptions(Hashtable jndiProperties) {
        jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        jndiProperties.put("jboss.naming.client.ejb.context", "true");
        jndiProperties.put("jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS", "false");
        jndiProperties.put("jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT", "false");
        jndiProperties.put(SSL_STARTTLS, "false");
        jndiProperties.put(TIMEOUT, Long.toString(timeoutValue));
        if (startSsl) {
            jndiProperties.put("jboss.naming.client.remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "true");
            jndiProperties.put(SSL_ENABLED, "true");
        }
        jndiProperties.put("jboss.naming.client.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS", "JBOSS-LOCAL-USER");
        jndiProperties.put(Context.PROVIDER_URL, providerURL);
        jndiProperties.put(Context.SECURITY_PRINCIPAL, new String(decrypt(userName), UTF_8));
    }

    @Override
    public void reconnect() {
        try {
            Hashtable jndiProperties = new Hashtable();
            addOptions(jndiProperties);
            jndiProperties.put(Context.SECURITY_CREDENTIALS, new String(decrypt(password), UTF_8));
            initialContext = new InitialContext(jndiProperties);
        } catch (NamingException ignore) {
        }
    }
}

CommManager.java

private boolean initializeEJBz(RemoteInvocation remoteInvocation) throws Exception {
        cs = remoteInvocation.getRemoteObject(CustomerService.class, JNDINames.CUSTOMER_SERVICE_REMOTE);
       ...

        // here is the integration point. try to get RMI service exported.
        myService = remoteInvocation.getRemoteObject(HelloWorldRMI.class, JNDINames.HELLO_WORLD_REMOTE);


        return true;
}
public static final String CUSTOMER_SERVICE_REMOTE = getRemoteBean("CustomerServiceBean", CustomerService.class.getName());

public static final string HELLO_WORLD_REMOTE = getRemoteBean("HelloWorldRMI", HelloWorldRMI.class.getName());

...

private static final String APPLICATION_NAME = "XXX";
private static final String MODULE_NAME = "YYYY";

...

protected static String getRemoteBean(String beanName, String interfaceName) {
        return String.format("%s/%s/%s!%s", APPLICATION_NAME, MODULE_NAME, beanName, interfaceName);
    }

服务器端:

HelloWorldRMI.java:

package com.example.springrmiserver.service;

public interface HelloWorldRMI {
    public String sayHelloRmi(String msg);
}

HelloWorldRMIImpl:

package com.example.springrmiserver.service;

import java.util.Date;

public class HelloWorldRMIimpl implements HelloWorldRMI {

    @Override
    public String sayHelloRmi(String msg) {
        System.out.println("================Server Side ========================");
        System.out.println("Inside Rmi IMPL - Incoming msg : " + msg);
        return "Hello " + msg + " :: Response time - > " + new Date();
    }
}

Config.java:

package com.example.springrmiserver;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.rmi.RmiServiceExporter;
import org.springframework.remoting.support.RemoteExporter;

import com.example.springrmiserver.service.HelloWorldRMI;
import com.example.springrmiserver.service.HelloWorldRMIimpl;

@Configuration
public class Config {

    @Bean
    RemoteExporter registerRMIExporter() {

        RmiServiceExporter exporter = new RmiServiceExporter();
        exporter.setServiceName("helloworldrmi");
        //exporter.setRegistryPort(1190);
        exporter.setServiceInterface(HelloWorldRMI.class);
        exporter.setService(new HelloWorldRMIimpl());

        return exporter;
    }


}

SpringServerApplication.java:

package com.example.springrmiserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Collections;

@SpringBootApplication
public class SpringRmiServerApplication {

    public static void main(String[] args)
    {
        //SpringApplication.run(SpringRmiServerApplication.class, args);
        SpringApplication app = new SpringApplication(SpringRmiServerApplication.class);
        app.setDefaultProperties(Collections.singletonMap("server.port", "8084"));
        app.run(args);
    }
}

那么,我的问题是如何与 spring rmi 服务器在 swing GUI 中互操作纯/普通/标准 java rmi 客户端?

编辑#1:

顺便说一句,如果您可以提供有关 spring RMI 存根创建的内部细节以及它们为什么不能互操作的进一步解释或链接,我会很高兴。真的谢谢。

另外,如果您查看我的 getRemoteBean 方法,它来自遗留代码,这个查找字符串是如何工作的?我的意思是 rmi 注册表文件或其他东西驻留在服务器的什么位置,或者这是默认格式还是我可以自定义它?

编辑#2: 我也在客户端尝试过这种查找:

private void initializeSpringEJBz(RemoteInvocation remoteInvocation) throws Exception {
    HelloWorldRMI helloWorldService = (HelloWorldRMI) Naming.lookup("rmi://xxx:1099/helloworldrmi");
    System.out.println("Output" + helloWorldService.sayHelloRmi("hello "));
    //hw = remoteInvocation.getRemoteObject(HelloWorldRMI.class, "helloworldrmi");
}

编辑#3:

在搜索时,我发现 spring 论坛中有人建议强制 spring 创建普通的 java rmi 存根,我们必须在服务器端进行一些更改,所以我尝试了这个:

import java.rmi.server.RemoteObject;

public interface HelloWorldRMI extends **Remote** {
   public String sayHelloRmi(String msg) throws **RemoteException**;
   ...
}

...

public class HelloWorldRMIimpl extends **RemoteObject** implements HelloWorldRMI {
...
}

上面的代码在解决问题的正确路径上吗?

除此之外,第一个问题是连接设置,正如您在问题开头看到的那样。为什么我收到此错误? "rmi://" 和 "remote://" 有什么区别?

【问题讨论】:

  • 你不能。您必须在客户端使用 Spring。
  • 感谢@user207421 的回复,但为了全面披露,我已尝试在问题中提供详细信息,以询问实现此目标的可能性。所以在我还问过的问题中,是否可以让我的客户部分基于弹簧,即只有 CommManager 使用弹簧。正如你所说,如果只有让我的客户使用弹簧才有可能,我怎么能部分实现这一点?在这种情况下,问另一个问题可能更合适,但我只是想在客户端中使用 spring 唯一的 rmi 功能。
  • 要么使用 Spring RMI,要么不使用。我看不出“部分”是如何进入其中的。
  • 对不起,我无法清楚地说明。我的意思是,在这种情况下,是否可以将 spring 的应用程序上下文设置为仅由 CommManager 而不是整个 GUI 客户端(即主要方法)使用?这样我就可以使用 Spring 只通过注入 RMI 相关的 bean 来管理 CommManager。

标签: spring spring-boot jakarta-ee jndi rmi


【解决方案1】:

当我试图弄清楚时,我能够找到解决方案。 Spring RMI 和 Java RMI 确实不能互操作,但目前我没有足够的知识来解释其原因。我还没有找到任何关于这种不匹配的内部的完整解释。

解决方案是通过java.rmi.*(Remote, RemoteException and server.UnicastRemoteObject) 在 Spring 后端使用纯 Java RMI。

java.rmi.server.UnicastRemoteObject 用于使用 Java 远程方法协议 (JRMP) 导出远程对象并获取与远程对象通信的存根。

编辑: 我认为这篇文章与这个互操作性问题密切相关:Java Spring RMI Activation

Spring 不支持 RMI 激活。 Spring 包含一个用于调用远程对象的 RmiServiceExporter,它包含对标准 RMI 的良好改进,例如不需要服务扩展 java.rmi.Remote。

解决方案:

这是服务器导出的接口:

package com.xxx.ejb.interf;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface HelloWorldRMI extends Remote {
    public String sayHelloRmi(String msg) throws RemoteException;
}

这是导出类的实现:

package com.xxx.proxyserver.service;

import com.xxx.ejb.interf.HelloWorldRMI;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;

public class HelloWorldRMIimpl extends UnicastRemoteObject implements HelloWorldRMI {

    public HelloWorldRMIimpl() throws RemoteException{
        super();
    }

    @Override
    public String sayHelloRmi(String msg) {
        System.out.println("================Server Side ========================");
        System.out.println("Inside Rmi IMPL - Incoming msg : " + msg);
        return "Hello " + msg + " :: Response time - > " + new Date();
    }
}

RMI 注册表是:

package com.xxx.proxyserver;

import com.xxx.proxyserver.service.CustomerServiceImpl;
import com.xxx.proxyserver.service.HelloWorldRMIimpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Collections;

@SpringBootApplication
public class ProxyServerApplication {

    public static void main(String[] args) throws Exception
    {
        Registry registry = LocateRegistry.createRegistry(1200); // this line of code automatic creates a new RMI-Registry. Existing one can be also reused.
        System.out.println("Registry created !");

        registry.rebind("just_an_alias",new HelloWorldRMIimpl());
        registry.rebind("path/to/service_as_registry_key/CustomerService", new CustomerServiceImpl());

        SpringApplication app = new SpringApplication(ProxyServerApplication.class);
        app.setDefaultProperties(Collections.singletonMap("server.port", "8084")); // Service port
        app.run(args);
    }
}

客户:

...
   HelloWorldRMI helloWorldService = (HelloWorldRMI)Naming.lookup("rmi://st-spotfixapp1:1200/just_an_alias");
   System.out.println("Output" + helloWorldService.sayHelloRmi("hello from client ... "));
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 2010-12-31
    • 1970-01-01
    相关资源
    最近更新 更多