简体   繁体   English

通过反射运行码头网络服务

[英]running jetty web service via reflection

Hello everyone I have been trying jetty as an embedded server for my rest web service and while using it standalone the program works fine but when i try to call the main method of the class that contains the jetty server startup logic using reflection i cant get it to work.Also when i remove the jettyServer.join line from my code then i get error java.lang.NoClassDefFoundError: org/eclipse/jetty/io/ManagedSelector$Accept and if i uncomment it then i get no error but when i try to access my web service i get 404 error. 大家好,我一直在尝试将码头作为嵌入式Web服务用于我的其余Web服务,并且在独立使用它时,程序运行良好,但是当我尝试使用反射调用包含码头服务器启动逻辑的类的主方法时,我无法获得它另外,当我从代码中删除jettyServer.join行时,我得到错误java.lang.NoClassDefFoundError:org / eclipse / jetty / io / ManagedSelector $ Accept ,如果我取消注释,那么我没有错误,但是当我尝试时访问我的网络服务,我得到404错误。

Here is the code for server startup in a class called App 这是名为App的类中用于服务器启动的代码

public static void main(String[] args) {
        // TODO Auto-generated method stub

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
          context.setContextPath("/JettyService/Servlet1");
          StartupListener sup=new StartupListener();
          context.addEventListener(sup);

          Server jettyServer = new Server(8080);
          jettyServer.setHandler(context);

          ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
          jerseyServlet.setInitOrder(0);

          jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", JettyServiceApp.class.getCanonicalName());

          try {
           jettyServer.start();
         // jettyServer.join();
           //stopServer(jettyServer);
          // jettyServer.stop();
          } catch (Exception e) {
           e.printStackTrace();
          } 
          finally{
          // jettyServer.destroy();
          }

    }

And here is the code that calls this method via reflection 这是通过反射调用此方法的代码

try {
            List<URL> urlsR=new ArrayList<URL>();
//contains source classes
            File file = new File("C:\\Users\\DemonKing\\Documents\\eclipseprojects\\JettyDemo\\bin\\");
//contains the depenedencies jars of jetty
            File file2 = new File("F:\\JettyJarsComplete");
            for(File child:file2.listFiles())
            {
                File resFile=new File(file2+"\\"+child.getName());
                System.out.println(resFile);
                URL url2 = resFile.toURI().toURL();
                urlsR.add(url2);
            }

            // convert the file to URL format
            URL url = file.toURI().toURL();
            urlsR.add(url); 
            //URL url2 = file2.toURI().toURL();
            //URL[] urls = new URL[] { url,url2 };
            URL[] urls = urlsR.toArray(new URL[urlsR.size()]);
            //url

            // load this folder into Class loader
            ClassLoader cl = new URLClassLoader(urls);
            Class<?> cls = cl.loadClass("org.demonking.App");
            System.out.println("web class loaders:" + cls.getClassLoader());
            Method m = cls.getDeclaredMethod("main", String[].class);
            m.invoke(null, (Object) null);

            // code added by to avoid the resource leak class loader is
            // never closed warning
            URLClassLoader urlCl = (URLClassLoader) cl;
            urlCl.close();

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

Need Help or pointers as to what am i doing wrong and is this scenario even achievable that is starting up a rest web service via reflection from another code. 需要帮助或有关我在做什么的指针,这种情况甚至可以实现,它是通过另一个代码的反射来启动一个REST Web服务的。

I understand why classnotfond errors occur in java but the classloader is referencing all the jars required and iam not getting error on jetty server startup as i can see it being initailised in the logs this only happens when itry to access my web service at its path 我了解为什么Java中会发生classnotfond错误,但是classloader引用了所有需要的jar,并且我在码头服务器启动时未收到错误,因为我可以看到日志中的错误信息仅在尝试访问其路径上的Web服务时才会发生

Update: got rid of the classnotfound exceptions and all classloader issues but still cant access the web service it always returns 404 error without any exception.Note this only happens when calling the method via reflection otherwise code works fine.Need Help 更新:摆脱了classnotfound异常和所有类加载器问题,但仍然无法访问Web服务,它总是返回404错误而没有任何异常。请注意,只有在通过反射调用方法时才会发生此错误,否则代码可以正常工作。

I finally got it to work. 我终于得到它的工作。 Just added my urlclassloader as thread's current contextclassloader and the code started to work. 刚刚将我的urlclassloader添加为线程的当前contextclassloader,代码开始起作用。 just added before jettyServer.start method 刚在jettyServer.start方法之前添加

Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());

can somenone now explain why this works because i think the thread started from jettyserver.start method did'nt had reference of my urlclassloader? 现在没人能解释为什么这样做了,因为我认为从jettyserver.start方法开始的线程没有引用我的urlclassloader? Am i right? 我对吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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