Saturday 15 September 2012

java - How do I make JNDI names compatible with both GlassFish and WildFly -



java - How do I make JNDI names compatible with both GlassFish and WildFly -

i developing java ee 7 application , have requirement application deployed onto application servers running either glassfish 4.0 or wildfly 8.1.0. issue i've got glassfish , wildfly utilize different formats jndi names can't see how create application compatible both.

in glassfish persistence.xml file references info source jdbc/mydatasouce, in wildfly info source needs java:/jdbc/mydatasource.

the same true classes annotated @resource. in glassfish annotation class using javamail @resource(name = "mail/mymailsession"), deploy onto wildfly need @resource(name = "java:mail/mymailsession").

i know unpack ear , jar files manually edit files such persistence.xml can't classes have been annotated @resource.

is there way can allow complied application deployed onto glassfish , wildfly without maintaining 2 different versions of code? i'm assuming reply lies application specific deployment descriptors can't find examples cover these 2 scenarios.

can point me in right direction please?

you can modify wildfly jndi names , strip undesired prefixes respective jndi names find to the lowest degree mutual denominator in both app servers. next works me glassfish , jboss 7.1. since expect wildfly backwards-compatible jboss in regard, guess it'll work wildfly well.

persistence

inject as:

@persistencecontext(unitname="testpu") private entitymanager entitymanager;

or via ejb-jar.xml:

<persistence-context-ref> <persistence-context-ref-name>entitymanager</persistence-context-ref-name> <persistence-unit-name>testpu</persistence-unit-name> <injection-target> ... </injection-target> </persistence-context-ref>

the corresponding persistence.xml:

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation=" http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="testpu" transaction-type="jta"> <jta-data-source>datasources/testds</jta-data-source> <class>org.jeeventstore.persistence.jpa.eventstoreentry</class> <properties> <property name="hibernate.show_sql" value="false"/> <property name="hibernate.format_sql" value="true"/> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> <property name="hibernate.connection.charset" value="utf-8"/> <property name="eclipselink.logging.level" value="fine"/> <property name="eclipselink.logging.level.sql" value="fine"/> <property name="eclipselink.logging.parameters" value="true"/> <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/> </properties> </persistence-unit> </persistence>

(note simple jta-data-source jndi name)

here's glassfish-resources.xml file used specify derby database on deployment, similar setup can used mysql or postgres.

<resources> <jdbc-resource pool-name="arquillianembeddedderbypool" jndi-name="datasources/testds"/> <jdbc-connection-pool name="arquillianembeddedderbypool" res-type="javax.sql.datasource" datasource-classname="org.apache.derby.jdbc.embeddeddatasource" is-isolation-level-guaranteed="false"> <property name="databasename" value="target/databases/derby"/> <property name="createdatabase" value="create"/> </jdbc-connection-pool> </resources>

and settings jboss standalone.xml:

<datasource jta="true" jndi-name="java:/datasources/testds" pool-name="testds" enabled="true" use-ccm="false"> <connection-url>jdbc:postgresql://localhost/test_db</connection-url> ... </datasource> resources

i have not injected javamail component on glassfish, similar datasoruce settings, might worth seek strip "java:" part @resource annotation well.

@resource(name = "mail/mymailsession")

and configure wildfly such that mail service resource available @ "java:mail/mymailsession" jndi location.

injection via ejb-jar.xml

another alternative manually inject fields via ejb-jar.xml file, , utilize build tool such maven re-create either of ejb-jar-glassfish.xml or ejb-jar-wildfly.xml desired ejb-jar.xml @ assembly time.

in 1 of our projects utilize mixed approach avoid burden xml configuration: configure little number of "provider" beans via ejb-jar.xml inject, e.g., persistence context persistencecontextprovider, , utilize cdi inject persistencecontextprovider ejbs via @ejb, found without farther configuration since reside in same ear.

java java-ee glassfish jndi wildfly

No comments:

Post a Comment