Announcement Announcement Module
Collapse
No announcement yet.
Hot to modify and refresh context at runtime Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hot to modify and refresh context at runtime

    Hello everybody,
    I'm having some troubles understanding the way I can modify and reload/refresh the context at runtime(without re-deploying my app).

    I need to add some bean definition to the context, depending from user behaviour (read: when a user submit a form, i have to add some bean to the application context, depending from user submitted data), then I (suppose) have to refresh the context to get my new beans available for any kind of operation (in detail: scheduling with Quartz).

    How can I do that?

    At the moment I tried to do something like this:

    in my ApplicationContextAware implementing class....
    Code:
    XmlWebApplicationContext ctx = (XmlWebApplicationContext) context; 
    System.out.println("bean count before"+ctx.getBeanDefinitionCount());
    registerBean(f); //f is my data Object-see below
    ctx.refresh();
    System.out.println("bean count after"+ctx.getBeanDefinitionCount());
    CronTriggerBean c= (CronTriggerBean) ctx.getBean(f.getNome()+"Job");
    and here's the registerBean(Fonte f) definition

    Code:
    private void registerBean(Fonte f){
    		try {
    			XmlWebApplicationContext ctx = (XmlWebApplicationContext) context;
    			DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) ctx.getBeanFactory();
    			BeanDefinitionBuilder simpleTriggerBeanBuilder = BeanDefinitionBuilder.rootBeanDefinition(org.springframework.scheduling.quartz.CronTriggerBean.class);
    			simpleTriggerBeanBuilder.addPropertyValue("cronExpression", "0/10 * * * * ?");
    			beanFactory.registerBeanDefinition(f.getNome()+"Job",simpleTriggerBeanBuilder.getBeanDefinition());
    		}catch(Exception e){e.printStackTrace();}
    		
    	}
    The output is:
    bean count before43
    bean count after43
    org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'ffffffffJob' is defined
    -bean count is always the same. Should be incremented by 1, doesn't?
    -'ffffffffJob" is the user submitted data

    What's wrong with that?
    Is this the right way to do what I'm trying to do(modify and refresh the context) ?
    Any suggestion?

    Thank you in advance.

    Regards.
    Last edited by Ciccio17; Jun 25th, 2009, 04:59 AM.
Working...
X