JAX-RS Resource Lifecycle Performance Impact

后端 未结 1 1827
予麋鹿
予麋鹿 2021-02-10 02:25

I know by default JAX-RS endpoints lifecycle is once-per-request, so that the request specific informations can be injected into the instance.

And we c

相关标签:
1条回答
  • 2021-02-10 03:02

    To start with your last question: I'm always using the default (per-request) and I seldom came to a point where I wanted to change this.

    What might be a reason to prefer one over the other?

    • If you want to serve some static content (maybe a welcome-document of your API) it makes sense to produce this content only once and hold it in a singleton resource class. But you can achieve the same by e.g. injecting an @ApplicationScoped CDI bean in a per-request scoped resource class.
    • If you prefer injecting the @xxxParam values like @QueryParam as fields instead of method parameters you should use the per-request lifecycle. This is not supported for singletons. (This does not include injecting via @Context).

    I made a little test to compare the performance of both. You can find the sources and the results on github. In short: I measured a difference from about 1.5 %. I don't think this should affect your application too much. Comparing the results of the JVisualVM monitoring I would tend to say that the per-request test is using more memory but you should decide on your own if this really affects your application.

    0 讨论(0)
提交回复
热议问题