Portable datasource jndi for JBoss and OC4j - java

I am new to JBoss development, I am migrating one application from OC4J to JBoss, What i came to know is jndi names are different in OC4J and JBoss Application Server, So i have one jndi datasource named as jdbc/sampleDataSource in OC4J datasource configuration. Similarly I have done a configuration for JBoss named as java:/jdbc/sampleDataSource.
As the ear should be able to deploy on both application server, I am facing issues while making code compatible for JBoss as well OC4j
For e.g
#Resource(mappedName = "jdbc/sampleDataSource")
private sampleDataSource dataSource;
How can i make above parameter compatible for JBoss as well OC4J as the jndi name is different in both Server?
There are also bean level data source injection:
#Resource( name = "jdbc/sampleDataSource", type = javax.sql.DataSource.class, mappedName = "jdbc/sampleDataSource", authenticationType = Resource.AuthenticationType.CONTAINER),
So please let me know what are the changes i need to perform to make the code compatible with JBoss AS7 and OC4J as well.

Related

Spring Boot: use database config from WildFly's standalone.xml

I'm currently developing an REST app whith Spring boot. For development, I hardcode my database configuration in application.properties. However, this app is going to be deployed on different WildFly servers, each of them defining their DB config (user credentials) in standalone.xml.
As I'm a newbe to Spring/Java, here is my question: How can I use the DB config from standalone for my spring app?
I already did some research. What I got is to define Services with an #PersistenceContext annotated entity manager. But how can I use it without defining new services, just by using plain JpaRepositories?
In Wildfly you will have a datasource configured with at JNDI name.
Instead of configuring url, username and password you have to configure this JNDI name:
spring.datasource.jndi-name=java:jboss/datasources/myGreatDS
That's all.

Wildfly unwanted eager EJB Injection

i am transforming a Java EE application from Weblogic 12.2.1.3 to Wildfly 18.1 (need to stick to Java 1.8) and i am facing a problem with "Eager" EJB injection.
For example, in the deployment of war named MyWar that contains a ViewScopedBean:
#ViewScoped
public class BatchCCBean implements Serializable {
#EJB(lookup = "java:global/consumer.batch.common/BatchJobService!com.company.api.batch.RemoteBatchJobService")
RemoteBatchJobService batchJobService;
}
In Weblogic, if consumer.batch.common is NOT deployed during MyWar deployment, everything goes OK, deployment succeeds. If and only IF you try to initialize the BatchCCBean then you get an Exception. In other words it uses lazy EJB initialization.
BUT in Wildfly, I cannot deploy the MyWar application without deploying consumer.batch.common first because Weld in Wildfly 18.1 tries to eagerly initialize all Remote EJB injections during deployment time.
This is not a wanted behavior. Does anybody know how to "disable eager Remote EJB Injection" in Wildfly?
Thanks

Issues deploying a Java application to Tomcat

I built a simple Java web application. It provides a series of RESTful APIs for the user to carry out certain operations on a Java DB through a web interface. I used NetBeans environment during the development, and Glassfish for testing.
Now that I finished it, I would like to be able to deploy it on another machine using binaries (although as for now I use the same machine until I learn how to do it).
I installed Tomcat 7, and moved the .war file into Tomcat's webapp folder. The application deploys. Thereafter I try to read some data from the databse using a button I created just for this, but get the following error
I am not sure what went wrong, but I have two theories.
1) The web application cannot connect to the database. Yet when I attempted to run the application again, after starting JavaDB from NetBeans, there was no difference.
2) Somehow, the application cannot reach the Node service. I assumed that there will be no need to change the API links while moving the app, but perhaps I was wrong.
Or maybe there is some other issue I did not consider? I will be grateful for any advice about how to properly deploy such an application.
EDIT: The issue was solved by using TomEE.
The error is come from your application server of choice.
TomCat is only a servlet container (means it only support Servlet/JSP).
Any other feature (JAX-RS, CDI etc) require a Java EE certified server e.g. GlassFish, WildFly,Payara, WebLogic, OpenLiberty or TomEE.
TomEE could be your best bet if you want to use TomCat in your production or test environment, it is basically TomCat + Java EE other feature.
EDIT:
TomEE don't have a GUI for JNDI datasource configuration like GlassFish, you need to edit conf/tomee.xml
<Resource id="myDataSource" type="javax.sql.DataSource">
jdbcDriver = org.apache.derby.jdbc.ClientDriver
jdbcUrl = jdbc:derby://localhost:1527/dbname
userName = app
password = app
</Resource>
And in your java code:
#Path("resources")
#Stateless
public class MyResources{
#Resource(name="myDataSource")
DataSource dataSource;
#GET
public Response SomeMethod(){
//Do stuff here
}
}
You can check here for more detail configuration on data source.

Custom jndi name for ejb3.X with Jboss 7.1

I am migrating one application from weblogic to jboss7.1 and ejb2.0 to ejb3.X. The application is using jndi name for ejb lookup.
Could someone help me to find out how can i configure my custom jndi name with ejb3.0 and jboss 7.1.
I have read on internet that in Jboss 7.1 there is no concept of jndi-name. if it is true. could someone let me know how can i lookup this jndi name, getting registered by jboss for ejbs present in ejb-jar.xml
java:global/pirptfe/RRMSessionFacadeEJB!com.bfm.app.pirptfe.heatmap.ejb.RRMSessionFacadeHome
java:app/pirptfe/RRMSessionFacadeEJB!com.bfm.app.pirptfe.heatmap.ejb.RRMSessionFacadeHome
java:module/RRMSessionFacadeEJB!com.bfm.app.pirptfe.heatmap.ejb.RRMSessionFacadeHome
java:jboss/exported/pirptfe/RRMSessionFacadeEJB!com.bfm.app.pirptfe.heatmap.ejb.RRMSessionFacadeHome
java:global/pirptfe/RRMSessionFacadeEJB!com.bfm.app.pirptfe.heatmap.ejb.RRMSessionFacadeRemote
java:app/pirptfe/RRMSessionFacadeEJB!com.bfm.app.pirptfe.heatmap.ejb.RRMSessionFacadeRemote
java:module/RRMSessionFacadeEJB!com.bfm.app.pirptfe.heatmap.ejb.RRMSessionFacadeRemote
java:jboss/exported/pirptfe/RRMSessionFacadeEJB!com.bfm.app.pirptfe.heatmap.ejb.RRMSessionFacadeRemote
i got the answer of my question. in ejb3.X on jboss7.1 and above there is no concept of custom jndi. you have to call your ejb as below
ctx.lookup("java:global/pirptfe/RRMSessionFacadeEJB!com.bfm.app.pirptfe.heatmap.ejb.RRMSessionFacadeHome");

ejb 3.1, weblogic 12c, java 1.6 Jndi Lookup issue

I have problem in configuring JNDI lookup for ejb3.1 and weblogic 12c, jdk1.6
#Remote
public interface Bank{
public String accounts();
}
#Stateless(name="BankSession")
public class BankSessionBean implements Bank{
#Override
public String accounts() {
//////// }
ejb-jar.xml:
<enterprise-beans>
<session>
<display-name>BankSession</display-name>
<ejb-name>BankSession</ejb-name>
<business-remote>com.examples.Bank</business-remote>
<ejb-class>com.examples.BankSessionBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
<enterprise-beans>
weblogic-ejb-jar.xml:
<weblogic-enterprise-bean>
<ejb-name>BankSession</ejb-name>
<jndi-name>BankSession</jndi-name>
</weblogic-enterprise-bean>
client code:
When i start using Jndi Look up using this syntax
java:comp/env/ejb/BankSession
Its giving following error.
javax.naming.NameNotFoundException: While trying to lookup 'java:comp/env/ejb/BankSession' didn't find subcontext.
Here ejbbean and client code runs on different jvm's.
Which Jndi look up should i use ?
java:comp/env/ejb/BankSession'
java:global/applicationName/moduleName/BankSession
java:module/BankSession
java:app/moduleName/BankSession
java:comp/env/ejb/BankSession is working fine with ejb 3.0 and oc4j server.
When i migrate to ejb3.1 and weblogic 12c, its not working.
I even tried without using weblogic deployment xml files. Same issue encountered.
How should i configure my Jndi here ? Please help as i am facing this issue since long time.
For a lookup in the form of java:comp/env/... you need an EJB ref in the deployment descriptor of the component that does the lookup!
java:comp/env/... is always relative to the component that does the lookup.
A lookup name that is independent of the actual component that does the lookup and works from anywhere inside your application is 4.: java:app/moduleName/...
java:global/appName/... will fail if you decide to rename your ear file one day.
java:module/... only works inside a component that is part of the same module.
So 4. is IMO the best alternative.

Categories

Resources