简体   繁体   中英

Using RMI with JSF and Tomcat

I want to write a simple application using RMI. On the server side I have an interface foo and a class fooImpl. My interface is:

import java.rmi.Remote;

public interface Foo extends Remote {
   public String exec(String test) throws Exception;
}

My implementation of the the above interface is:

import java.rmi.server.UnicastRemoteObject;

public class FooImpl extends UnicastRemoteObject implements Foo {

    public FooImpl() throws Exception {
        java.rmi.registry.LocateRegistry.createRegistry(1099);
        java.rmi.Naming.rebind("myFoo",this);
    }

    public String exec(String test) {
        return "This is a Test";
    }

    public static void main(String[] args) {
        try {
            FooImpl fooImpl = new FooImpl();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

On the client side, I have web application using PrimeFace which is runnig on Tomcat. There is a IndexBean on client side:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

@ManagedBean
@RequestScoped
public class IndexBean {

    public void execute() {
        try {
            Foo foo = (Foo) java.rmi.Naming.lookup("//localhost/myFoo");
            System.out.println(foo.exec());
        } catch (NotBoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

}

when I execute the code, I get the following error at the client side:

java.rmi.UnmarshalException: error unmarshalling return; nested exception is: java.lang.ClassNotFoundException: Foo (no security manager: RMI class loader disabled) at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source) at java.rmi.Naming.lookup(Naming.java:101) at bean.IndexBean.execute(IndexBean.java:15) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.apache.el.parser.AstValue.invoke(AstValue.java:245) at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:277) at org.apache.myfaces.view.facelets.el.ContextAwareTagMethodExpression.invoke(ContextAwareTagMethodExpression.java:96) at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:88) at javax.faces.event.ActionEvent.processListener(ActionEvent.java:51) at javax.faces.component.UIC omponentBase.broadcast(UIComponentBase.java:409) at javax.faces.component.UICommand.broadcast(UICommand.java:103) at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:1013) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:284) at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1302) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:745) at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:38) at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:170) at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:197) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081) at org.apache.coyote.AbstractPro tocol$AbstractConnectionHandler.process(AbstractProtocol.java:658) at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:744) Caused by: java.lang.ClassNotFoundException: Foo (no security manager: RMI class loader disabled) at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:553) at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:646) at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:311) at sun.rmi.server.MarshalInputStream.resolv eProxyClass(MarshalInputStream.java:255) at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1558) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1514) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370) ... 44 more

Also, I changed the port which Tomcat uses to do not have conflict with RMI port.

The client doesn't have the remote interface on its CLASSPATH, or, probably, the stub either. To avoid using a stub, add

super(0);

as the first line of the constructor.

NB you must store the result of createRegistry() in a static variable, to prevent the Registry from exiting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM