Author Message
MichaelNorman
Joined: Jun 3, 2015
Messages: 448
Offline
Can you elaborate on the exact order in which the ServiceLifeCycle init/destroy, and the Service constructor are called? Also at what point would static members of the CallListener be initialized?

JoelEzell
Joined: Nov 15, 2013
Messages: 780
Offline
Hi Michael, sorry for the delayed response. I'm not quite sure what you mean by "Service constructor". Can you say more about that?

The ServiceLifeCycle.init() method is meant to be the primary place that services perform initailization functions similar to what you might find in a constructor. This method is invoked either when the service is deployed or upon Breeze node startup. I'm not entirely sure whether init() is inoked before or after static initalizers are invoked in your annotated call listener. They should be roughly concurrent and I'd recommend that you not design your code such that it relies on them being invoked in a particular order if you can avoid it.
MichaelNorman
Joined: Jun 3, 2015
Messages: 448
Offline
I'm speaking of the CallListener constructor.
I use an external library for logging, serilogj, which is a java port of the .net version. It has a function where it will write out the class which logging originated from but it has to be set AFTER its initial configuration, which right now I have placed in the ServiceLifeCycle init method.

Example of the CallListener's logging variable declaration would look like:

var cartLog = Log.ForContext("Source", typeof(CartController).FullName);

cartLog.Information("Adding {Item} to cart {CartId}", item, cart.Id);
cartLog.Information("Cart value is {CartTotal}", cart.Total);


I have tried placing this in the CallListener constructor as well as directly in the class variable declaration just like the Avaya logger is set up. In neither case is the correct info applied because it would appear that the CallListener members and constructor are called BEFORE the ServiceLifeCycle init method which seems to go against the purpose of that class.

JoelEzell
Joined: Nov 15, 2013
Messages: 780
Offline
Thanks for the additional information. We've not seen much use of constructors on the CallListener implementation object. In general, with annotated container-managed objects like this, I believe it's not considered best practice to do so. The ServiceLifeCycle init method is similar to the standard @PostConstruct annotation for JEE beans.
https://stackoverflow.com/questions/3406555/why-use-postconstruct

For your use case, could you perhaps have a global singleton that exposes static methods that allow access to your logger classes? ServiceLifeCycle.init() could initialize that singleton instance and your CallListener implementation could then access it when calls arrive.
Go to:   
Mobile view