Question

I am trying to create my first very simple web app. I am using maven, jetty, restful. I have read some topics about it, but they often go to difficult for complete begginer to fast. And I haven't figured out how to get rid of these two problems as simple as possible.

  1. Later on I try to make the app more complex. But I want to test now, how can I get the time the "request" was submitted to server. And also I want to get the time that it took to get the answer to client. I want to display the info in json format. Is there a simple way to do it? How can I do it?

    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    
    @Path("sample")
    public class HelloWorldService {
    
        @Path("/helloWorld/{first}/{second}/{third}")
        @GET
        @Produces(MediaType.APPLICATION_JSON)
        public HelloWorld helloWorld(@PathParam("first") String first, @PathParam("second") String  second, @PathParam("third") String third) {
            return new HelloWorld(first, second, third);
        }
    }
    

And also:

public class HelloWorld {

    private String first;
    private String second;
    private String third;

public HelloWorld(String first, String second, String third) {
    this.first = first;
    this.second = second;
    this.third= third;
}

public HelloWorld(String first, String second) {
    this.first = first;
    this.second = second;
}

public HelloWorld(){
}

public HelloWorld(String first) {
    this.first= first;
}

    public String getFirst() {
        return first;
    }

    public String getSecond(){
        return second;
    }

    public String getThird(){
        return third;
    }
}

Thank you! :)

Was it helpful?

Solution

Depending on what you need, you could simply measure the time needed to process the request:

class ResponseWrapper<T> {
    public final Long generationTime;
    public final T response;

    public ResponseWrapper(final Long generationTime, final T response) {
        this.generationTime = generationTime;
        this.response = response;
    }
}

@...
public ResponseWrapper<HelloWorld> helloWorld(...) {
    long startTime = System.currentTimeMillis();

    // process the request

    long elapsedTime = System.currentTimeMillis() - startTime;

    return new ResponseWrapper<HelloWorld>(elapsedTime , new HelloWorld(...));
}

Or, if you need some more universal solution, use filters from Servlet API:

public class GenerationTimeFilter implements Filter {
    ...

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {
       long startTime = System.currentTimeMillis();

       chain.doFilter(req, res);

       long elapsedTime = System.currentTimeMillis() - startTime;
    }
}

To see how to modify a ServletResponse inside filter, see this question: Looking for an example for inserting content into the response using a servlet filter


Just keep in mind, that this type of measurement will not give the final user the time he/she had to wait to get the response. Your application server will need a few miliseconds to handle raw HTTP request/response processing, not to mention network latency.

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