Am 23.03.2011 18:44, schrieb Clement Escoffier: > > > On 23.03.11 18:36, "Henrik Niehaus" <[email protected]> wrote: > >> Am 23.03.2011 18:07, schrieb Clement Escoffier: >>> Hi, >>> >>> >>> On 23.03.11 16:37, "Henrik Niehaus" <[email protected]> wrote: >>> >>>> Hi felix users, >>>> >>>> I'm thinking about using iPojo to create component instances >>>> programatically. I have a class OsdSession, which represents a user >>>> session and has dependencies to several services. The OsdSession itself >>>> runs as a thread and handles user interaction until the user explicitly >>>> ends the session. >>>> My idea was to use iPojo to create new instances of the OsdSession, >>>> because then I don't have to create all the service lookup / handling, >>>> but let iPojo inject all dependencies. >>>> >>>> I played around with ipojo and I'm now able to create a new instance >>>> like so (simplified): >>>> >>>> @Component >>>> public class OsdSession implements Runnable {...} >>>> >>>> @Component >>>> public class Activator { >>>> >>>> @Requires(filter = >>>> "(factory.name=de.berlios.vch.osdserver.OsdSession)") >>>> private Factory sessionFactory; >>>> >>>> private OsdSession createSession() throws UnacceptableConfiguration, >>>> MissingHandlerException, ConfigurationException { >>>> InstanceManager instance = (InstanceManager) >>>> sessionFactory.createComponentInstance(null); >>>> instance.start(); >>>> OsdSession session = (OsdSession) instance.createPojoObject(); >>>> } >>>> >>>> } >>>> >>>> But with this code I have to check, if the instance is valid and call >>>> the validate and invalidate method and do all the stuff, which ipojo >>>> normally does for me. >>>> >>>> My goal was to get the same behaviour as if I had defined the instance >>>> in the metadata.xml. But instead of having only one instance, I want to >>>> create them, when they are needed. >>>> >>>> Another point is, that the instances should be disposed if the user >>>> session ends, so that everything gets cleaned up and I don't leak >>>> memory. >>>> >>>> What do you think about my approach? Is there a way to do this? Maybe >>>> there are other approaches to solve this kind of problems? >>> >>> You should let iPOJO creates the pojo object for you, and not call >>> createPojoObject. If you want to force the object creation, use >>> @Component(immediate=true). To retrieve the created object, just use >>> InstanceManager.getPojoObject() >>> >>> Regards, >>> >>> Clement >>> >> >> Thanks Clement, >> >> but this is only a technical thing. Any thoughts on my other questions? >> >> Do I have to manage the components lifecycle (check validity, call >> validate/invalidate etc.) on my own? > > No, iPOJO will do that for you. > >> >> Can I automatically dispose the component after the thread ended, or do >> I have to implement some kind of callback, which then calls the dispose >> method of ComponentInstance to achieve this behaviour? > > You must call dispose when you don't need the component anymore. > > Regards, > > Clement > >
Ok, thanks. I will go on with this implementation and see if it works for me. --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

