如何将变量扩展或传递给persistence.xml

时间:2014-05-28 16:25:58

标签: java jpa glassfish rmi

我有两台Glassfish服务器,每台运行一个我的应用程序的实例。我想在它们之间启用缓存协调。为此,我需要在persistence.xml文件中配置缓存协调。问题是eclipselink.cache.coordination.rmi.url属性的值需要引用应用程序运行的框,即rmi://myserver1:8686/

我想也许persistence.xml可以扩展系统属性,但似乎它不能。

我的JVM选项包含:

-DCOORDINATION_RMI_URL=rmi://myserver1:8686

我的persistence.xml文件包含以下内容:

<property name="eclipselink.cache.coordination.rmi.url" value="${COORDINATION_RMI_URL}" />
<property name="eclipselink.session.customizer" value="com.foobar.conf.CacheCoordinationSessionCustomizer"/>

然后日志抱怨以下错误:

[#|2014-05-28T16:40:59.688+0100|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=136;_ThreadName=Thread-2;|[EL Warning]: 2014-05-28 16:40:59.685--ServerSession(1341405133)--Thread(Thread[Thread-60,5,grizzly-kernel])--Local Exception Stack: 
Exception [EclipseLink-22102] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.RemoteCommandManagerException
Exception Description: Could not post connection in local naming service under name ${COORDINATION_RMI_URL}/01fa1eea-7ab2-4351-ab0b-4cf7b7f942f2
Internal Exception: java.net.MalformedURLException: invalid URL String: ${COORDINATION_RMI_URL}/01fa1eea-7ab2-4351-ab0b-4cf7b7f942f2
    at org.eclipse.persistence.exceptions.RemoteCommandManagerException.errorBindingConnection(RemoteCommandManagerException.java:89)

我已经尝试过设置SessionCustomizer,但它似乎无法更改/设置任何属性,至少它不会改变应用程序的行为。

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.eclipse.persistence.config.SessionCustomizer;
import org.eclipse.persistence.sessions.JNDIConnector;
import org.eclipse.persistence.sessions.Login;
import org.eclipse.persistence.sessions.Session;

public class CacheCoordinationSessionCustomizer implements SessionCustomizer {

    public void customize(Session session) throws Exception {
        JNDIConnector connector = null;
        Context context = null;
        try {
            context = new InitialContext();
            if (context != null) {
                Properties p = System.getProperties();
                if(p.getProperty("COORDINATION_RMI_URL") != null) {
                    session.setProperty(PersistenceUnitProperties.COORDINATION_RMI_URL, p.getProperty("COORDINATION_RMI_URL"));
                    session.setProperty(PersistenceUnitProperties.COORDINATION_PROTOCOL, "rmi");
                    session.setProperty(PersistenceUnitProperties.COORDINATION_NAMING_SERVICE, "rmi");
                }
            } else {
                throw new Exception("_JPAEclipseLinkSessionCustomizer: Context is null"); 
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我知道我可以更改构建过程以更改persistence.xml文件并创建特定于机器的war文件,但这不是一个有效的解决方案,因为它不能扩展到n台机器。

1 个答案:

答案 0 :(得分:0)

EclipseLink使用持久性属性来构建会话对象。构建完成后,您可以使用会话自定义程序直接更改会话 - 不再使用这些属性。

在自定义程序中,请致电session.getCommandManager().setUrl(p.getProperty("COORDINATION_RMI_URL"));