簡體   English   中英

從JNDI custom-resource檢索其他屬性

[英]Retrieving additional properties from JNDI custom-resource

我在Glassfish服務器上配置了這個JNDI自定義資源:

在此輸入圖像描述

我也部署了一個Web應用程序,在某些時候,我想獲得為我的自定義資源的“版本”附加屬性配置的值。

我的工廠類是這樣的:

public class TestCRFactory implements ObjectFactory {

    @Override
    public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) {
        if (obj instanceof Reference) {
            Reference ref = (Reference) obj;
            Enumeration<RefAddr> addrs = ref.getAll();
            while (addrs.hasMoreElements()) {
                RefAddr addr = addrs.nextElement();
                if (addr.getType().equals("version")) {
                    String version = (String) addr.getContent();
                    System.out.println(version); // it shows me "1"
                }
            }
        }
    }
}

如果我查找對象:

Context context = new InitialContext();
Object obj = context.lookup("test/TestCR");

我的代碼工作正常,我可以在工廠類中獲得“版本”屬性沒有問題。

但是現在我想獲得“version”屬性而不查找對象並調用工廠類。 我只想通過MBeanServer做類似的事情:

import javax.management.MBeanServer;
import java.lang.management.ManagementFactory;
import javax.management.ObjectName;

...
boolean existsObject = false;
String name = "amx:pp=/domain/resources,type=custom-resource,name=test/TestCR";
ObjectName objName = new ObjectName(name);
try {
    MBeanServer mbean = ManagementFactory.getPlatformMBeanServer();
    existsObject = mbean.getObjectInstance(objName) != null; // this line works
    if (existsObject) {
       Object attr = mbean.getAttribute(objName, "version"); // this line doesn't work. it doesn't give me the "version" property I want.
    }
} catch (Throwable e) {
    existsObject = false;
}

我的問題是:我做錯了什么? 我應該將屬性名稱放在name變量的末尾嗎? 或類似的東西?

我知道了!

只需使用這樣的getAttribute方法:

getAttribute("amx:pp=/domain/resources/custom-resource[test/TestCR],type=property,name=version", "Value");

所以我的最終代碼是:

boolean existsObject = false;
ObjectName objName = new ObjectName("amx:pp=/domain/resources,type=custom-resource,name=test/TestCR");
try {
    MBeanServer mbean = ManagementFactory.getPlatformMBeanServer();
    existsObject = mbean.getObjectInstance(objName) != null;
    if (existsObject) {
       Object attr = mbean.getAttribute("amx:pp=/domain/resources/custom-resource[test/TestCR],type=property,name=version", "Value");
       // here 'attr' var is indicating '1', as I've set! (I tested with other values too) 
    }
} catch (Throwable e) {
    existsObject = false;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM