Search code examples
javaweblogicjerseyjax-rs

How do I build a single access web service?


I have a Jax-RS Jersey web service on Weblogic. It runs fine but returns a large amount of data. The problem is if I have more that 4 calls for the service at the same time I use up all the JVM memory on the server, then everything stops working and I have to reboot.

How can I limit the service to only run 2 or 3 instances and have other calls wait?


Solution

  • How are you returning your data? Do you create the entire object to return in memory? If so then you may want to look into streaming the response instead.

    Can you give us an idea as to what your service is doing?

    Edit:
    You can stream stuff (like data from a ResultSet) like this;

    @GET
    public MyResultStream getData()
    {
      ResultSet rs = queryDatabase();
      return new MyResultStream(rs);
    }
    

    You'll need to provide MyResultStream yourself;

    public class MyResultStream implements javax.ws.rs.core.StreamingOutput
    {
      private ResultSet rs;
    
      public MyResultStream (ResultSet rs)
      {
        this.rs = rs;
      }
    
      public void write(OutputStream output)
      {
        //write any document pre-able
        // for example <results>
    
        while (rs.next())
        {
          //get the data from the ResultSet and write it to the output in XML form
          // for example <result><foo>bar</foo></result>
        }
    
        //write any document post-amble
        // for example </results>
      }
    }
    

    Remember you'll have to close your ResultSet somehow.