Page 1 of 1
About Question enthuware.ocejws.v6.2.201 :
Posted: Sun Jun 29, 2014 7:37 pm
by nickeasyuptech
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);
} }
Re: About Question enthuware.ocejws.v6.2.201 :
Posted: Mon Jun 30, 2014 12:48 am
by fjwalraven
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
Re: About Question enthuware.ocejws.v6.2.201 :
Posted: Mon Apr 24, 2017 6:48 am
by tioola
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.
Re: About Question enthuware.ocejws.v6.2.201 :
Posted: Mon Apr 24, 2017 2:13 pm
by fjwalraven
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
Re: About Question enthuware.ocejws.v6.2.201 :
Posted: Sun Apr 30, 2017 2:59 am
by fjwalraven
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