我如何使JNDI名称与GlassFish和WildFly兼容 [英] How do I make JNDI names compatible with both GlassFish and WildFly

查看:106
本文介绍了我如何使JNDI名称与GlassFish和WildFly兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Java EE 7应用程序,并要求将应用程序部署到运行GlassFish 4.0或WildFly 8.1.0的应用程序服务器上。我得到的问题是GlassFish和WildFly为JNDI名称使用了稍微不同的格式,但我看不到如何使我的应用程序与两者兼容。

在GlassFish中,我的persistence.xml文件引用数据源jdbc / myDataSouce,但在WildFly中,数据源需要为java:/ jdbc / myDataSource。



对于使用@Resource注释的类也是如此。在GlassFish中,使用JavaMail的类的注释将是@Resource(name =mail / myMailSession),但要部署到WildFly,这需要是@Resource(name =java:mail / myMailSession)。 >

我知道我可以解压缩EAR和JAR文件以手动编辑诸如persistence.xml的文件,但我无法为使用@Resource注释的类执行此操作。 / p>

有没有办法让我的编译应用程序可以部署到GlassFish和WildFly上,而无需维护两个不同版本的代码?我假设答案可能与应用程序特定的部署描述符有关,但我找不到涵盖这两种情况的任何示例。



任何人都可以指引我朝着正确的方向您可以修改Wildfly JNDi名称,并从相应的JNDI名称中去除不需要的前缀,以找到最不共同的分母。

在这两个应用服务器。 Glassfish和JBoss AS 7.1适用于我。因为我期望Wildfly在这方面与JBoss兼容,所以我猜它也适用于Wildfly。



持久性



注入为:

  @PersistenceContext(unitName =TestPU)
private EntityManager entityManager ;

或通过 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>

相应的 persistence.xml



 < persistence version =2.0xmlns =http://java.sun.com/xml/ns/persistencexmlns :xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http://java.sun.com/xml/ns/persistence http://java.sun.com/ XML / NS /持久/ persistence_2_0.xsd> 
< persistence-unit name =TestPUtransaction-type =JTA>
< jta-data-source> datasources / TestDS< / jta-data-source>
< class> org.jeeventstore.persistence.jpa.EventStoreEntry< / class>
<属性>
< property name =hibernate.show_sqlvalue =false/>
< property name =hibernate.format_sqlvalue =true/>
< property name =hibernate.hbm2ddl.autovalue =create-drop/>
< property name =hibernate.connection.charSetvalue =UTF-8/>
< property name =eclipselink.logging.levelvalue =FINE/>
< property name =eclipselink.logging.level.sqlvalue =FINE/>
< property name =eclipselink.logging.parametersvalue =true/>
< property name =eclipselink.ddl-generationvalue =drop-and-create-tables/>
< / properties>
< / persistence-unit>
< /余辉>

(注意简单的 jta-data-source JNDI名称)



以下是用于在部署中指定Derby数据库的 glassfish-resources.xml 文件,类似的设置可以用于MySQL或Postgres。

 < resources> 

jndi-name =datasources / TestDS/>

res-type =javax.sql.DataSource
datasource-classname =org.apache.derby。 jdbc.EmbeddedDataSource
is-isolation-level-guaranteed =false>
< property name =databaseNamevalue =target / databases / derby/>
< property name =createDatabasevalue =create/>
< / jdbc-connection-pool>

< /资源>

以及JBoss standalone.xml

 < datasource jta =truejndi-name =java:/ datasources / TestDSpool-name = TestDSenabled =trueuse-ccm =false> 
< connection-url> jdbc:postgresql:// localhost / test_db< / connection-url>
...
< / datasource>



资源



我没有注入Glassfish上的JavaMail组件,但类似于数据生成设置,可能值得尝试从 @Resource <>中删除 java: / code>注解。

$ p $ @Resource(name =mail / myMailSession)
/ code>

,然后配置Wildfly,使邮件资源在 java:mail / myMailSession JNDI位置。



通过 ejb-jar.xml注入



另一种选择是通过 ejb-jar.xml 文件手动注入字段,然后使用构建工具,如maven将 ejb-jar-glassfish.xml ejb-jar-wildfly.xml 中的任意一个复制到所需的 ejb-jar.xml 在汇编时。 在我们的一个项目中,我们使用混合方法来避免xml配置带来的负担:我们通过 ejb-jar.xml 将例如持久化上下文注入到 PersistenceContextProvider 中,然后使用CDI注入 PersistenceContextProvider 通过 @EJB 到EJB中,由于它们驻留在相同的EAR中,因此无需进一步配置即可找到。


I am developing a Java EE 7 application and have a requirement for the application to be deployed onto application servers running either GlassFish 4.0 or WildFly 8.1.0. The issue I've got is GlassFish and WildFly use slightly different formats for JNDI names but I can't see how to make my application compatible with both.

In GlassFish my persistence.xml file references the data source jdbc/myDataSouce, but in WildFly the data source needs to be java:/jdbc/myDataSource.

The same is also true for classes that are annotated with @Resource. In GlassFish the annotation for a class using JavaMail would be @Resource(name = "mail/myMailSession"), but to deploy onto WildFly this would need to be @Resource(name = "java:mail/myMailSession").

I know that I could unpack the EAR and JAR files to manually edit files such as persistence.xml but I can't do that for classes that have been annotated with @Resource.

Is there a way I can allow my complied application to be deployed onto GlassFish and WildFly without maintaining two different versions of the code? I'm assuming the answer probably lies with application specific deployment descriptors but I can't find any examples that cover these two scenarios.

Can anyone point me in the right direction please?

解决方案

You can modify the Wildfly JNDi names and strip the undesired prefixes from the respective JNDI names to find the least common denominator in both app servers. The following works for me with Glassfish and JBoss AS 7.1. Since I expect Wildfly to be backwards-compatible to JBoss in this regard, I guess it'll work for Wildfly as 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 the simple jta-data-source JNDI name)

Here's a glassfish-resources.xml file used to specify a Derby database on deployment, a similar setup can be used for 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 the settings from the 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 a JavaMail component on Glassfish, but similar to the datasoruce settings, it might be worth a try to strip the "java:" part from the @Resource annotation as well.

@Resource(name = "mail/myMailSession")

and then configure Wildfly such that that the mail resource is available at the "java:mail/myMailSession" JNDI location.

Injection via ejb-jar.xml

Another option is to manually inject the fields via a ejb-jar.xml file, and then use a build tool such as maven to copy either of ejb-jar-glassfish.xml or ejb-jar-wildfly.xml to the desired ejb-jar.xml at assembly time.

In one of our projects we use a mixed approach to avoid the burden with the xml configuration: We configure a small number of "provider" beans via ejb-jar.xml to inject, e.g., the persistence context into a PersistenceContextProvider, and then use CDI to inject the PersistenceContextProvider into the EJBs via @EJB, which are found without further configuration since they reside in the same EAR.

这篇关于我如何使JNDI名称与GlassFish和WildFly兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆