简体   繁体   中英

How to modify JNDI Custom Resources Properties Values on the fly

My architecture:
GlassFish Server Open Source Edition 3.1.2.2 (5)
Java EE 6
Eclipse IDE

I created a EJB Timer, which prints a log message:

@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"));
    }
}

This class injects my custom JNDI Resource:

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

Eclipse Console:

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 settings

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>



在此处输入图片说明

Now i want to change this property value during application runtime. Editing it via Adminconsole or Asadmin doesnt work. Is this possible, or is there an other/better soulution?

Many thanks in advance

There is a possibility to solve your problem:

If an application uses resource injection , the GlassFish Server invokes the JNDI API , and the application is not required to do so.

Once injected the properties are not reloaded and there is no direct posibility to reload the resource by default.

However, it is also possible for an application to locate resources by making direct calls to the JNDI API .

You need to do a JNDI Lookup for your Custom Resoruce , either scheduled or everytime before using the properties. This code worked for me:

@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"));        
}

According to my understanding, the mailProperties resource is injected after the EJB is instantiated by the container which only occurs one time in the lifecycle of bean.

Therefore, futures properties changes are not available to him.

An alternative could be to try to lookup the mailProperties inside @Timeout method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM