JAX-RS Resource Lifecycle Performance Impact

扶醉桌前 提交于 2019-12-03 08:52:10

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!