Martijn Brinkers wrote on 12 February 2008 22:29: > But the 'problem' with mailet init is that there is no > guranteed order. > Lets suppose I need to access the global resource from within the init > of a more than one mailet. Which mailet should initialize the global > resource? I can, of course, initialize it in every init to make sure > that the global resource is initialize but that's not > something I like.
One solution is to use lazy initialization. Options include... 1) Initialize early: Have the Mailet.init() method invoke a Factory object that checks if the resource is initialized. If it isn't, it initializes the resource and saves it before returning the newly initialized object. If it is, it returns the previously initialized and saved object. The returned object is saved by the Mailet.init() method as an instance for use by the Mailet.service() method. The Mailet.service() method uses the object instance saved by the Mailet.init() method. The Mailet.destroy() method calls the Factory to tear down the resource which the Factory will do if it hasn't already been torn down. 2) Initialized on demand: The Mailet.init() method does nothing. The Mailet.service() method calls the Factory described in (1) each time it is invoked to obtain the initialized object. Initialization will occur on the first call, thereafter the already initialized object will be returned. The Mailet.destroy() method is the same as (1). For heavyweight resources and those with a potential for failure I prefer (1) as the costs and risks are resolved before we start servicing requests. For others I prefer (2), but either is fine. > As an experiment I added my own 'service' to assembly.xml which is > configured upon startup (the service implements Configurable). This > seems to be working but somewhere on the WIKI I read that this > functionality will be 'removed' by giving mailets their own > classloader. AFAIK a separate class loader isn't happening anytime soon, though others may correct me. You could wrap your service acquisition in the Factory described above so that how a resource is obtained is insulated from the mailets that use it. Then just the Factory needs to change if your resource management approach changes. I hope this helps. -- Steve > > Martijn > > On Tue, 2008-02-12 at 22:09 +0000, Steve Brewin wrote: > > Hi > > > > The Mailet init() method offers the oppurtunity to set things up. As > > documented here - > http://james.apache.org/server/2.3.1/custom_mailet.html - > > it is called just once as the mailet container is started. > > > > -- Steve > > > > > > > > > -----Original Message----- > > > From: Martijn Brinkers [mailto:[EMAIL PROTECTED] > > > Sent: 12 February 2008 20:45 > > > To: James Users List > > > Subject: Where should I put my initialization code? > > > > > > > > > Hi, > > > > > > For my mailets/matchers I need to initialize some global > objects upon > > > start of James. One way to solve this would be to initialize > > > on demand, > > > ie when a getter is called for the global object. I would > > > however prefer > > > to initialize the objects beforehand because it makes > testing somewhat > > > easier. Is there a way to do this differently with James? > > > Should I write > > > a James service for this? > > > > > > Martijn > > > > > > > > > > --------------------------------------------------------------------- > > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > > > > > > > > > > --------------------------------------------------------------------- > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
