How will the Rest Service be executed when a user accesses a resource from a resource class?

When multiple users request the same resource method from a service class, how will requests be processed on the server?

How will the leisure service be performed for each request? What is the difference between the life cycle of a leisure service and a servlet?

For example, if the resource is given below, how it will be created and executed in the following scenarios:

case 1: two users simultaneously call two different methods

case 2: two users simultaneously use the same method

@Path("greet")
public class GreetingResource {

    @GET
    @Path("welcome/{username}")
    @Produces(MediaType.TEXT_PLAIN)
    public String sayWelcome(@PathParam("username") String User) {
        return "Welcome!" + User;
    }

    @GET
    @Path("hello/{username}")
    @Produces(MediaType.TEXT_PLAIN)
    public String sayHello(@PathParam("username") String User) {
        return "Hello " + User;
    }
}
+4
source share
2 answers

Servlet Classes

. . JSR 369, Servlet 4.0:

, ( ), . [...]

Servlet , :

  • , init.
  • service .
  • , destroy, .

.

, JAX-RS. , JAX-RS .

(, @Path), . , JAX-RS . JSR 370, JAX-RS 2.1:

. , , , , , . [...]

documentation, . JVM , HTTP- HTTP-.

, .

EJB CDI

, EJB, @Stateless @Singleton.

JAX-RS CDI, JAX-RS CDI . JAX-RS , . CDI beans @RequestScoped @ApplicationScoped JAX-RS.

. Java EE 8 .

(, @Provider), . , JAX-RS . JSR 370:

JAX-RS. , , (), , , . [...]

+2

, JAX-RS:

(, ). scope . , . , . .

JAX-RS:

, , root , URI .

, .

Path’s are matched with Resource classes.
Constructor is called.
Dependencies are injected.
Appropriate method is called.
Resource is garbage collected.

, JAX-RS . . JVM HTTP- HTTP.

, singleton JAX-RS: JAX-RS

, 1 2 , 2 GreetingResource . 2, , 2 , .

+6

Source: https://habr.com/ru/post/1694811/


All Articles