Re: [Asterisk-java-users] AsteriskServer as a service / in a spring context ?
Brought to you by:
srt
From: Gaëtan M. <gm...@ea...> - 2008-04-06 10:35:17
|
Hi Thanks for your time. >> 1/ There is no "start" method on the server. Instead it starts "on >> demand" when sending the first action, or worst, when one adds a >> listener on it. > > I think we can fix this by some refactoring and provide an > implementation that initializes automatically and one that throws > MCE or > IllegalStateException when a method is called that requires a > connection > and it is not yet initialized. Indeed I think some minor refactoring is the best option. In fact that's what I initially did but I later reworked this to keep a minimal patch against trunk. The AsteriskServerImpl class should of course maintain the old behavior, hence my idea to move common code in an abstract superclass than requires a connection object, and then handle connection creation/ initialization specifics in the subclasses. > > A simple solution might be to replace to calls to initializeIfNeeded() > by calls to a new protected method called checkInitialize() of > similar, > that can be overridden by subclasses and either throw an exception or > perform the initialization if its not yet initialized. Lol, checkinitialize() was exactly the name I used but once again I renamed it and wrapped it in InitializeifNeeded to reduce the patch size ;) However it should not throw an exception when called from addAsteriskServerListener, or, at least, addAsteriskServerListener should catch it in the service oriented implementation. The AsteriskServer interface should however still declare it throws an MCE but we'll know that it won't happen with the service oriented implementation. > >> Is there a good reason to call initializeIfNeeded() in the >> addAsteriskServerListener() method (and as a consequence have it >> potentially throw a MCE) ? > > The idea behind this that if you register a listener and the object > has > not yet been initialized you won't receive any events. In fact the > public initialize() method was only added later on, originally > AsteriskServer was meant to work out of the box with as few lines of > code as possible. - Maybe it's a bit too smart now :-) And that's really great for starters. We must keep the old behaviour. Another problem is that automatically starting the connection before adding the listener could also cause lost events (ie my "server initialized" event). > > >> 2/ If that very first connection fails in "initializeIfNeeded", >> then the >> method throws an MCE that escalades to the addAsteriskServerListener >> method and eventually reaches the spring context factory. As a >> result, >> the server is down and won't start again until we try to send >> something. >> Moreover as the exception was thrown before the actual >> listeners.add() >> call, our listener is eventually not added. >> So the application completely fails to start in spring if it can't >> reach >> asterisk while starting. >> > > This is a bit harder. Usually I prefer things to fail early so you > notice if you put in the wrong hostname or credentials. I understand > that there are situations where this is not the required behavior. As > you point out preventing the application from deploying due to > Asterisk > maintainance is not acceptable. > To solve this issue we may have to add a feature to force a > ManagerConnection to keep connecting. This won't probably look much > different from the RECONNECTING hack, except for that it's hidden :-) In the demo patch I attached, I now start a background thread whose aim is to try and force the first connection in a loop (and swallow MCE exception for that purpose). > > How should we proceed? > If you're interested in this kind of patch I can try and write it. This should be fairly easy. Gaetan |