2013-08-02 62 views
1

我的架构飞:
GlassFish应用服务器版3.1.2.2(5)
Java EE 6的
的Eclipse IDE
如何修改JNDI自定义资源的属性值上

我创建了一个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: [email protected] 
INFO: 2 Aug 2013 10:55:50 GMT Programmatical: [email protected] 
INFO: 2 Aug 2013 10:56:00 GMT Programmatical: [email protected] 

Glassfish的设置

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

server.resources.custom-resource.properties/[email protected] 

Command get executed successfully. 
asadmin> 



enter image description here

现在我想改变在应用程序运行时该属性值。 通过Adminconsole或asadmin的编辑它不工作。 这是可能的,或者是有一个其他/更好soulution?

提前

回答

6

还有就是要解决你的问题的可能性:

如果应用程序使用resource injection,GlassFish服务器调用JNDI API,以及这样做的应用是不需要的。

一旦注入的属性不加载并没有直接posibility默认重新加载资源。

但是,它也有可能为应用程序通过向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"));   
} 
1

非常感谢据我了解,EJB是由只发生在lifecycle of bean.

因此一次性容器实例化后的mailProperties资源注入,期货属性的变化是不可用给他。

另一种可能是,试图查找@Timeout方法里面mailProperties。

相关问题