EJB abstract remote bean - java

I am developing Java6 web-app (.war) deployed onto glassfish-3 server. Part of my logic, concerned with DB connections etc. is part of another project - .ear deployed along with my app. It all worked well until instead of a simple managed beans I had to create another layer of abstraction to injected logic. Maybe the code will explain it best.
My web-app:
public class OtherClass {
#EJB(name = "IClass", mappedName = "A")
private IClass iClass;
}
}
.ear deployed app:
public abstract class AbClass {...}
#Stateless(name = "IClass", mappedName = "A")
public class A extends AbClass implements IClass {...}
#Stateless(name = "IClass", mappedName = "B")
public class B extends AbClass implements IClass {...}
Common imports (another project as a maven dependency):
#Remote
public interface IClass extends Serializable {...}
However that does not work and while trying to resolve #EJB throws:
Caused by: javax.naming.NameNotFoundException: A#.com.example.IClass not found
...
Caused by: java.lang.IllegalStateException: Exception attempting to inject Remote ejb-ref name=IClass,Remote 3.x interface =com.example.IClass,ejb-link=null,lookup=,mappedName=A,jndi-name=,refType=Session into class com.example.ItherClass: Lookup failed for 'java:comp/env/IClass' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}
I am aware I could work around that problem for example by creating a Factory, but I was under the impression that that approach should work.

Related

Getting javax.Naming.NameNotFoundException when calling WebMethod in JEE 5

I'm using JEE version 5.0.2. I'm calling a WebMethod in a #WebService annotated class. This annotated class calls a Stateless #EJB that contains the business logic (the implementation) of the WebMethod I'm calling.
My WebService class is defined as follows:
#WebService
HandlerChain(file = "MyXService_handle.xml")
public class MyXService {
#EJB(mappedName="MyXBean", beanName="MyXBean")
public MyXBean myXBean;
#WebMethod
public String sayHello(#WebParam(name="nTimes") int nTimes) {
return myXBean.sayHello(nTimes);
}
}
My EJB is defined as follows:
#Stateless(name="MyXBean", mappedName="MyXBean")
#TransactionManagement(TransactionManagementType.BEAN)
public class MyXBean {
#PersistenceContext name="persistence/my_PU", unitName="my_PU")
protected EntityManager entityManager;
public String sayHello(int nTimes) {
String s = "";
for (int i = 0; i < nTimes; i++) {
s += "Hello";
}
return s;
}
}
I'm getting the following error:
Caused by: com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref com.my.domain.MyXService/myXBean#jndi: MyXBean#null#com.my.domain.MyXBean#Session#MyXBean into class com.sun.enterprise.webservice.JAXWSServlet
at com.sun.enterprise.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:387)
at com.sun.enterprise.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:206)
at com.sun.enterprise.util.InjectionManagerImpl.injectInstance(InjectionManagerImpl.java:117)
at com.sun.web.server.J2EEInstanceListener.handleBeforeEvent(J2EEInstanceListener.java:259)
... 31 more
Caused by: javax.naming.NameNotFoundException: MyXBean#com.my.domain.MyXBean not found
at com.sun.enterprise.naming.TransientContext.doLookup(TransientContext.java:216)
at com.sun.enterprise.naming.TransientContext.lookup(TransientContext.java:188)
at com.sun.enterprise.naming.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:74)
at com.sun.enterprise.naming.LocalSerialContextProviderImpl.lookup(LocalSerialContextProviderImpl.java:111)
at com.sun.enterprise.naming.SerialContext.lookup(SerialContext.java:409)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at com.sun.enterprise.naming.NamingManagerImpl.lookup(NamingManagerImpl.java:944)
at com.sun.enterprise.naming.java.javaURLContext.lookup(javaURLContext.java:173)
at com.sun.enterprise.naming.SerialContext.lookup(SerialContext.java:407)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at com.sun.enterprise.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:287)
... 34 more
|#]
The WebService class and the EJB are exactly in the same module in the same WAR. This WAR is being deployed in a Glassfish Server version 2.1.1. I only get this warning in the deploy log (server.log):
[#|2018-12-10T22:51:59.045+0100|WARNING|sun-appserver2.1|javax.enterprise.system.tools.deployment|_ThreadID=78;_ThreadName=Thread-946;_RequestID=bf2b9b34-5240-4eca-8b4b-9d16baad7c5b;|Unresolved <ejb-link>: MyXBean|#]
I'm following the Java EE 5 specification to implement all of this, so I'm trying to do my architecture "by the book".
I already tried to implement an interface as explained in here https://docs.oracle.com/javaee/5/tutorial/doc/bnbmg.html but with no avail, the error persisted.
I have no xml configuration concerning the beans. I was expecting all to work via the annotations but I'm getting the javax.Naming.NameNotFoundException explained above.
Am I missing something? I'm available to give more details if needed.
Thank you.
You are using the ejb name and the mapped name. The usage of mapped name is vendor specific. Glassfish is using this as a "global JNDI name". Since you are using both, I don't know which one will take precedence over the other. With your example, empty #EJB and #Stateless anntoations should work.

Correct Configuration for JSF EJB application using remote interface

I'm creating a java class library and EJB module as a user authentication application which can be used by stand alone clients by calling the remote interface EJB and its implementation class (session bean). I'm Using Netbeans 8 and Glassfish 4.0. After succesfully building the app I get the following stack when trying to run it:
SEVERE: Exception during lifecycle processing
java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.RuntimeException: java.lang.NoClassDefFoundError: Lcom/manaar/security/services/remote/UserServiceRemote;
at com.sun.enterprise.web.WebApplication.start(WebApplication.java:168)
at org.glassfish.internal.data.EngineRef.start(EngineRef.java:122)
at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:291)
at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:352)
...
SEVERE: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.RuntimeException: java.lang.NoClassDefFoundError: Lcom/manaar/security/services/remote/UserServiceRemote;
The steps I'm following are:
1) Create the Java class library and populate it with the remote interface and entity class
2) Create the EJB module and populate this with the implementation class (session bean) and dao methods
3) Add both projects above to the library of the client JSF web application
4) Call the remote interface as an EJB in the client app and use the methods to show a user profile
The main classes are:
1) Java class library
#Remote
public interface UserServiceRemote {
public List<Users> findAllUsers();
public Users findByName(String userName);
public void createUser(Users newUser);
public void updateUser(Users updatedUsers);
public void deleteUser(Users userToDelete);
public void adminUpdateUser(Users aUpdatedUser);
#Entity
#Table(name = "SHA_USERS")
public class Users {
#Id
private String userName;
private String password;
2) EJB Module
#Stateless
public class UserServiceImpl implements UserServiceRemote {
#EJB
private UsersDao dao;
#Override
public Users findByName(String userName) {
return dao.findByName(userName);
}
3) Java class library and EJB module are both added as Projects in the client application
4) The managed bean in the client app:
#ManagedBean
#SessionScoped
public class SecClientFull {
#EJB
private UserServiceRemote useServ;
private Users loggedUser;
private String userName;
public String showProfile() {
loggedUser = useServ.findByName(userName);
return "/Users/AppUserProfile";
}
public String getUserName() {
userName = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
return userName;
}
...
}
Looking in this forum and elsewhere online I've seen several posts talking about bug in GF3. I'm using the newest version so I'm hoping this is not a bug and I have simply configured this wrong.
Also I'm not clear if I still need to add any configurations for the EJBs in the glassfish-web.xml (or sun-web.xml) configuration file. I've assumed that the Java EE annotations are sufficient.
Finally I should say I coded this manually. I'm not sure if using the Netbeans wizards to create the libraries and session beans is a better way to make sure the configurations are correct.
Would really appreciate any advice or feedback and pls let me know if I need to provide further information. Thanks in advance!
The solution to the above is twofold:
1) Make sure that both Java class library and EJB module are created with separate 'lib' directories if they are to used by stand alone applications (which they are in my case). It's easy to overlook that point if you're creating EJBs for use in the same app (as many tutorials are)
2) The entity class (Users) must implement the java.io.Serialization interface and so should the managed bean on the client side
When first re-creating the Java class library and EJB module I got a ClassCastException that Users cannot be cast to Serialization. As the Java EE tutorial says the implementation of Serialization is for security reasons when EJB modules are used by stand alone applications and transferred accross networks.
So I added the interface to the entity class and the managed bean on the client side. The exception was eliminated and the app now runs perfectly. Thanks again!
Did you create default no args constructor for your class. Because I don't see it in your code.

Weblogic12c deployment EJB ambiguous error

I'm trying to deploy the application in Weblogic 12c. During deployment, I'm getting the below error
weblogic.management.DeploymentException: weblogic.application.naming.ReferenceResolutionException: [J2EE:160092]Error: The ejb-link "BeanClass" declared in the ejb-ref or ejb-local-ref "...BeanClassService/beanClass" in the module "....EJB.jar" is ambiguous. Qualify this ejb-link to remove the ambiguity.
Code:
IBeanClass.java
#Local
public interface IBeanClass {}
BeanClass.java
#Stateless(name = "BeanClass")
#PermitAll
public class BeanClass implements IBeanClass { ...... }
IBeanClassService.java
#Local
public interface IBeanClassService { ......... }
BeanClassService.java
#Stateless(name = "BeanClassService")
public class BeanClassService implements IBeanClassService {
#EJB(beanName = "BeanClass")
private IBeanClassService beanclass;
... }
I'm making a call to the EJB from the web application project:
ClientClass.java
public class ClientClass{ ....
#EJB(beanName = "BeanClassService")
private IBeanClassService beanclass;
..... }
The code was working fine in weblogic10 but now in weblogic 12 exception occurs. Please help in resolving this issue.
BeanClass is not an IBeanClassService, so even by basic java convention, you cannot assign a IBeanClass object to that variable there, let alone inject an EJB into that spot. What you should have is
#EJB(beanName = "BeanClass")
private IBeanClass beanclass;
i have the same problem the solution was:
<dependency>
<groupId>bo.sigep.modulo</groupId>
<artifactId>moduloSigep-ejb</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
in the war pom maven
the key is provided
regards

Unsatisfied dependencies for type [...] with qualifiers [#Default] at injection point (using #Stateful EJB with CDI)

I have the following code to manage two kinds of repositories. Both repository classes inherit an interface to allow reinitialization of their resources.
public interface CachingRepository
{
public void invalidateCache();
}
Global, application-scoped repo:
#Named("globalRepo")
#ApplicationScoped
public class GlobalRepository implements CachingRepository
{
private List<Category> categories;
...
#Override
public void invalidateCache()
{
categories = null;
}
...
}
Per user, session-scoped repo:
#Named("userRepo")
#SessionScoped
//#Stateful // <- NOTE HERE
public class UserRepository implements CachingRepository, Serializable
{
private List<MyFile> files;
#Override
public void invalidateCache()
{
files = null;
}
...
}
When injecting this (without #Stateful) into the context
#Named
#ViewScoped
public class MyHandler implements Serializable
{
#Inject
private UserRepository userRepo;
...
}
it works. However, when adding #Stateful to the UserRepository class, deployment fails with an exception saying:
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [UserRepository] with qualifiers [#Default] at injection point [[field] #Inject private de.company.project.pack.MyHandler.userRepo]
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:275)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:244)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:107)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:127)
at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:346)
at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:331)
at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:366)
at org.jboss.as.weld.WeldContainer.start(WeldContainer.java:83)
at org.jboss.as.weld.services.WeldService.start(WeldService.java:76)
... 5 more
Adding the name of the CDI bean like
#Inject #Named("userRepo")
private UserRepository userRepo;
results in the same exception. The only thing that works in conjunction with #Stateful is to use the interface in the var declaration:
#Inject #Named("userRepo")
private CachingRepository userRepo;
I might need sub class functionality here however, so using CachingRepository isn't really desired (at the moment).
Q's:
Why isn't this working as expected? The UserRepository var should already identify which class to instantiate, shouldn't it? What's the logic to this?
Why does the #Stateful EJB annotation have such severe effects here? Why does it essentially force me into using the CachingRepository interface in the var declaration?
Note, I' using Seam 3 Faces making the #ViewScoped become a CDI view-scoped bean, so the problem at hand is likely still CDI-only.
I had the same problem with this misleading exception...
By adding #Stateful to UserRepository you expose EJB methods of the CachingRepository interface without having a no-interface view declared. Add #LocalBean to UserRepository to activate the no-interface view. See EJB 3.1 Specification, Section 4.9.8 "Session Bean's No-Interface View"
The bean class must designate that it exposes a no-interface view via its bean class definition or in the deployment descriptor. The following rules apply:
...
If the bean exposes at least one other client view, the bean
designates that it exposes a no-interface view by means of the
#LocalBean annotation on the bean class or in the deployment
descriptor.
...
I also refer to this stackoverflow answer for more information about no-interface views.
I had the same error.
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408:
Unsatisfied dependencies for type UserTransaction with qualifiers
#Default at injection point [BackedAnnotatedField] #Inject...
I solved this problem like this:
I used UserTransaction this way when I received an error.
#Inject
UserTransaction trans;
Instead of #Inject, I used #Resource annotation.
I had the same problem.
And this is what I did to resolve it:
I had to produce the EJB I am trying to inject as below with a wildfly container:
#ApplicationScoped
public class Resources {
private static final String DISCOUNT_SERVICE_ENDPOINT_PROPERTY =
"services.discount.endpoint";
private MyServiceImpl myService;
}
#Produces
public MyServiceImpl produceMyServiceImpl() {
if (myService == null) {
String endpoint = System.getProperty(DISCOUNT_SERVICE_ENDPOINT_PROPERTY);
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(MyServiceImpl.class);
factory.setServiceName(MyService.SERVICE);
factory.setAddress(endpoint);
myService = (MyServiceImpl) factory.create();
}
return myService;
}
The below is a config which goes under your standalone-full.xml file.
<property name="services.discount.endpoint" value="http://localhost:8080/my_service/MyService/MyServiceImpl"/>
I had the same problem. I hope this can help someone.
To solve the problem:
Right click on projcet
Click on Properties
find Project facets
On Project facets, active the option CDI
Apply and save.
Now all is fine.
Maybe you should add non-parameterized Constructor on the bean class.
I read on Jboss Documentation that says: concrete Java class that has a constructor with no parameters (or a constructor designated with the annotation #Inject) is a bean.

How should be EJB Stateless Session Bean correctly injected into the web module?

Being completely new to Java EE (but not to Java itself) I'm trying to build a very simple "Enterprise Application" with Hibernate as JPA provider and JSF as the actual UI framework. For this purposes I'm using the NetBeans 7 with GlassFish 3.1.
{ApplicationName}-ejb:
I've accomplished to generate entity classes from database and local sesssion beans for these entities. Beans.xml is in place.
#Stateless
public class QuestFacade extends AbstractFacade<Quest> implements QuestFacadeLocal {
// some methods here as well as EntityManager injection ...
}
{ApplicationName}-war:
I've created a simple POJO as a backing bean for the JSF page. I've annotated it with javax.inject.#Named and javax.enterprise.context.#SessionScoped. This backing bean is now accessible from the JSF page as well as being injected when the actual page is accessed. Beans.xml is in place as well.
#Named
#SessionScoped
public class QuestBean implements Serializable {
#EJB
protected QuestFacade questFacade;
// several methods delegating lookups to the questFacade ...
}
Having this deployed and page accessed, I'm, however, getting an error from GlassFish that the QuestFacade cannot be looked up by the JNDI.
The stacktrace is quite long but the initial cause could be enough:
Caused by: javax.naming.NamingException: Lookup failed for 'model.session.QuestFacade#model.session.QuestFacade' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: model.session.QuestFacade#model.session.QuestFacade not found]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at com.sun.ejb.EjbNamingReferenceManagerImpl.resolveEjbReference(EjbNamingReferenceManagerImpl.java:173)
... 74 more
Caused by: javax.naming.NameNotFoundException: model.session.QuestFacade#model.session.QuestFacade not found
at com.sun.enterprise.naming.impl.TransientContext.doLookup(TransientContext.java:248)
at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:215)
at com.sun.enterprise.naming.impl.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:77)
at com.sun.enterprise.naming.impl.LocalSerialContextProviderImpl.lookup(LocalSerialContextProviderImpl.java:119)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:505)
... 78 more
I understand that I'm persuading GlassFish to inject an EJB from a different module within the same application. Should the #Remote interface be used instead? I've also tried to explicitely specify the name for both #Stateless and #EJB annotation but without any success.
I believe that I'm doing something fundamentaly wrong, but I cannot find out what.
Any suggestion or would be greatly appreciated!
I believe that I'm doing something fundamentaly wrong, but I cannot find out what.
What you're doing wrong is that if you implement a business interface (either #Local or #Remote), then you must declare the variable where injection takes place as having the type of that interface, not of the actual bean class.
So in your case:
#Named
#SessionScoped
public class QuestBean implements Serializable {
#EJB
protected QuestFacadeLocal questFacade;
// several methods delegating lookups to the questFacade ...
}
However, a business interface is not required in EJB when you're doing local (in-jvm) communication. As you discovered, if you don't specify a business interface at all for your EJB, you can inject the bean class itself. This is because you then automatically get the so-called no-interface view.
If you want, you can optionally declare that you want BOTH the local view and the no-interface view. In that way, you can inject your bean class in places whether either the bean type itself is declared or its business interface. For this you use the #LocalBean.
#Stateless
#LocalBean
public class QuestFacade extends AbstractFacade<Quest> implements QuestFacadeLocal {
// some methods here as well as EntityManager injection ...
}
Injection can thus happen in two ways now:
#Named
#SessionScoped
public class QuestBean implements Serializable {
#EJB
protected QuestFacadeLocal questFacade; // possible because of local view
#EJB
protected QuestFacade questFacadeN; // possible because of no-interface view
// several methods delegating lookups to the questFacade ...
}
In practice I didn't found much use for having both methods available at the same time though, but maybe this adds to your understanding.
Apparently the problem was that I generated #Local session beans. Per this tutorial it is no longer necessary (?) to specify the #Local or #Remote interface. I still not completely understand the problem though.
I Hope this answer could potentialy save up some time to somebody :-)
Jarda

Categories

Resources