ClassCastException during JNDI lookup for MongoDB, inside WAB, running Liberty - mongodb

I want to use JNDI lookup for MongoDB, inside WAB/OSGi, running Liberty Profile.
import com.mongodb.DB;
#WebServlet("/MongoServlet")
public class MongoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
DB db = (DB) new InitialContext().lookup("java:comp/env/mongodb");
} catch (NamingException e) {
e.printStackTrace();
}
}
}
The JNDI lookup gives below exception.
[ERROR ] SRVE0777E: Exception thrown by application class 'com.osgi.jndi.web.MongoServlet.doGet:57'
java.lang.ClassCastException: com.mongodb.DBApiLayer incompatible with com.mongodb.DB
com.mongodb.DBApiLayer is subclass of com.mongodb.DB.
I believe that the ClassCastException is because of different classloaders being used by my application, and OSGi bootclassloader. But, I don't know how to troubleshoot it.

Currently there is no way to use the mongodb-2.0 feature with an OSGi application.
Looking up a com.mongodb.DB resource will return you back an instance of DBApiLayer, but that's not the issue (since DBApiLayer extends DB). You would still get a ClassCastException even if you did this:
DBApiLayer db = (DBApiLayer) new InitialContext().lookup("java:comp/env/mongodb");
Would throw:
java.lang.ClassCastException: com.mongodb.DBApiLayer incompatible with com.mongodb.DBApiLayer
The reason for this limitation is that the mongodb-2.0 feature will use a Classloader from Liberty to load com.mongodb classes, and an OSGi application will use a separate Classloader to load com.mongodb classes, no matter how you configure things.
Normal Java EE applications can do things like this:
<library id="MongoLib">
<file name="${server.config.dir}/lib/mongo-java-driver-2.11.4.jar"/>
</library>
<application name="myApp">
<classloader commonLibraryRef="MongoLib"/>
</application>
However, <osgiApplication> elements do not have that capability. Unfortunately, the only solution here is to open an RFE with IBM (or upvote one if an RFE already exists for this).

Related

UnknownServiceException: Unknown service requested [EnversService]

I want to run Hibernate in OSGi. I have added the standard Hibernate OSGi bundle and a Blueprint implementation, so that Envers gets registered right on startup.
Even without any kind of documentation I found out you have to start Envers, because... I doubt there is a logical reason, it does not work otherwise.
However now, even though Envers was registered in Blueprint, I get the following exception:
org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.envers.boot.internal.EnversService]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:184)
at org.hibernate.envers.boot.internal.TypeContributorImpl.contribute(TypeContributorImpl.java:22)
at org.hibernate.boot.internal.MetadataBuilderImpl.applyTypes(MetadataBuilderImpl.java:280)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.populate(EntityManagerFactoryBuilderImpl.java:798)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:187)
at org.hibernate.jpa.boot.spi.Bootstrap.getEntityManagerFactoryBuilder(Bootstrap.java:34)
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilder(HibernatePersistenceProvider.java:165)
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:114)
at org.hibernate.osgi.OsgiPersistenceProvider.createEntityManagerFactory(OsgiPersistenceProvider.java:78)
at org.acme.project.Main.startSession(PersistenceUnitJpaProvider.java:38)
The stack trace starts at PersistenceProvider#createEntityManagerFactory in the following snippet:
public class Main {
private EntityManagerFactory entityManagerFactory;
public void startSession(Map<String, Object> config) {
BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
ServiceReference<PersistenceProvider> serviceReference = context.getServiceReference(PersistenceProvider.class);
PersistenceProvider persistenceProvider = context.getService(serviceReference);
this.entityManagerFactory = persistenceProvider.createEntityManagerFactory("persistenceUnit", config);
context.ungetService(serviceReference);
}
I found this bug, and maybe this issue is fixed in the current version of Hibernate. But since the bundle IDs are broken, I have to use 5.1.
So Envers is registered, but not really. What could be the reason for such a strange error message?

How to configure Infinispan 8.0.1 in Dynamic Web Project using Eclipse IDE

I am new to Infinispan and JBOSS Cache, and am trying to learn these concepts using infinispan documentation. But was not successful in configuring custom xml configuration for cache. Can you please help me out ??
I have following Java Class (Infinispan jar files added to build path)
CustomCacheBean.java
package com.jboss.cache;
import java.io.IOException;
import org.infinispan.Cache;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.eviction.EvictionStrategy;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;
public class CustomCacheBean {
public static void main(String[] args) {
EmbeddedCacheManager manager =
new DefaultCacheManager();
manager.defineConfiguration("custom-cache",new ConfigurationBuilder().build());
Cache<Object, Object> c = manager.getCache("custom-cache");
try {
c = new DefaultCacheManager("infinispan.xml").getCache("xml-configured-cache");
} catch (IOException e) {
e.printStackTrace();
}
}
}
And following is my xml
infinispan.xml (placed under web_Content folder)
<infinispan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:infinispan:config:8.0.1 http://www.infinispan.org/schemas/infinispan-config-8.0.1.xsd"
xmlns="urn:infinispan:config:8.0.1">
<namedCache name="xml-configured-cache">
<eviction strategy="LIRS" maxEntries="10" />
</namedCache>
</infinispan>
When I try to execute CustomCacheBean java class, I am getting following error
Console :
log4j:WARN No appenders could be found for logger (infinispan.org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.infinispan.commons.CacheConfigurationException: ISPN000327: Cannot find a parser for element 'infinispan' in namespace
'urn:infinispan:config:8.0.1'. Check that your configuration is up-to date for this version of Infinispan.
at org.infinispan.configuration.parsing.ParserRegistry.parseElement(ParserRegistry.java:147)
at org.infinispan.configuration.parsing.ParserRegistry.parse(ParserRegistry.java:131)
at org.infinispan.configuration.parsing.ParserRegistry.parse(ParserRegistry.java:118)
at org.infinispan.configuration.parsing.ParserRegistry.parse(ParserRegistry.java:105)
at org.infinispan.manager.DefaultCacheManager.<init>(DefaultCacheManager.java:271)
at org.infinispan.manager.DefaultCacheManager.<init>(DefaultCacheManager.java:244)
at org.infinispan.manager.DefaultCacheManager.<init>(DefaultCacheManager.java:231)
at com.jboss.cache.CustomCacheBean.main(CustomCacheBean.java:19)
I would recommend using Java based configuration instead of XML. You may take a look at tutorials:
Tutorials page
Distributed Cache (which is probably what you will need)
Please note there is a Github button at the bottom of the page (which will navigate you to the Github repository).
You're probably using wrong namespace in the XML config - namespaces don't use micro version, therefore, use:
xsi:schemaLocation="urn:infinispan:config:8.0 http://www.infinispan.org/schemas/infinispan-config-8.0.xsd" xmlns="urn:infinispan:config:8.0"
instead of
xsi:schemaLocation="urn:infinispan:config:8.0.1 http://www.infinispan.org/schemas/infinispan-config-8.0.1.xsd" xmlns="urn:infinispan:config:8.0.1"
Please, make sure that your IDE validates your configuration against the XSD; this can save you a lot of fuss (not only with Infinispan).

IllegalStateException while using UserTransaction (Java EE)

I am trying to make my first Java Enterprise Application and use UserTransactions.
Therefore I use JNDI Lookup with java:comp/UserTransaction to get my UserTransaction Object.
public void myMethod(MyEntity e) throws ApplicationException {
try {
this.ut = getUserTransaction();
this.ut.begin();
this.myStatefulBean.myBusinessMethod(e);
} catch ...
When I start my Transaction with .begin() and try to invoke any method in my stateful Bean (which works properly before starting a UserTransaction) I get an EJBTransactionRolledbackException which leads me to the following error:
java.lang.IllegalStateException: cannot add non-XA Resource to global JTS transaction
The Bean I am using is annotated with #DataSourceDefinition having className = "org.apache.derby.jdbc.ClientXADataSource" thus there is imo no non-XA Resource.
What am I doing wrong?
I am using openjpa 2.2.1, Java EE Version 6
Solved the problem by myself. I used in my DataSourceDefinition name = "java:global/jdbc/testDB" as name, but in my persistence.xml the DataSource name was defined as <jta-data-source>testDB</jta-data-source>
This seems to work in CMTs but not in a UserTransaction. Correcting the entry in persistence.xml to <jta-data-source>java:global/jdbc/testDB</jta-data-source> fixed the problem.

Connecting standalone client to Stateless SessionBean in Glassfish 3

I've followed the instructions here to create a client to a remote SessionBean. I run the client on the same machine that Glassfish 3.1.2 beta is running on. When I use the gf-client.jar from the 3.1.2 beta Glassfish I get the following Exception which is the same Exception if I leave the gf-client.jar out of the classpath:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
If I use a 3.1.1 gf-client.jar from a Maven repository I get a huge stack trace with complaints about it not being able to find some .jar files from Derby which I'm not even using. Apparently a version mismatch problem.
Has anyone gotten a standalone client to connect to Glassfish 3.1.2 beta? Did this change in JavaEE 6?
Here's the code:
#Stateless
public class LockTestDeadlockService implements LockTestDeadlockServiceI {
public int getP1Id() throws SQLException {
int parentId = -1;
return parentId;
}
}
#Remote
public interface LockTestDeadlockServiceI {
public int getP1Id() throws SQLException;
public void insertChildUpdateParent(int parentId) throws SQLException;
}
Here's my client:
public class LoadTestClient {
static Logger logger = Logger.getLogger(LoadTestClient.class);
public static void main(String[] args) {
String jndiName = "java:global/locktest-0.0.1-SNAPSHOT/LockTestDeadlockService";
try {
LockTestDeadlockServiceI lockTestService =
(LockTestDeadlockServiceI) new InitialContext().lookup(jndiName);
logger.info("Got lockTestService Remote Interface");
} catch (NamingException e) {
logger.info("Failed to get lockTestService Remote Interface: " + e);
}
}
}
The short answer is that to connect to GF 3.x from a client, you need a mini-glassfish install via the Application Client Container (ACC) using either webstart or the package-appclient script. Open up the gf-client.jar and look at its classpath in the manifest file. There are a ton of files listed in there. This was similar in GF 2.x, but it seemed to need less dependencies on the client (though it was 15MB with that version).
See these:
Create an "Application Client" with Maven in Java EE
With which maven dependencies can i create a standalone JMS client for Glassfish?
http://docs.oracle.com/cd/E18930_01/html/821-2418/beakt.html#scrolltoc
http://docs.oracle.com/cd/E18930_01/html/821-2418/beakv.html#beakz

NameNotFoundException when calling a EJB in Weblogic 10.3

First of all, I'd like to underline that I've already read other posts in StackOverflow (example) with similar questions, but unfortunately I didn't manage to solve this problem with the answers I saw on those posts. I have no intention to repost a question that has already been answered, so if that's the case, I apologize and I'd be thankful to whom points out where the solution is posted.
Here is my question:
I'm trying to deploy an EJB in WebLogic 10.3.2. The purpose is to use a specific WorkManager to execute work produced in the scope of this component.
With this in mind, I've set up a WorkManager (named ResponseTimeReqClass-0) on my WebLogic configuration, using the web-based interface (Environment > Work Managers > New). Here is a screenshot:
Here is my session bean definition and descriptors:
OrquestratorRemote.java
package orquestrator;
import javax.ejb.Remote;
#Remote
public interface OrquestratorRemote {
public void initOrquestrator();
}
OrquestratorBean.java
package orquestrator;
import javax.ejb.Stateless;
import com.siemens.ecustoms.orchestration.eCustomsOrchestrator;
#Stateless(name = "OrquestratorBean", mappedName = "OrquestratorBean")
public class OrquestratorBean implements OrquestratorRemote {
public void initOrquestrator(){
eCustomsOrchestrator orquestrator = new eCustomsOrchestrator();
orquestrator.run();
}
}
META-INF\ejb-jar.xml
<?xml version='1.0' encoding='UTF-8'?>
<ejb-jar xmlns='http://java.sun.com/xml/ns/javaee'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
metadata-complete='true'>
<enterprise-beans>
<session>
<ejb-name>OrquestradorEJB</ejb-name>
<mapped-name>OrquestratorBean</mapped-name>
<business-remote>orquestrator.OrquestratorRemote</business-remote>
<ejb-class>orquestrator.OrquestratorBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor></assembly-descriptor>
</ejb-jar>
META-INF\weblogic-ejb-jar.xml
(I've placed work manager configuration in this file, as I've seen on a tutorial on the internet)
<weblogic-ejb-jar xmlns="http://www.bea.com/ns/weblogic/90"
xmlns:j2ee="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.bea.com/ns/weblogic/90
http://www.bea.com/ns/weblogic/90/weblogic-ejb-jar.xsd">
<weblogic-enterprise-bean>
<ejb-name>OrquestratorBean</ejb-name>
<jndi-name>OrquestratorBean</jndi-name>
<dispatch-policy>ResponseTimeReqClass-0</dispatch-policy>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
I've compiled this into a JAR and deployed it on WebLogic, as a library shared by administrative server and all cluster nodes on my solution (it's in "Active" state).
As I've seen in several tutorials and examples, I'm using this code on my application, in order to call the bean:
InitialContext ic = null;
try {
Hashtable<String,String> env = new Hashtable<String,String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
env.put(Context.PROVIDER_URL, "t3://localhost:7001");
ic = new InitialContext(env);
}
catch(Exception e) {
System.out.println("\n\t Didn't get InitialContext: "+e);
}
//
try {
Object obj = ic.lookup("OrquestratorBean");
OrquestratorRemote remote =(OrquestratorRemote)obj;
System.out.println("\n\n\t++ Remote => "+ remote.getClass());
System.out.println("\n\n\t++ initOrquestrator()");
remote.initOrquestrator();
}
catch(Exception e) {
System.out.println("\n\n\t WorkManager Exception => "+ e);
e.printStackTrace();
}
Unfortunately, this don't work. It throws an exception on runtime, as follows:
WorkManager Exception =>
javax.naming.NameNotFoundException:
Unable to resolve 'OrquestratorBean'.
Resolved '' [Root exception is
javax.naming.NameNotFoundException:
Unable to resolve 'OrquestratorBean'.
Resolved '']; remaining name
'OrquestratorBean'
After seeing this, I've even tried changing this line
Object obj = ic.lookup("OrquestratorBean");
to this:
Object obj = ic.lookup("OrquestratorBean#orquestrator.OrquestratorBean");
but the result was the same runtime exception.
Can anyone please help me detecting what am I doing wrong here? I'm having a bad time debugging this, as I don't know how to check out what may be causing this issue...
Thanks in advance for your patience and help.
Your EJB gets bound under the following JNDI name (when deployed as EJB module):
Object obj = ic.lookup("OrquestratorBean#orquestrator.OrquestratorRemote");
Note that I deployed your code (without the weblogic-ejb-jar.xml) as an EJB module, not as a shared library.
seems like your mapped-name in ejb-jar.xml "Orquestrator" may be overriding the mappedName=OrquestratorBean setting of the Bean.
Have you tried ic.lookup for "Orquestrator" ?