CommunicationException while client's ctx.lookup() in GlassFish 3.1 - java

I have one entity class which implements Serializable and I got this error on client:
javax.naming.CommunicationException:
Communication exception for
SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory,
java.naming.factory.url.pkgs=com.sun.enterprise.naming,
java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl}
[Root exception is
java.rmi.MarshalException: CORBA
BAD_PARAM 1398079494 Maybe; nested
exception is:
java.io.NotSerializableException:
----------BEGIN server-side stack trace----------
org.omg.CORBA.BAD_PARAM: WARNING:
IOP00100006: Class
com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate
is not Serializable vmcid: SUN minor
code: 6 completed: Maybe
when I'm doing SomeService serv = (SomeService)ctx.lookup("java:global/MyProject/SomeServiceImpl");
Does Entity class should be in the same package both in server and in client?
Now Entity class is in the package (dir) of Client App and in the package (dir) where SomeService interface is on the server.

I added #Remote annotation to service interface and error disapeared.

I tried the following and everything works Ok.
When creating InitialContext as:
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext ctx = new InitialContext(props);
And later,
myBeanService = (MyBeanService) ctx.lookup("java:global/AppName/MyBeanService");
Shows that exception, but if I just call this way:
myBeanService = (MyBeanService) new InitialContext().lookup("java:global/AppName/MyBeanService");
There is no problem. The problem are the JNDI properties.

Related

Bootstrap Hibernate SessionFactory in Netty/Armeria handler method fails with ClassNotFoundException

I have a Java Application that uses Armeria for a Web Service. When I create my Hibernate SessionFactory in the main method it works fine. But I am trying to create the SessionFactory when a certain Http Endpoint is called. In the handler method the session factory can not be created
Exception in thread "Thread-1" org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 0 and column 0 in RESOURCE hibernate.cfg.xml. Message: null
Caused by: javax.xml.bind.JAXBException
- with linked exception:
[java.lang.ClassNotFoundException: com/sun/xml/bind/v2/ContextFactory]
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:226)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:441)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:122)
... 17 more
Caused by: java.lang.ClassNotFoundException: com/sun/xml/bind/v2/ContextFactory
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at javax.xml.bind.ContextFinder.safeLoadClass(ContextFinder.java:577)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:224)
... 21 more
All I could find about this error is that JaxB is not provided for Java > 8 but i am using Java 8 and it works fine if I just create it at Application launch.
I believe it's some sort of class path conflict. In Java 8, the following code fails with ClassNotFoundException: com/sun/xml/bind/v2/ContextFactory, as reported in the question:
public class MyService {
#Get("/start")
public HttpResponse start() throws Exception {
final StandardServiceRegistryBuilder registryBuilder =
new StandardServiceRegistryBuilder().configure();
...
}
}
However, the problem goes away after upgrading to a newer Java version, such as Java 11.
Fortunately, the problem can be worked around by specifying the context class loader explicitly:
#Get("/start")
public HttpResponse start() throws Exception {
Thread.currentThread().setContextClassLoader(MyService.class.getClassLoader());
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
final StandardServiceRegistryBuilder registryBuilder =
new StandardServiceRegistryBuilder().configure();
...
}

Multiple entityMangagerFactories ( 1PC pattern used) leads to java.lang.IllegalStateException: 'url' not set exception

I have the Spring Maven project using Java 11. Jboss WIldfly 15.0.1 Final is my deployment server.
To import data (objects using Hibernate) from one data source to another data source (MySQL)- I have added one primary entityManagerFactory with JpaTransactionManager and I have 2 other entity manager factories separate perisistenceUnits and a chainedTransactionManager being used(for 1 Phase commit-1PC commnit).
When deploying the war, sometimes, I get the below exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'someDAO': Injection of persistence dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'onetimeConfig': Injection of persistence dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'entityMgrFactory2' defined in class path resource [com/path/ImportConfig.class]: Bean
instantiation via factory method failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to instantiate
[javax.persistence.EntityManagerFactory]: Factory method 'entityMgrFactory2' threw exception;
nested exception is java.lang.IllegalStateException: 'url' not set
someDAO class header is:
#Transactional(rollbackFor = {Exception.class})
#Repository("someDAO")
public class SomeDAOImpl extends ParentDAOImpl implements SomeDAO {
The someDAO class extends the ParentDAOImpl class in which the primary entityManagerFactory is used and is also mentioned using #DependsOn. So, why does on deploy the creation of someDAO is trying to trigger the creation of entityMgrFactory2 when entityManagerFactory is mentioned in?!
#Repository("parentDAOImpl")
#DependsOn({"entityManagerFactory"})
public class ParentDAOImpl implements ParentDAO, Serializable {
#PersistenceContext
private transient EntityManager entityManager;
...
The problem never replicates on my local machine. It does occurs only while deploying on other servers and deployment fails due to this. And also the exception sometimes mentions someDAO and other times a different DAOclass that extends ParentDAOImpl.
Please help me, I am a novice.

Can't get Remote EJB to work with EJB Client API on Wildfly

I'm currently struggling with getting remote EJB invocation to work on wildfly (8.x and 9.x).
In detail it's about remote invocation from a standalone client application (not from another app server) using the EJB Client API approach. The remote naming approach works for me but isn't applicable in my scenario because I need to use client-side interceptors for passing context data to a server-side interceptor for the remote invocations.
But for now I try to get remote invocations with the client API to work for a simple example. Therefore I tried the quickstart for remote ejb invocation which is available on github (wildfly/quickstart/ejb-remote). The point is that this quickstart raises the same error as my on simple sample app. Here are some details of my application:
My remote interface:
package test.ejb;
public interface HelloRemote {
String greet(String name);
}
My Bean implementation:
package test.ejb;
import javax.ejb.Remote;
import javax.ejb.Stateless;
#Stateless(name = "Hello")
#Remote(HelloRemote.class)
public class HelloBean implements HelloRemote {
public String greet(String name) {
return "Hello " + name;
}
}
The remote view of the bean is correctly registered at the server (in export namespace):
java:jboss/exported/ejb-test-backend.jar/Hello!de.coryx.HelloRemote
Here now the client side:
My Main class:
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.jboss.ejb.client.EJBClientContext;
import test.ejb.HelloRemote;
public class Main {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
Context context = new InitialContext(props);
String jndiLookup = "ejb:/ejb-test-backend.jar/Hello!" + HelloRemote.class.getName();
HelloRemote hello = (HelloRemote) context.lookup(jndiLookup);
System.out.println(hello.greet("World"));
}
}
The jboss-ejb-client.properties (packaged in jar/META_INF):
endpoint.name=client-endpoint
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=localhost
remote.connection.default.port=8080
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
When I execute the main Method I get the following error message (same thing occurs when trying the wildfly quickstart that I mentioned above):
Exception in thread "main" java.lang.IllegalStateException: EJBCLIENT000025: No EJB receiver available for handling [appName:, moduleName:ejb-test-backend.jar, distinctName:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext#497470ed
at org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:774)
at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:116)
at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:186)
at org.jboss.ejb.client.EJBInvocationHandler.sendRequestWithPossibleRetries(EJBInvocationHandler.java:255)
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:200)
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:183)
at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:146)
at com.sun.proxy.$Proxy0.greet(Unknown Source)
at Main.main(Main.java:16)
When I use the remote naming approach everything is fine:
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
props.put(Context.PROVIDER_URL, "http-remoting://127.0.0.1:8080");
props.put("jboss.naming.client.ejb.context", true);
props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
props.put("jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT", "false");
Context context = new InitialContext(props);
String jndiLookup = "/ejb-test-backend.jar/Hello!" + HelloRemote.class.getName();
HelloRemote hello = (HelloRemote) context.lookup(jndiLookup);
System.out.println(hello.greet("World"));
Output here is (as expected):
Hello World
So is there anyone who knows what could be wrong here or better, who has a working example for remote EJB invocation on Wildfly using the EJB client API?
Thanks in advance!
As so often, I stumbled over the solution shortly after writing down the question and talking about it.
The problem with this setup was that the jboss-ejb-client.properties file has not been loaded by the client API which was then missing the connection url, ...
I don't know whether there where changes to the required location where these properties have to be placed or whether I was too dumb to read it correctly or whether I just adapted a tutorial that was corrupted. It doesn't even matter ;)
The solution is to place the properties file toplevel on the classpath and not in the META-INF directory of the JAR!
It works now as expected and I can register client-side interceptors.

Jboss AS 7.1.1 Final Local JNDI EJB Invocation

Using Jboss AS 7.1.1 Final, I'm trying to invoke a Local EJB(3.1) through JNDI.
My Local EJB is:
#Stateless(mappedName = "Services")
#LocalBean
public class Services implements ServicesLocal {
.....// scary stuffs here
}
My Interface Services is:
#Local
public interface ServicesLocal {
.... // Some powerfull stuffs here
}
I'm trying to invoke this EJB above like this:
private ServicesLocal getLocalEJB() throws NamingException {
log.info("\n\n\n\n\n\n\n\n ################## Getting the ServicesLocal");
InitialContext context = new InitialContext();
return (ServicesLocal) context.lookup("ejb:/global/docs/docs-ejb-0.1/Services!com.mycompany.docs.local.ServicesLocal");
}
This is the error I'm getting when invoking this code above:
############ Getting the ServicesLocal
15:36:10,437 INFO [org.jboss.ejb.client] (http-localhost-127.0.0.1-8080-1) JBoss EJB Client version 1.0.5.Final
15:36:10,456
ERROR [stderr] (http-localhost-127.0.0.1-8080-1) java.lang.IllegalStateException: No EJB receiver available for handling [appName:,modulename:global,distinctname:docs] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext#396f6a24
15:36:10,457
ERROR [stderr] (http-localhost-127.0.0.1-8080-1) ^Iat org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:584)
And this is the JNDI log when the Jboss 7.1.1 Final boot. Having the EJB that I want to invoke:
15:07:07,975 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-7) JNDI bindings for session bean named Services in deployment unit subdeployment "docs-ejb-0.1.jar" of deployment "docs.ear" are as follows:
java:global/docs/docs-ejb-0.1/Services!com.mycompany.docs.local.ServicesLocal
java:app/docs-ejb-0.1/Services!com.mycompany.docs.local.ServicesLocal
java:module/Services!com.mycompany.docs.local.ServicesLocal
java:global/docs/docs-ejb-0.1/Services!com.mycompany.docs.services.Services
java:app/docs-ejb-0.1/Services!com.mycompany.docs.services.Services
java:module/Services!com.mycompany.docs.services.Services
I think my context.lookup("ejb:/global/docs/docs-ejb-0.1/Services!com.mycompany.docs.local.ServicesLocal") is wrong. What I can do to fix that and invoke my Local EJB with JNDI?
Have you tried to use what JBoss tells you?
context.lookup("java:global/docs/docs-ejb-0.1/Services!com.mycompany.docs.local.ServicesLocal")
Your EJB is stored in java namespace, not in ejb one.

EJB Timer Service error

I'm trying to use the EJB timer service, I have a class named TimerBean which carries the methods to schedule a timer and handle the timeout, this implements TimerBeanRemote an interface class.
In another session bean I have the following:
TimerBeanRemote service = (TimerBeanRemote) new InitialContext().lookup("TimerBean/remote");
When I try to run it on the server I get the error:
javax.naming.NamingException: Lookup failed for 'TimerBean/remote' in SerialContext [Root exception is javax.naming.NameNotFoundException: TimerBean]
Any ideas as to why it can't find it? Thanks!
Following from your comments -if you are trying to access TimerBeanRemote within the same container then you can inject the #Remote ejb in your servlet or JSF Backing Bean else you can locate your EJB through a JNDI lookup.
Suppose your TimerBean is: com.mypackage.timer.TimerBeanRemote
then as per explanation above you can either inject or lookup:
Injection
public class MyServlet ...{
#EJB
com.mypackage.timer.TimerBeanRemote timerBean;
}
JNDI lookup:
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
String[] serverDetails = server.split(":");
props.setProperty("org.omg.CORBA.ORBInitialHost", MyHost);
props.setProperty("org.omg.CORBA.ORBInitialPort", MyPort);
InitialContext ic = new InitialContext(props);<br>
TimerBeanRemote timerBean = (TimerBeanRemote)ic.lookup("com.mypackage.timer.TimerBeanRemote");
You can read the following articles for further details:
http://download.oracle.com/javaee/1.4/tutorial/doc/Session5.html
http://www.javabeat.net/articles/3-ejb-30-timer-services-an-overview-1.html

Categories