从基于Web的应用程序到基于桌面的应用程序

时间:2012-04-05 18:17:11

标签: java spring desktop-application rmi

我正在开发一个应用程序,它包括两个基于Web的应用程序,另一个是桌面应用程序,基于Web的应用程序将调用基于桌面的应用程序,这两个机器都在局域网中。应用程序是用Java编写的。有人建议我使用RMI。基于Web的应用程序将像RMIClient一样,桌面应用程序将是RMIServer。对于基于Web的应用程序,我们使用Spring Framework spring为内部使用Java RMI的RMI Service提供了自己的方式。我决定基于这种模式进行原型设计。

现在我使用Spring创建了RMIClient,请参阅下面的代码: -

package com.rmi.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MainController {

@RequestMapping("/callrmiserver")
public void callRMISever(){
    ApplicationContext ctx = new ClassPathXmlApplicationContext("ServiceBean.xml");
    System.out.print("before getbean");
    Hello hello = (Hello) ctx.getBean("helloService");
    System.out.print("i am greeting from "+hello.greeting());
   }

 }

ServiceBean.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"      xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id="Helloservice" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
    <property name="serviceUrl" value="rmi://localhost:1009/Services" />
    <property name="serviceInterface" value="com.rmi.client.Hello" />
</bean>

界面就像:

package com.rmi.client;

public interface Hello {
  String greeting();
}

基于桌面的RMIServer就像

package rmiapplication;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
public class RMIServer {
    public static void main(String[] args) throws RemoteException,  MalformedURLException {
       System.out.println("server conf...");
               LocateRegistry.createRegistry(1009);
               Hello hello = new HelloImpl();
               Naming.rebind("Services", hello);
               System.out.println("server.RMI Server is ready.");
        }

  }

方法的实施部分是: -

package rmiapplication;

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

公共类HelloImpl扩展UnicastRemoteObject实现Hello {

private static final long serialVersionUID = 6789759288861838122L;

public HelloImpl() throws RemoteException {
    }

    public String greeting() throws RemoteException {
        System.out.print("i am at server side call");
        return "greeting";
    }

    }

当我正在运行时是整个设置,从rmiregistry,RMIServer开始,然后是基于web的应用程序我得到java.rmi.NotBoundException这里是完整的堆栈跟踪: -

SEVERE: Servlet.service() for servlet [rmi] in context with path [/SpringRMIClient] threw exception [Request processing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.rmi.client.RMIService#0' defined in class path resource [ServiceBean.xml]: Cannot resolve reference to bean 'Helloservice' while setting bean property 'Helloservice'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Helloservice' defined in class path resource [ServiceBean.xml]: Invocation of init method failed; nested exception is org.springframework.remoting.RemoteLookupFailureException: Could not find RMI service [rmi://localhost:1009/Services] in RMI registry; nested exception is        java.rmi.NotBoundException: Services] with root cause
    java.rmi.NotBoundException: Services
at sun.rmi.registry.RegistryImpl.lookup(Unknown Source)
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Unknown Source)
at org.springframework.remoting.rmi.RmiClientInterceptor.lookupStub(RmiClientInterceptor.java:200)
at org.springframework.remoting.rmi.RmiClientInterceptor.prepare(RmiClientInterceptor.java:147)
at org.springframework.remoting.rmi.RmiClientInterceptor.afterPropertiesSet(RmiClientInterceptor.java:135)
at org.springframework.remoting.rmi.RmiProxyFactoryBean.afterPropertiesSet(RmiProxyFactoryBean.java:68)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1317)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1076)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:872)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.rmi.client.MainController.callRMISever(MainController.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:409)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:774)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.ha.tcp.ReplicationValve.invoke(ReplicationValve.java:333)
at org.apache.catalina.ha.session.JvmRouteBinderValve.invoke(JvmRouteBinderValve.java:219)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:279)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

现在我不确定这有什么问题我已经尝试了谷歌但没有取得任何成功现在我有两个问题基于此

  1. 如何删除上述异常,我也尝试过使用LAN中的不同机器
  2. 我们从基于Web的应用程序触发桌面应用程序的最佳方式,以便它开始执行。完整的问题陈述是我们有在基于Web的应用程序上运行的编译器以及在本地机器上运行的编译代码
  3. 谢谢,

    我有解决方案,如果有人遇到任何与Spring Web应用程序和使用RMI邮件的非弹簧桌面应用程序通信相关的问题

    感谢您的社区支持

1 个答案:

答案 0 :(得分:1)

发现有一些情况,Spring服务器和非spring客户端在rmi上产生问题。但是当您使用非Spring服务器和Spring客户端(Web Server到桌面应用程序)时,这是可能的。在我的情况下,使用

时出现问题

这是我的客户

public void getRMIServer(){
    Registry registry = null;
    Hello stub;
    try {
        System.out.println("IN RMI CLients");
        registry = LocateRegistry.getRegistry("localhost");
        stub = (Hello) registry.lookup("yash");
        System.out.println(stub.greeting());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这是我的服务器

public static void main(String[] args){
    try {
        System.out.println("RMI Server Started");
        HelloImpl hello = new HelloImpl();
        UnicastRemoteObject.unexportObject(hello, true);
        Hello stub = (Hello) UnicastRemoteObject.exportObject(hello, 0);
        Registry reg = LocateRegistry.createRegistry(1098);
        reg.rebind("yash", stub);
        System.out.println("server.RMI Server is ready.");
    } catch (Exception e) {
        e.printStackTrace();
    }

}

我们还需要驻留在服务器和客户端上的共享资源,这只是通用接口

package shared;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Map;
  public interface Start extends Remote{
  public void startRMI(Map<Object,Object> m) throws RemoteException;
  public void stopRMI() throws RemoteException;
}

共享资源是扩展远程接口

的接口

如果有人需要工作解决方案,请告诉我