I have two EJBs , EJB A references EJB B, both are deployed to the same server.
Each time I try to deploy EJB A it tries to register EJB B which is already deployed and registered which causes error:
java.lang.IllegalStateException
Any idea how to prevent EJB A from registering the referenced EJB?
I am using NetBeans and JBoss 4.2.3 as well as EJB 3.0.
If you use maven build
you can use
<scope> provided </scope>
in the pom file of maven of EJB A
Although I cannot imagine why you get an exception if your EJB A is singleton try the annotation #DependsOn on A to specify a dependency on B.
Related
We are trying to migrate from websphere application server to websphere liberty profile.
As part of POC we are deploying a sample war with an EJB inside it. We injected the EJB using #EJB inside a restful web service. While calling the web service liberty is throwing NPE. Then i tried using InitialContext to lookup the EJB and its working fine.
Below are the features i enabled
Jaxrs 1.1
Jdbc 4.0
ejbLite 3.1
We are running liberty on java 1.6.
Below is some code snippet
#EJB(beanName="StudentServiceBean")
private StudentServiceBean service;
Inside web service method
service.getStudent(id); // NPE here
Ejb class
#Stateless(name="StudentServiceBean")
public class StudentServiceBean {}
I even tried using #EJB with lookup and then tried with simple #EJB becUse the EJB is a local EJB and packed inside the war module itself
P.S.
I moved the ejb to an ejb module and packed the application as an ear and tried. Still the ejb is not getting injected into the web service.
Then i created a new ejb and injected the new ejb into the existing ejb. The new ejb is injected properly. I think there is some problem with servlet container and the ejb container.
I tried it on liberty woth jee7 features also but still i am facing the same problem.
If the #EJB annotation cannot be resolved to an EJB, then you would have received a failure when the server created an instance of the class containing the #EJB annotation. Since that does not appear to be happening for you, and instead the instance is created fine, just without that field being set, then the server is not scanning your class for annotations or you have the javax.ejb.EJB annotation class packaged as part of your application.
I would recommend checking the following:
Make sure the javax.ejb.EJB class is not being packaged as part of your annotation
Check that the web.xml for your WAR module has a version > 2.4. WAR modules with a version of 2.4 (or earlier) will not be scanned for annotations.
Check that the web.xml does not contain the setting metadata-complete="true". This setting turns off annotation scanning.
JAX-RS 1.1 is part of EE6. The EE6 platform spec does not list JAX-RS endpoints among the EE components supporting injection (Table EE.5-1). I don't think your usage there is portable.
I've an EAR with an EJB and WAR project in it. I use Netbeans and the Maven building tool. In my EJB there is one EJB, a singleton class: http://pastebin.com/f8jXw0TK
Here is a screenshot of my project structure in Netbeans: https://gyazo.com/09e075972279f637de2d4d98fe9a1ac7
When I deploy the EAR or the EJB-JAR in Glassfish (localhost). I get the follow error message:
Error occurred during deployment: Exception while deploying the app [arp-auth-business-1.0] : Invalid ejb jar [arp-auth-business-1.0]: it contains zero ejb. Note: 1. A valid ejb jar requires at least one session, entity (1.x/2.x style), or message-driven bean. 2. EJB3+ entity beans (#Entity) are POJOs and please package them as library jar. 3. If the jar file contains valid EJBs which are annotated with EJB component level annotations (#Stateless, #Stateful, #MessageDriven, #Singleton), please check server.log to see whether the annotations were processed properly.. Please see server.log for more details.
I've added a simple stateless bean to the EJB JAR, and then it deployed. But then I saw just the simple stateless bean I created, but not my singleton class.
What could be wrong? The server.log shows the same error I get in Glassfish but with a exception stack from the loader. No error from the singleton class or something like that...
Still trying but can't figure it out. Maybe it's maven? But the jar and war file are in the ear file. And in the jar file there is my singleton class.
Kind regards
Are there any differences in capabilities of the EJB when defining / running it from a WAR vs. an EJB container? What are the benefits vs. drawbacks of deciding on one approach vs. other.
What capabilities do we lose when accessing it from a WAR?
In our case, the developers want to use the EJB for creating / accessing REST webservice.
One of our architects has mentioned below. And for this reason he wants to have a separate EJB that would be added ( the jar ) to EAR but also to WAR for using it as REST endpoint. i would prefer not to have it in multiple places
I’d prefer our approach to put transaction/service based code in EJBs to
leverage Container Managed Transactions, JPA, MDB and all the good stuff EJBs
have to offer.
From the documentation I have read on using EJB as a REST service implementation, it says
Add the EJB class to the WEB-INF/classes directory of your WAR file or to a
JAR that is located in your WEB-INF/lib directory. When a client makes a request
to a JAX-RS annotated enterprise bean, the JAX-RS runtime environment looks up
and uses an EJB instance of the class to invoke the resource method.
So, I want to know, if we put the EJB in the WAR - as in creating the source in the WAR's source so that the class will be added to WEB-INF/classes when the WAR is built, instead of having to put the same ejb jar in two different places based on what it is used for - as a REST webservice endpoint vs. other capabilities, will it satisfy all the requirements or I will have to put the jar in two places?
I am using Websphere 8.5 with EJB 3.1, if that makes a difference in the answer.
There are two primary differences highlighted in section 15.4 of the EJB 3.1 specification:
All EJBs in a WAR share the component namespace (java:comp) with the WAR and all other EJBs in the WAR. Normally, each EJB has its own component namespace. This makes it easier to share reference names and bindings (though this can be done explicitly in EE 6 with java:module or java:app), but it increases the chance of conflict in a large WAR.
EJB classes are loaded by the WAR class loader. In practice, this doesn't matter much, it's just something to be aware of if you encounter class loading problems.
If you want to use an EJB as a REST service, you must package the EJB in the WAR. If you're concerned about "duplicating" EJB logic inside the WAR and for an EJB module, you could declare a base class in the EJB module, and then declare subclasses in the WAR and EJB modules that extend the base class and are annotated #Stateless or #Singleton.
Regarding EJB capabilities there is no difference between packaging an EJB in a WAR or in an EJB module.
There are situations where you have to package EJBs in WARs e.g. if you have a REST endpoint which is at the same time an EJB.
Most often WARs encapsulate frontend functionality. In these situations it is just from a design perspective not advisable to put the EJBs into WARs.
My Maven EAR project has two WAR module and one EJB module. There is a FacesConverter class in EJB module and when I try to use it from one of the WAR module it throws an exception. I register this converter with annotation #FacesConverter("org.util.ObjectConverter") use it in a JSF page
javax.servlet.ServletException: Expression Error: Named Object: org.util.ObjectConverter not found.
When ObjectConverter is in WAR module it works fine, but it won't load from the EJB module.
What am I missing here?
Cheers
Why do you put it in EJB module? Front-end (read: JSF) artifacts should go in WAR module. EJB module should only contain business services which are supposed to be reuseable to front-ends other than JSF, such as JSP/Servlet, Struts2, SpringMVC, JAX-RS, etc. The EJB module should absolutely have no single line of javax.faces.* import/dependency in the code.
JSF doesn't look for converters (let alone any other JSF related artifacts such as validators, managed beans and Facelets files) in EJB module, but only in WAR module. Just keep them in the WAR module. Whatever code you think need to share between the WAR and EJB modules should be refactored into a separate Java project which end up as a common JAR file in EAR module (note that this should in turn also contain no JSF-specific artifacts.
I've searched this for a while now, but can't seem to find an answer to this. How can I execute some code in when deploying an EJB3 jar-file to a JBoss server? For example, I need to run some sql migration scripts before the beans are ready to be used.
If you can't use EJB 3.1 (with #Singleton #Startup), I would recommend packaging your EJB module in an EAR with a WAR. Add a ServletContextListener to the WAR, and take your action in the contextInitialized method.
You can create a JBoss MBean service with a Listener that can perform any initialization (SQL scripts run in your case) after JBoss is fully started and before any EJB is used.
I have created such a service and we run it on JBoss 4.2.3.GA, so, you do not need to migrate to JBoss 7.