Accessing Local interface (EJB3) within 2 different application on same JBOSS instance (same JVM) - jboss7.x

Can I access Local interface from another application within same JBOSS instance?
Example:
In my shared library:
#Local
class EmployeeLocal {
void doAction();}
#Remote
class EmployeeRemote{
void doAction();
}
EAR Application:
#Stateless
class EmployeeService implements EmployeRemote,EmployeeLocal {
#Override
public doAction() { System.out.println(“Do something!”)};
}
}
WAR Application:
public AccessEJB {
#EJB
IEmployeeLocal employeLocal;
//Injection doesn't work either, throws exception saying not EJB "Caused //by:org.jboss.as.server.deployment.DeploymentUnitProcessingException: //JBAS014544: No EJB found with interface of type 'IEmployeeLocal' for binding
public void doSomethingWithEJB() {
IEmployeeRemote iEmployee =
(IEmployeeRemote ) context.lookup("ejb:" + appName + "/" + moduleName + "/EmployeeService!EmployeRemote");
iEmployee.doAction(); //THIS WORKS
IEmployeeLocal iEmployeeLocal =
(IEmployeeLocal) context.lookup("ejb:" + appName + "/" + moduleName + "/EmployeeService!EmployeLocal");
iEmployee.doAction(); //THIS DOESN'T WORK
}
}
If this is possible, then please provide a example (with jboss configuration required). I have tried dependency injection, all sorts of jndi lookups

Related

CDI in a servlet produces NullPointerException

I am trying to use CDI in a servlet, here is a snippet of my servlet code
public class MyServlet extends HTTPServlet{
#Inject
#Any
Instance<MyProcedure> procedures;
public void handleRequest(ServletRequest req, ServletResponse res) throws Exception {
if (procedures == null) {
System.out.println("procedure list is NULL");
}
for (Object o : procedures) {
System.out.println("calling procedure " + o.toString());
}
}
}
MyProcedure is am interface and the appliation WAR contains a couple of classes that implement MyProcedure.
I always get a NullPointerException because the list of procedures is null...I do have a beans.xml file in my WEB-INF directory.
I found out what the problem was. The interface that was implemented by my beans had to be included in the war archive under WEB-INF/lib. After I did that everything started to work as expected.

Resteasy and Google Guice: how to use multiple #ApplicationPath and resource with #Injection?

I created a project to test the dependency injection offered by Google Guice in my Jax-rs resources, using Resteasy.
My intentions are:
Use multiple #ApplicationPath for the versions of my API. In each class annotated with #ApplicationPath I load a set of classes for the specific version.
Each resource have a #Inject (from Google Guice) in his constructor to inject some services.
I created two classes annotated with #ApplicationPath: ApplicationV1RS and ApplicationV2RS. In both I added the same resources classes (UserResource and HelloResource), only for my test.
My Module is configured like this:
public class HelloModule implements Module
{
public void configure(final Binder binder)
{
binder.bind(IGreeterService.class).to(GreeterService.class);
binder.bind(IUserService.class).to(UserService.class);
}
}
When I call http://localhost:9095/v1/hello/world or http://localhost:9095/v2/hello/world, I receive the same error:
java.lang.RuntimeException: RESTEASY003190: Could not find constructor
for class: org.jboss.resteasy.examples.guice.hello.HelloResource
Well, as I expected, this not works. The Google Guice is not "smart" to instantiate the resource classes using the construtor for me.
But I can't find a way to work. To be really honest, I'm really confuse about how the Google Guice, Jetty and Resteasy play with each other in this scenario.
If I abandon the idea of use #ApplicationPath, my resources work with Google Guice configuring my HelloModule like this:
public class HelloModule implements Module
{
public void configure(final Binder binder)
{
binder.bind(HelloResource.class);
binder.bind(IGreeterService.class).to(GreeterService.class);
binder.bind(UserResource.class);
binder.bind(IUserService.class).to(UserService.class);
}
}
But in this case, I'm passing the control to register my resources (HelloResource and UserResource) to Guice. It's not flexible for me, I can't setup my multiple #ApplicationPath.
So, what I'm missing or not understanding?
I created a project with the problemetic code. Is very easy to setup and test: https://github.com/dherik/resteasy-guice-hello/tree/so-question/README.md
Thanks!
When you have getClasses method in your Application then it tries to create instance for all the registered resources using the default constructor which is missing in our Resources class. One way is to create a default constructor and Inject the dependencies through setter Injection.
And then instead of overriding getClasses in ApplicationV1RS and ApplicationV2RS you override getSingletons. Since Resources can be Singleton.
Below are the changes that I made to make it work the way you want.
ApplicationV1RS.java
#ApplicationPath("v1")
public class ApplicationV1RS extends Application {
private Set<Object> singletons = new HashSet<Object>();
public ApplicationV1RS(#Context ServletContext servletContext) {
}
#Override
public Set<Object> getSingletons() {
Injector injector = Guice.createInjector(new HelloModule());
HelloResource helloResource = injector.getInstance(HelloResource.class);
UserResource userResource = injector.getInstance(UserResource.class);
singletons.add(helloResource);
singletons.add(userResource);
return singletons;
}
}
ApplicationV2RS.java
#ApplicationPath("v2")
public class ApplicationV2RS extends Application {
private Set<Object> singletons = new HashSet<Object>();
public ApplicationV2RS(#Context ServletContext servletContext) {
}
#Override
public Set<Object> getSingletons() {
Injector injector = Guice.createInjector(new HelloModule());
HelloResource helloResource = injector.getInstance(HelloResource.class);
UserResource userResource = injector.getInstance(UserResource.class);
singletons.add(helloResource);
singletons.add(userResource);
return singletons;
}
}
HelloResource.java
#Path("hello")
public class HelloResource {
#Inject
private IGreeterService greeter;
public HelloResource() {
}
#GET
#Path("{name}")
public String hello(#PathParam("name") final String name) {
return greeter.greet(name);
}
}
UserResource.java
#Path("user")
public class UserResource {
#Inject
private IUserService userService;
public UserResource() {
}
#GET
#Path("{name}")
public String hello(#PathParam("name") final String name) {
return userService.getUser(name);
}
}
Add #Singleton to your Service Classes.
Hope it helps.
I have also pushed the code to forked repo. check it out

Using Jersey Test Framework to target APIs in other files

I have read the documentation concerning the Jersey Test framework and have successfully used JerseyTest's target method to reach a #Path annotated endpoint within my own file. Simplified code is below.
public class TestApplication extends ResourceConfig {
public TestApplication() {
registerClasses(TestService.class);
}
}
#Override
protected Application configure() {
return new TestApplication();
}
#Path("create")
public static class TestService {
#POST
#Path("testObj")
#Consumes(APPLICATION_JSON)
public static Response createTestObj(final TestObj testObj) {
return Response.ok("testObj created").build();
}
}
#Test
private void ensureObjectCreated() {
JSONObject myObj = createNewObj();
final Response response = target("create/testObj").request(APPLICATION_JSON)
.post(Entity.json(myObj.toString()));
Assert.isEqual(response.status, 200);
}
Now I want to reach a #Path annotated endpoint in other files/directories. How do I do so? The problem may be that the other files are actual production code, so I cannot make the classes static. However the endpoints in the other paths are reachable.
Just register them in the resource config, either individually (depending on the scope of the test), or specify a package to scan with the packages method.
#Override
public ResourceConfig configure() {
return new ResourceConfig()
.register(SomeResource.class)
.packages("your.resource.package.to.scan");
}
The only reason the class in the example is static is because it is an inner class that needs to be instantiated by the framework.
When you access the resource, it will not include the root application path, only the #Path value on the class, and whatever sub path, just like in your code above.

Failed to create EJB in REST service

I have a problem with injecting EJB inside of a REST service (using jersey on glassfish 3.2 server) and I'm puzzled.
I have an EJB interface declared as:
import javax.ejb.Local;
#Local
public interface TestServiceLocal {
public String getText();
}
and the class bean that implements it:
import javax.ejb.Local;
import javax.ejb.Stateless;
/**
* Session Bean implementation class TestService
*/
#Stateless
#Local(TestServiceLocal.class)
public class TestService implements Serializable, TestServiceLocal {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public TestService() {
// TODO Auto-generated constructor stub
}
#Override
public String getText() {
return this.getClass().getName();
}
}
The REST service looks like:
#Path("/service")
#Stateless
public class TestRestService {
#EJB(beanName="TestService")
private TestServiceLocal testService;
public TestRestService () {
}
#GET
#Produces(MediaType.TEXT_PLAIN)
#Path("/events")
public String getText() {
return testService.getText();
}
}
The problem is that when the REST service is called the bean cannot be created:
SEVERE: EJB5070: Exception creating stateless session bean : [TestRestService]
WARNING: EJB5184:A system exception occurred during an invocation on EJB TestRestService, method: public java.lang.String TestRestService.getText()
WARNING: javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:454)
at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2547)
at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1899)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
at com.sun.proxy.$Proxy839.getText(Unknown Source)
I had already took a look at the answers posted here but none of them seemed to work for me. Any help will be appreciated. Thank you!
PS: I forgot to mentioned (don't know if it's relevant). My project is created under eclipse Juno as Dynamic Web Project.

EJB 3.1 : Singleton bean not getting injected inside another stateless bean though both beans are getting registered

Here is my bean that is trying to inject a singleton bean InformationService :
#Path("/information/{name}")
#Stateless (name="InformationResource")
public class InformationResource {
#EJB
private InformationService appService;
#GET
#Produces(MediaType.APPLICATION_XML)
public Information getInfo(#PathParam("name") String name){
return appService.getMap().get(name);
}
#PUT
#POST
#Consumes(MediaType.APPLICATION_XML)
public Information putInfo(#PathParam("name") String name, Information info){
return appService.getMap().put(name,info);
}
#DELETE
public void deleteInfo(#PathParam("name") String name){
appService.getMap().remove(name);
}
}
This is the InformationService class
#Singleton
public class InformationService {
private Map<String,Information> map;
#PostConstruct
public void init(){
map = new HashMap<String,Information>();
map.put("daud", new Information("B.Tech","Lucknow"));
map.put("anuragh", new Information("M.Sc","Delhi"));
}
public Map<String,Information> getMap(){
return map;
}
}
Its part of a very simple JAX-RS implementation and I am deploying as war in JBoss 6.1 Final. The problem is that InformationService throwing a NullPointerException when I make the proper get request. If I initialize appService explicitly, everything works fine. Why is #EJB annotation not working ?
Are you using Jersey as REST implementation? If so, EJB injection is not supported out of the box.
This link provides more information on this and also a solution.
Check that your #Singleton is javax.ejb.Singleton.
Any other exceptions before NPE ?