【问题标题】:Retrieving additional properties from JNDI custom-resource从 JNDI 自定义资源中检索附加属性
【发布时间】:2014-01-17 18:53:32
【问题描述】:

我在 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");

我的代码有效,我可以毫无问题地在工厂类中获取“版本”属性。

但现在我想在不查找对象并调用工厂类的情况下获取“版本”属性。我只想通过 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 变量的末尾吗?或者类似的东西?

【问题讨论】:

  • 我在这里没有看到任何与JNDI 相关的内容。请出示。另请描述您面临的确切问题。
  • @PM77-1 我认为现在我提出了更好的方式。
  • +1 优秀描述

标签: java glassfish jndi mbeans


【解决方案1】:

我明白了!

只需像这样使用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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多