0

I am writing a small embedded Jetty web server, but I have a shared object that has an expensive construction and I don't want each single request to the servlet to have to rebuild this object. Unfortunately the way I add a resource in Jetty is through the ResourceConfig constructor, but this does not allow me to do anything beyond adding the class name:

 // instantiate this expensive object
 MyExpensiveSharedObjectClass myExpensiveSharedObject = new MyExpensiveSharedObjectClass();
 String result = myExpensiveSharedObject.search("alpha");

 // set up the service
 final ResourceConfig resourceConfig = new ResourceConfig(MyService.class);

Then MyService.java contains your standard declarations such as:

    @GET
    @Path("/doSomething")
    @Produces(MediaType.APPLICATION_JSON)
    public String doSomething() {
        // do stuff with myExpensiveSharedObject....except how do I get to it??
        // String result = myExpensiveSharedObject.search("alpha");

What I want ideally is a way that each time a request comes into /doSomething, I can perform methods on the myExpensiveSharedObject object that I've created earlier.

I imagine this is quite simple but I can't find a simple method of accomplishing this.

Alternatively - is there a way to have some form of shared memory space within the Jetty servlet? I noticed that every single request seems to instantiate a new instance of the servlet, so I can't, for example, build up a shared memory map within the object that can be reused by all instances of the class. I am sure this is possible but I can't figure out how to do it.

Basically, I'm just trying to find a way to have an object that is expensive to construct, something that would be ideal within a constructor method or passed into the servlet itself, but within the context of these servlets, constructors are called every single request, so I can't go that route.

Joe
  • 607
  • 6
  • 13
futureboy
  • 83
  • 2
  • 10

1 Answers1

1

I think you're running into jersey issues, not jetty ones (but you may also be running into issues with both!). See if Dependency injection with Jersey 2.0 helps you understand how to do inject dependencies into your resource classes. If so, then you should be able to do

bind(myExpensiveSharedObject).to(MyExpensiveSharedObjectClass.class)

in an AbstractBinder implementation, which you can then register with jersey's ResourceConfig by doing

resourceConfig.register(binder)
Joe
  • 607
  • 6
  • 13
  • Ha yeah I'm totally lost... that example points to lots of XML configuration (which I don't have). Do you know of any simple full examples for embedded jetty servers? – futureboy May 14 '19 at 20:15
  • The XML is, I believe, just for setting up a ResourceConfig via servlet config mechanisms. Since you're doing so programmatically you should be able to ignore that part, create an `AbstractBinder` subclass that binds your expensive object, and register it with the `resourceConfig`. The best full example for jetty/jersey is probably the dropwizard source, but I wouldn't say it's simple. – Joe May 15 '19 at 03:56