About Question enthuware.ocejws.v6.2.201 :

Moderators: Site Manager, fjwalraven

Post Reply
nickeasyuptech
Posts: 29
Joined: Sat Jun 08, 2013 11:33 pm
Contact:

About Question enthuware.ocejws.v6.2.201 :

Post by nickeasyuptech » Sun Jun 29, 2014 7:37 pm

Is the first correct answer for this question correct? I don't see a Singleton or Stateless annotation on the root resource.


Root resources and Sub resource classes are allowed to be a Singleton or a Stateless session bean -

Code: Select all

@Path("rs") public class AdditionService extends Application {    

@Path("/addN/")       
public Sub getSub(){       
return new Sub();    
} } 
@Singleton public class Sub {        
@GET    @Path("{num1}/{num2}")   
 public String getAddition(@PathParam("num1") int num, @PathParam("num2") int num2){       return "" + (num+num2);  
  } }

fjwalraven
Posts: 423
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: About Question enthuware.ocejws.v6.2.201 :

Post by fjwalraven » Mon Jun 30, 2014 12:48 am

Both root resource and sub-resource can be a Singleton or a Stateless Session Bean.

However, there is no requirement that they both need to be an EJB. A sub-resource can be a Singleton or a Stateless Session Bean with a root resource being a POJO and vice-versa.

Regards,
Frits

tioola
Posts: 6
Joined: Mon Sep 23, 2013 5:19 pm
Contact:

Re: About Question enthuware.ocejws.v6.2.201 :

Post by tioola » Mon Apr 24, 2017 6:48 am

The EJB's are being created using the new Operator, doesn't it mean that they a are out of the application server context and therefore are not being managed as EJB's? As far as I know EJB's must be managed by the app server.

fjwalraven
Posts: 423
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: About Question enthuware.ocejws.v6.2.201 :

Post by fjwalraven » Mon Apr 24, 2017 2:13 pm

Hi!
The EJB's are being created using the new Operator, doesn't it mean that they a are out of the application server context and therefore are not being managed as EJB's? As far as I know EJB's must be managed by the app server.
This indeed looks a bit like instantiating a Pojo however we are using a feature of the JAX-RS integration with EJB's. We don't need to know all the details but it is important to know that sub-resource locators (e.g. getSub()) enable reuse of resource classes. The sub-resource class can be marked as an EJB (making it scalable) just like the root-resource class.

From the Java EE6 tutorial:
In general, for JAX-RS to work with enterprise beans, you need to annotate the class of a bean with @Path to convert it to a root resource class. You can use the @Path annotation with stateless session beans and singleton POJO beans....... Session beans can also be used for subresources.
http://docs.oracle.com/javaee/6/tutorial/doc/gkncy.html

Regards,
Frits

fjwalraven
Posts: 423
Joined: Tue Jul 24, 2012 2:43 am
Contact:

Re: About Question enthuware.ocejws.v6.2.201 :

Post by fjwalraven » Sun Apr 30, 2017 2:59 am

The EJB's are being created using the new Operator, doesn't it mean that they a are out of the application server context and therefore are not being managed as EJB's? As far as I know EJB's must be managed by the app server.
I just did some debugging and found out that JAX-RS is not instantiating an EJB but just a normal java Sub object. So, basically you are right: nice find!

Note however that the EJB-lookup procedure is not part of the WSD exam. You only need to know that a subresource can also be an EJB.

This will be the updated code:

Code: Select all

@Path("rs")
@Stateless
public class AdditionService extends Application {
   @EJB (name="ejb/Sub") 
   private Sub sub;
   @Resource 
   private SessionContext context;
    
   @Path("/addN/")
   public Sub getSub(){
      return (Sub) context.lookup("ejb/Sub");
   }
}
@Singleton
public class Sub {	
   @GET
   @Path("{num1}/{num2}")
   public String getAddition(@PathParam("num1") int num, @PathParam("num2") int num2){
      return "" + (num+num2);
   }
}
Thank you for your feedback!

Regards,
Frits

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests