JBoss 5 AS and EJB3 bean injection from servlets? - jboss

It was my understanding that JBossAS 5.x supported EJB3 bean injection in servlets with the #EJB3 annotation. I'm using the default configuration for JBossAS 5.0.1.GA and it's not working. I've added the mappedName argument to the #EJB annotation with the session beans JNDI name, and it just doesn't do anything. No apparent errors, the bean is just never injected.
The session beans are in the ejb-jar, the servlets in a war, everything is packaged in an ear. Manual JNDI lookups work just fine. Any ideas?

This may happen if your web.xml points to an older version of the spec. Ideally, it should be something like
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">
This link may help a little too.

You should also check the jar file needed for EJB3 deployment in the classpath.JAr file is jboss-ejb3-ext-api.jar.

Related

Disable scanning of CDI beans in WAR

I have WAR package with CDI beans. Deployment of the package is very slow because every time during deployment the package is scanned for CDI beans. Is there any option to disable this process?
The correct way is to disable discovery in the beans.xml of the relevant archive:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="none">
</beans>
According to the CDI specification this removes the archive from the list of bean-archives.
I have a feeling that what you're looking for is more of a tool. As mentioned, Weld uses class scanning to find annotations. There are ways to speed this up. One that works pretty well is Jandex, an annotation processor that can be used at compile time to create an index (easier to read database) of your classes and annotations. This does dramatically boost deployment times.

Why the OSGI element cannot be recognized by the STS?

The xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">
<bean id="demoBean"
class="org.spring.demo.DemoBean">
</bean>
<osgi:service ref="demoBean" interface="org.spring.demo.DemoInterface"></osgi:service>
</beans>
The SpringExplorer lists the "demoBean" node, but doesn't list the element of "osgi:service".
Other spring eclipse plugin should be installed?
Actually, this looks a little bit like a bug that I recently fixed in STS:
https://jira.spring.io/browse/INT-3674
So it may be worth trying updating your STS from nightly update site.
It could also be
another bug in STS causing it to miss beans that actually exist
a problem with the bean definition or schema causing the bean not to be properly defined.
You can try to distinguish between these two cases by making sure the beans you think should exist actually do exist at runtime when the application context is instantiated.
If the bean in fact exist and doesn't show up in STS model, then I'd consider that a bug in STS and you should consider filing a bug report. Otherwise it is another problem.

JEE6 war packing into several modules

I want to split parts of my WAR application into different JAR components.
Every JAR component contains JPA-Entities, EJBs & JSF-Composite-Components.
Example: extracting the user management (XHTMLs, EJBs, JPA-Entities) into an own jar.
At first everything looks working fine as long as I don't have to use an entityManager.
Problem:
In EJBs of JAR-files the entityManager will never be injected.
I'm using the #PersistenceContext annotation for injection.
I have a beans.xml in all META-INF folders and everything (excepted entityManager) is injected correctly. It doesn't even matter if I place a persistence.xml in all JAR-files or only in the WAR-file.
Does anybody have a glue how this can be done?
Where do I have to place the different configuration files (beans.xml, persistence.xml)?
Do I need an ejb-jar.xml file?
Is this possible at all?
Technology-Stack:
JBoss 6.1 / PrimeFaces 3.2 / Maven / EJB 3.1 / Hibernate / JTA / CDI
You need a persistence.xml file in every jar that contains entities.
Every such persistence xml should define a different persistence unit.
If you want one persistence unit to refer to entities define in a different persistence unit then you need to include it as jar-file entry in the persistence unit definition (see example below).
This works for us for a very similar stack JBoss 5.1 (and then 7)/ EJB3/ Hibernate, Maven, ...
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="PersistenceUnitA">
<jar-file>JarContainingOtherPersistenceUnit</jar-file>
<jta-data-source>java:/jboss-mysql-ds</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
</properties>
</persistence-unit>

how to set specific ejb jndi names in jboss 7.1.1

my web application contains a few 2.x stateful and stateless session ejbs. but unlike previous jboss versions (as well as other major app servers), i am unable to specify an ejb jndi name in jboss 7.1.1. i have followed their docs by eliminating jboss.xml in favor of jboss-ejb3.xml, as well as starting the app server in full profile mode.
<jboss:ejb-jar xmlns:jboss="http://www.jboss.com/xml/ns/javaee"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-ejb3-2_0.xsd
http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"
version="3.1"
impl-version="2.0">
<enterprise-beans>
<session>
<ejb-name>MyEJB</ejb-name>
<jndi-name>MyEJB_JNDI_NAME</jndi-name>
</session>
</enterprise-beans>
</jboss:ejb-jar>
however, the jndi-name in jboss-ejb3.xml is not taking hold, preventing me from setting my own custom ejb jndi name. does anyone know how to do this right?
<jndi-name> is no longer supported in JBoss AS 7. From this JIRA:
In AS7 we no longer support binding to custom JNDI names for EJBs. So
the beans are always bound to the spec mandated java:global, java:app
and java:module namespaces. The <jndi-name> for the session bean
element is no longer supported.

Java Web Application deployment on JBoss 7.1

I intend to develop and deploy a simple web application using
EJB 3
JSP and servlet
JBoss 7
JPA
I have coded the application and created a standard ear with following structure
app.ear
|_ lib (with all required jars such as the commons logging)
|_ META-INF
|_ application.xml
|_ jboss-app.xml
|_ app_ejb.jar (contains a stateless EJB, an entity, persistence.xml etc...)
|_ app_web.war (jsps, servlet, web.xml etc..)
jboss-app.xml :- (library is the app name)
<!DOCTYPE jboss-app PUBLIC "-//JBoss//DTD J2EE Application 1.4//EN" "http://www.jboss.org/j2ee/dtd/jboss-app_4_0.dtd">
<jboss-app>
<loader-repository>library:app=ejb3</loader-repository>
</jboss-app>
application.xml :-
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd" version="1.4">
<display-name>library</display-name>
<module>
<ejb>ejb_library.jar</ejb>
</module>
<module>
<web>
<web-uri>web_library.war</web-uri>
<context-root>web_library</context-root>
</web>
</module>
</application>
Now when I deploy this ear with the admin console of JBoss 7, the web module seems to have been deployed, even the pages are hit. However, the further part of the application where I try to inject the EJB in the servlet (as a instance varialbe using #EJB), fails.
There seems to be some problem in the way I am deploying the application.
I tried to find a tutorial where I could deploy the app in JBoss but with all that I am stuck till this.
Could anyone guide me with this? I am using JBoss for the first time and am not completely aware of JBoss specific configurations.
EJB that you are injecting, is it implementing an interface? and is that interface registered with the server? #Local or #Remote.
If not, then Try introducing a #Local interface and then your EJB implementation implementing that interface; and then, use that local interface to inject the EJB implementation.
Also, check the threads below;
JBoss 6: Injecting EJB into servlet
Inject an EJB into JAX-RS (RESTful service)
http://docs.jboss.org/ejb3/docs/tutorial/1.0.0/html/Injecting_EJB_in_Servlets.html
A silly thought, but can you re-arrange the modules declarations in application.xml; bring web modules first and ejb following it.
module -> web module
module -> ejb module
Moreover, this is also related to your situation:
http://docs.jboss.org/jbossas/docs/Server_Configuration_Guide/4/html/ch01s04s02.html