Jakarta EE Cookbook
上QQ阅读APP看书,第一时间看更新

How it works...

Well, there's a lot happening here! First, we should have a look at the @Produces annotation. This is a Jakarta CDI annotation that says to the server, Hey! This method knows how to construct a User object.

Since we didn't create a default constructor for the User class, the getUser method from our factory will be injected into our context as one.

The second annotation is our custom @Profile annotation, which has our ProfileType enumeration as a parameter. It is the qualifier for our UserProfile objects.

Now, let's have a look at these declarations:

@Profile(ProfileType.ADMIN)
public class ImplAdmin implements UserProfile{
...
}

@Profile(ProfileType.OPERATOR)
@Default
public class ImplOperator implements UserProfile{
...
}

This code will teach Jakarta CDI how to inject a UserProfile object:

  • If the object is annotated as @Profile(ProfileType.ADMIN), use ImplAdmin.
  • If the object is annotated as @Profile(ProfileType.OPERATOR), use ImplOperator.
  • If the object is not annotated, use ImplOperator, as it has the @Default annotation.

We can see these in action in our endpoint declaration:

 @Inject
@Profile(ProfileType.ADMIN)
private UserProfile userProfileAdmin;

@Inject
@Profile(ProfileType.OPERATOR)
private UserProfile userProfileOperator;

@Inject
private UserProfile userProfileDefault;

So, Jakarta CDI is helping us use the context to inject the right implementation of the UserProfile interface.

By taking a look at the endpoint methods, we will see the following:

 @GET
@Path("getUser")
public Response getUser(@Context HttpServletRequest request,
@Context HttpServletResponse response)
throws ServletException, IOException{

request.setAttribute("result", user);
request.getRequestDispatcher("/result.jsp")
.forward(request, response);
return Response.ok().build();
}

Note that we included HttpServletRequest and HttpServletResponse as parameters for our method, but annotated them as @Context. So, even though this is not a servlet context (when we have easy access to request and response references), we can ask Jakarta CDI to give us a proper reference to them.

Finally, we have our user event engine, as follows:

 @Inject
private Event<User> userEvent;

...

private ProfileType fireUserEvents(ProfileType type){
userEvent.fire(user);
userEvent.fireAsync(user);
return type;
}

public void sendUserNotification(@Observes User user){
System.out.println("sendUserNotification: " + user);
}

public void sendUserNotificationAsync(@ObservesAsync User user){
System.out.println("sendUserNotificationAsync: " + user);
}

So, we are using the @Observes and @ObserversAsync annotations to say to Jakarta CDI, Hey CDI! Watch over the User object... when somebody fires an event over it, I want you to do something.

And for something, Jakarta CDI understands this as calling the sendUserNotification and sendUserNotificationAsync methods. Try it for yourself!

Obviously, @Observers will be executed synchronously, while @ObservesAsync will be executed asynchronously.