Reusing JAX RS Client in multi-threaded environment (with resteasy)

前端 未结 3 1555
闹比i
闹比i 2020-12-31 02:37

According to the documentation,

\"Clients are heavy-weight objects that manage the client-side communication infrastructure. Initialization as well

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-31 03:29

    Your implementation is not thread-safe. When two threads access someMethod at the same time they are sharing the same Client and one will try to make a second request while the first one is not finished.

    You have two choices:

    • Synchronize the access to the Client and WebTarget manually.
    • Let the container manage concurrency by annotating the enclosing type with @javax.ejb.Singleton which guarantees thread safety. (see chapter 4.8.5 of the EJB specification)

    If someMethod in a container managed environment I would use the second approach.

提交回复
热议问题