【问题标题】:How to modify JNDI Custom Resources Properties Values on the fly如何动态修改 JNDI 自定义资源属性值
【发布时间】:2013-08-02 11:10:18
【问题描述】:

我的架构:
GlassFish Server 开源版 3.1.2.2 (5)
Java EE 6
Eclipse IDE

我创建了一个 EJB 计时器,它会打印一条日志消息:

@Startup
@Singleton
public class ProgrammaticalTimerEJB {
    private final Logger log = Logger.getLogger(getClass().getName());

    @Resource(name = "properties/mailconfig")
    private Properties mailProperties;

    @Resource
    private TimerService timerService;

    @PostConstruct
    public void createProgrammaticalTimer() {
        log.log(Level.INFO, "ProgrammaticalTimerEJB initialized");
        ScheduleExpression everyTenSeconds = new ScheduleExpression().second("*/10").minute("*").hour("*");
        timerService.createCalendarTimer(everyTenSeconds, new TimerConfig("passed message " + new Date(), false));
    }

    @Timeout
    public void handleTimer(final Timer timer) {
        log.info(new Date().toGMTString() + " Programmatical: " + mailProperties.getProperty("to"));
    }
}

这个类注入我的自定义 JNDI 资源:

    @Resource(name = "properties/mailconfig")
    private Properties mailProperties;

Eclipse 控制台:

INFO: 2 Aug 2013 10:55:40 GMT Programmatical: tim.herold@mylocal.de
INFO: 2 Aug 2013 10:55:50 GMT Programmatical: tim.herold@mylocal.de
INFO: 2 Aug 2013 10:56:00 GMT Programmatical: tim.herold@mylocal.de

Glassfish 设置

asadmin> get server.resources.custom-resource.properties/mailconfig.property

server.resources.custom-resource.properties/mailconfig.property.to=tim.herold@mylocal.de

Command get executed successfully.
asadmin>





现在我想在应用程序运行时更改此属性值。 通过 Adminconsole 或 Asadmin 编辑它不起作用。 这可能吗,还是有其他/更好的解决方案?

提前非常感谢

【问题讨论】:

    标签: java jakarta-ee properties glassfish jndi


    【解决方案1】:

    有可能解决您的问题:

    如果应用程序使用resource injection,GlassFish Server 会调用JNDI API,而应用程序不需要这样做。

    一旦注入,属性就不会重新加载,并且默认情况下没有直接重新加载资源的可能性。

    但是,应用程序也可以通过直接调用JNDI API 来定位资源。

    您需要为您的Custom Resoruce 执行JNDI Lookup,无论是预定的还是每次使用属性之前。这段代码对我有用:

    @Timeout
    public void handleTimer(final Timer timer) throws IOException, NamingException {
        Context initialContext = new InitialContext();
        mailProperties = (Properties)initialContext.lookup("properties/mailconfig");
        log.info(new Date().toGMTString() + " Programmatical: " + mailProperties.getProperty("to"));        
    }
    

    【讨论】:

      【解决方案2】:

      据我了解,mailProperties资源是在容器实例化EJB之后注入的,在lifecycle of bean.中只出现一次

      因此,他无法使用期货属性更改。

      另一种方法是尝试在@Timeout 方法中查找邮件属性。

      【讨论】:

        猜你喜欢
        • 2014-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多