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