Question

I'm try to create a Grizzly web service engine but some elements are missing.

Here is what I would like to do:

  • Create a Grizzly server as an executable jar (this is ok)

  • This server would must be able to look into a folder for web service archives (contained in a jar or a war or other)

  • The server should expose this web services.

What I already know :

  • web services can be built as servlet (It seems that Grizzly is able to run servlets but I did not find anything about how to load an external serlvet contained in a ".war" or other)

  • I tried to use a ClassLoader to load a JAX-RS2 webservice exported in a ".jar" but the @path binding did not work (I might have missed something)

What I would like to know :

  • Which type of archive should I use for the web service archives ?

  • Should the web services be servlets ? In these case, how to dynamicaly load a servlet with Grizzly ?

  • Am I going in the wrong direction ?

Here is what I have done with the classloader

File file  = new File("c:\\Users\\User\\Desktop\\myresource.jar");
 URL url = file.toURI().toURL();  
 URL[] urls = new URL[]{url};
 ClassLoader cl = new URLClassLoader(urls);

URLClassLoader child = new URLClassLoader (urls, this.getClass().getClassLoader());

Class<?> classToLoad = Class.forName ("Test.ExternalWS.MyResource", true, child);
Method method = classToLoad.getDeclaredMethod ("getIt");
Object instance = classToLoad.newInstance ();
Object result = method.invoke (instance);
this.WSInstance = classToLoad.newInstance();
  • Here is the webservice

    @Path("myresource") public class MyResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Got it!";
    }
    

    }

Nothing happens when I try to reach localhost:8080/....../myresource.

I tried all possible urls and put a breakpoint... never reached.

Was it helpful?

Solution

Not sure which Jersey version you're using. In Jersey 2 I made it work this way:

    File file = new File("/path/resource1.jar");
    URL url = file.toURI().toURL();
    URL[] urls = new URL[]{url};
    ClassLoader cl = new URLClassLoader(urls, Main.class.getClassLoader());

    ResourceConfig rc = new ResourceConfig()
            .setClassLoader(cl)
            .files("/path/resource1.jar");
    HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(
            BASE_URI, rc);

May be there is more elegant way...

OTHER TIPS

Are you using Grizzly 1.x or 2.x branch ?

I develop Grizzly Deployer in Grizzly 1.x. You should check the source code if it does what you need

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top