Ejb jndi binding in java code - binding

The method is loading a bean
Context ctx = new InitialContext();
Object lookupResult = ctx.lookup("abilitecConsumerClient/local");
And another class
#Stateless
#LocalBinding(jndiBinding="abilitecConsumerClient/local" )
#Local()
What exactly does represent abilitecConsumerClient/local? Is that the path in my local machine that I need to look for this property file or the file I need it loads for this EJB JNDI binding name?
If you can also give info how to specify a custom EJB JNDI binding name in JBoss EAP 6?
This app is going to run on JBOSS EAP 6.

According to the old JBoss documentation:
...when the application is deployed in a .jar, session beans will bind to JNDI in the form (...) ejbName/local in the case of local interfaces. When the EJBs are deployed in an .ear file, the default JNDI binding will be prepended by the name of the .ear file.
So, if we have an EAR file which name is foo.ear the default JNDI name would be foo/EJB-NAME/local.
This behavior can be overriden by defining our own #LocalBinding annotation on our bean.
By definition, the #LocalBinding annotation specifies the local JNDI binding for an EJB local interface.
Which means that the value of the element jndiBinding, passed on #LocalBinding, represents the name associated (binded) to the bean, where the annotation was placed, on the Java Naming and Directory Interface (JNDI).
This then allows us to retrieve an instance of the bean, as you mentioned, by looking it up on the JNDI, using the jndiBinding defined on the #LocalBinding annotation:
MyBean bean = ctx.lookup("abilitecConsumerClient/local");
NOW, regarding your last question, notice that in beginning of my answer I said ...old JBoss documentation.... Why? Because since JBoss EAP 6 (or JBoss AS 7), the binding of EJBs to custom JNDI names isn't allowed anymore either by annotations or XML configuration.
One may assume that it was due to the efforts of Java EE 6 to standardize EJB Namespaces.
You can take a closer look at the JNDI changes present on JBoss EAP 6, where EJB 3.1 introduced a standardized global JNDI namespace and a series of related namespaces that map to the various scopes of a Java EE application.
Making a parallel between prior EAP 6 and after, we have:
Prior to EAP 6
// Bean definition
#Stateless
#LocalBinding(jndiBinding="custom/MyBean")
public class MyBeanImpl implements MyBean {
(...)
}
// look-up
ctx = new InitialContext();
MyBean myBean = (MyBean) ctx.lookup("custom/MyBean");
After EAP 6
// Bean definition
#Stateless
#Local(MyBean.class)
public class MyBeanImpl implements MyBean {
(...)
}
// look-up
MyBean myBean = (MyBean)
InitialContext.lookup("java:module/MyBean");
// or through injection
#EJB
MyBean myBean;
Finally, you can find more examples on how to do the EJB look-up on the JBoss AS 7 examples and take a careful look at the following:
Portable JNDI sintax
Application-specified Portable JNDI Names
Section "Examples of JNDI mappings in previous releases and how they might look now" of How do I migrate my application from AS5 or AS6 to AS7

Related

CDI failure with Dependent/applicatio Scope class

I am trying to Inject ServletContext in my dependant scope class but it always gives the failure.
java.lang.IllegalStateException: No CXF message usable for JAX-RS #Context injections in that thread so can't use interface javax.servlet.ServletContext
I am not able to undestand the reason that why can't I inject it here. I am using a producer method and when inside producer method,I try to access ServletContext obj then it gives above exception. I have also checked if the Injected servlet context is null but it is not null. But When I call any method using it for example sc.getContextPath() it gives the above exception. Below is the code snippet:
#Dependent
public class AuthContexthandler {
#Context
ServletContext sc;
#Produces
JWTAuthContextInfo getInfo() {
try{
System.out.println(sc.getContextPath()); //here I get the above mentioned error
//rest of the code
}catch(Exception e){
e.printStackTrace();
}
}
}
you must use #inject instead of #Context.
you can use #Context to inject object instances related to the context of HTTP requests into to JAX-RS source class and as AuthContextHandler(as #Christoph Böhme said) is not a JAX-RS source class so you cannot use #Context
but as http://docs.jboss.org/weld/reference/latest/en-US/html_single/ says:
An object bound to a lifecycle context is called a bean. CDI includes
built-in support for several different kinds of bean, including the
following Java EE component types:
managed beans, and EJB session beans. Both managed beans and EJB
session beans may inject other beans. But some other objects, which
are not themselves beans in the sense used here, may also have beans
injected via CDI. In the Java EE platform, the following kinds of
component may have beans injected:
message-driven beans,
interceptors,
servlets,
servlet filters and
servlet event listeners,
JAX-WS service endpoints and handlers,
JAX-RS resources,
providers and javax.ws.rs.core.Application subclasses, and
JSP tag handlers and tag library event listeners.
it means you also can use #Inject annotation in your JAX-RS source class.
there are also some predefined Beans in CDI such as ServletContext that you can use #inject annotation to inject them.
https://docs.jboss.org/seam/3/servlet/latest/reference/en-US/html/injectablerefs.html
https://docs.oracle.com/javaee/7/tutorial/cdi-adv004.htm

NotSerializableException on ViewScoped ManagedBean with CDI Injected properties

I'm trying to create a scalable JSF application.
I would like to save view states on client side but I have troubles with ViewScoped ManagedBean with CDI Injected attributes.
SomeService.java :
#Singleton
public class SomeService {
// ...
}
SomeBean.java
#ManagedBean
#ViewScoped
public class SomeBean implements Serializable {
#Inject
private SomeService someService;
}
Unfortunately glassfish fails to serialize someService which I don't want to be serializabled but re-injected.
I tried to make it transient which ends up to a NullPointerException when accessing someService after de-serialization.
What shall I do?
I'm aware that I could use CDI ViewScoped with Seam Faces or CODI but I want to minimize at most dependencies.
I could also wait for JEE7 which will provide #ViewScoped for CDI but we won't be using JEE7 before months.
UPDATE :
I just wanted to add that I was using embedded EJB bundled in a jar which is itself linked to my war.
NotSerializableException's stack trace has the following message :
com.company.core.service.__EJB31_Generated__SomeService__Intf____Bean__
I don't like to self respond to my own questions but after some more research I found that it was a bug in Mojarra 2.1.6 (I'm using Glassfish 3.1.2.2) which is now solved in Mojarra 2.1.20.
To update Mojarra you just need to download a fresher version (eg: https://maven.java.net/content/repositories/releases/org/glassfish/javax.faces/2.1.20/javax.faces-2.1.20.jar) and place it in the $GLASSFISH/modules directory as javax.faces.jar.

How to access a local interfaces from one project into another within the same ear project

I have a web project that has FacesValidator, this validator needs to access an EJB service to verify if a record exists. Unfortunately, I cannot inject my enterprise beans since the validator is not a managed-bean, so I'm trying to access it via InitialContext. I've tried different combination from http://docs.oracle.com/javaee/6/tutorial/doc/gipjf.html but failed.
What works is this format:
java:global/myProject-ear-1.0.0/myProject/MyService!com.czetsuya.myProject.service.membership.MyService,
My question is can it be simplify? Seems too long.
Thanks,
czetsuya
Look at the server logs. A bit decent EJB container (at least, Glassfish 3 and JBoss 6/7 do), logs all available JNDI names of the EJB during EJB deployment step. Provided that the validator is properly been put in the WAR and the EJB has a #Local interface, then the shortest JNDI name would be the java:app one which should in your case have been java:app/myProject/MyService.
A completely different alternative is to just make the validator a JSF or CDI managed bean instead, so that you can just use the #EJB annotation.
#ManagedBean // Or #Named.
#ApplicationScoped // Provided that the instance doesn't have any state.
public class MyValidator implements Validator {
#EJB
private MyService myService;
// ...
}
and reference it by binding instead of validatorId:
<f:validator binding="#{myValidator}" />
Note that from JSF 2.2 on, you should be able to inject #EJB in a #FacesValidator (and #FacesConverter).
See also:
How to inject in #FacesValidator with #EJB, #PersistenceContext, #Inject, #Autowired

Inject singleton bean into session bean via remote interface, object always "null"

I need to inject a singleton bean into the session bean. Below are the corresponding classes. The problem is that the injected object is always null. I tried all of the JNDI lookup strings which my JBoss 7.0.1 server showed me during startup (i.e. JNDI bindings for session bean named GlobalBean in deployment unit subdeployment .. of deployment .. are as follows: ..). I also tried commenting out the #EJB annotation in GlobalBean.java and also tried to use the "ejb/GlobalBean" during injection. However, no luck. What could be the reason? Thx.
GlobalBean.java:
#Startup
#Singleton
#Remote(GlobalBeanRemote.class)
#EJB(name="ejb/GlobalBean", beanName="GlobalBean", beanInterface=GlobalBeanRemote.class)
#ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
public class GlobalBean implements GlobalBeanRemote
{
// CODE
}
SessionBean.java:
#Stateful
public class SessionBean extends ParentBean
{
#EJB(name="java:module/GlobalBean!project.framework.interfaces.GlobalBeanRemote")
private GlobalBeanRemote globalBeanAPI3;
// CODE
}
In your SessionBean class try changing name attribute of #EJB to mappedName.
#EJB(mappedName="java:module/GlobalBean!project.framework.interfaces.GlobalBeanRemote")
This will, of course, only work if your two beans are in the same module.
Update
Given that your beans are in separate modules, try using the java:app namespace:
#EJB(mappedName="java:app ...")
The java:app namespace is used to look up local enterprise beans packaged within the same application. That is, the enterprise bean is packaged within an EAR file containing multiple Java EE modules. JNDI addresses using the java:app namespace are of the following form:
java:app[/module name]/enterprise bean name[/interface name]
Also try removing GlobalBean's #EJB annotation. #EJB is used to define a dependency.

JBoss 6: Injecting EJB into servlet

Folks,
I am very annoyed by having to re-learn and waste time with this stuff every time a new version of JBoss rolls around.
I have a stateless EJB that is discovered and declared in the JNDI space:
10:01:53,044 INFO [org.jboss.ejb3.proxy.impl.jndiregistrar.JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
DTalk/UserManager/local - EJB3.x Default Local Business Interface
DTalk/UserManager/local-com.doctalk.ejb.UserManagerLocal - EJB3.x Local Business Interface
I need to use this EJB in a servlet which is part of a war which is part of the EAR that contains the EJB. I'd like to do it using injection.
When I use the most intuitive notation:
#EJB
private UserManager userManager;
I get an exception in JBoss logs.
When I use a more flowery notation such as:
#EJB( mappedName = "UserManager" )
private UserManager userManager;
Or
#EJB( mappedName = "DTalk/UserManager/local" ) // EAR is called DTalk
private UserManager userManager;
I get no injections errors in jboss but the injected bean is null.
This is maddening and a huge waste of time and makes me question why I don't dump the Eclipse/jboss tools franchise in favor of NetBeans and GlsssFish.
Any insights appreciated.
Thanks.
You are trying to inject (a proxy to) the bean instance itself, instead of its interface.
Yet, according to the deployment logging you've shown, you have only declared the bean to be bounded in JNDI via its (local) interface. In order to make the injection happen, you should either declare the variable in which you're injecting as the interface:
#EJB
private UserManagerLocal userManager;
OR declare that a no-interface view should be created for your bean:
#Stateless
#LocalBean
public class UserManager implements UserManagerLocal {
...
}
after which you can declare the variable as you did earlier:
#EJB
private UserManager userManager;

Resources