如何动态修改JNDI自定义资源属性值

Khi*_*nsu 1 java jndi properties glassfish java-ee

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

我创建了一个EJB Timer,它打印一条日志消息:

@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"));
    }
}
Run Code Online (Sandbox Code Playgroud)

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

    @Resource(name = "properties/mailconfig")
    private Properties mailProperties;
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)



在此输入图像描述

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

提前谢谢了

Den*_*hel 6

有可能解决您的问题:

如果应用程序使用resource injection,GlassFish Server将调用JNDI API,并且不要求应用程序执行此操作.

注入后,属性不会重新加载,并且默认情况下无法直接重新加载资源.

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

你需要做一个JNDI Lookup为你的Custom Resoruce使用特性之前,无论是定期或每次.这段代码对我有用:

@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"));        
}
Run Code Online (Sandbox Code Playgroud)