cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
110
Views
0
Helpful
0
Comments
cdnadmin
Community Member
This document was generated from CDN thread

Created by: null on 14-01-2004 07:03:26 PM
I am writing some custom elements and read in the "design considerations" section of the programmers guide that I should make all my attributes static since there is only one instance created. My question is do I then need to "protect" access (i.e. synchronized) when I change the value of any of these attributes? I guess another way to ask the same question is do all my calls for a particular application run in one thread, or is there a separate thread for each call?

Thanks,
Mike

Subject: RE: thread safety in custom elements
Replied by: null on 14-01-2004 09:23:58 PM
Hi Mike,

The best answer to this question can actually be found on page 36 of the Programmers Guide:

"The Audium Server creates a single instance of each configurable element and calls its execution method whenever it is visited in the call flow. This is very important because in this design, the element class acts as if it is static. The consequence of this is that all member variables act as static variables, meaning that once changed, every caller experiencing that element is exposed to the same value. It is highly recommended to use only static final member variables and keep all other variables local to the execution method."

So, if you use any member variables for an element, Audium suggests to use [b:63cdb932bb]static final[/b:63cdb932bb] variables (i.e. constants) where all other required variables remain [b:63cdb932bb]local[/b:63cdb932bb] to the execution method (doAction, doDecision, or addXmlBody).

If you want to use member variables, since a single instance will be shared across all calls in an application, yes you would need to synchronize any code that accesses or changes those variables. We still recommend against using member variables, though. Local variables to the execution method and session-scoped variables such as element or session data should provide most of the desired functionality. The only situation where you would use a member variable or a static variable would be if you want data stored in this variable to be retained across calls to the application (basically instead of using a database or external property file). This still has the defficiency of not being very permanent - updating the application with the updateApp administration script will erase any data stored in member variables or static variables.

As for how the Audium Server runs in a multi-threaded environment, the Audium Server is basically a web application archive (WAR) that runs on top of an existing multi-threaded Java application server. Typically, these servers keep a thread pool available (you can control the size of this pool in the application server configuration). When an HTTP request is received from the voice browser, a new thread is taken from the pool and used to handle the request (i.e. produce the VoiceXML page). When done, the thread is returned to the pool. So one call to an Audium application does not run in a single thread, it can potentially run in hundreds, one for each VoiceXML page produced in the call. The session is the way the application server keeps track of all the hapennings of the call, so as long as you refer to only local variables of an element's execution method and element or session data (which is stored in the application server session), you do not have to worry at all about running in a multi-threaded environment. Obviously if you are using member variables or static variables you do have to keep this behavior in mind and ensure that all uses are synchronized.

The section on design in the Programmer's Guide gets into the detail about the environment the Audium Server runs under. I highly recommend reading this section carefully to anyone (not just you Mike!) interested in constructing custom elements.

Hope this helps!

Subject: RE: thread safety in custom elements
Replied by: null on 14-01-2004 10:02:38 PM
After re-reading my above post I realized it may have sounded a bit snippy. I apologize if it came off that way, I certainly did not mean to tone the message in this manner.

Please accept my apology on this,

Elam Birnbaum

Subject: RE: thread safety in custom elements
Replied by: null on 14-01-2004 10:22:36 PM
No problem. Not snappy to me.

I missed the 'final' in 'static ifnal' when I read the doc you
quoted and that caused me some confusion of what I read
later.

One thing that should be mentioned in the docs, and it might
be but I don't remember readin it, is that session data also
needs to be thread safe because different calls are using the same
session object at the same time.

And I presume all your API code is thread safe so I shouldn't
have to worry about multiple phone calls making calls
into the API, say getting session data?

Two requests I have for enhancements. One is you make
available a place to store application-level data. That is, data
that lasts longer than a session. Right now we are limited to
static member variables on one of our elements (likely a
custom start call element), and that falls apart when
you update an application. Second, we need a way to initialize our
application. That is, when the app is first loaded by the audium
server, it would be nice if we could call some code to do
some application-level initialization.

Thanks,

And try not to be so snappy......
...
....
I'm joking, I'm joking

Mike

Subject: RE: thread safety in custom elements
Replied by: null on 14-01-2004 11:07:04 PM
Hi Mike,

Thanks for such understanding so snappily 8-)

One last point, Java objects stored in session data do not actually need to be thread safe because only one call is accessing the Java object at any one moment in time. Java application servers create a session data store (basically an HTTPSession object behind the scenes) for each session that is used to store everything having to do with that single session (in our case, a phone call). Session data is just stored in this data store, so any instances of classes stored in session data are only accessed by a single call. The only way you can make information shared between calls is if a custom Java class whose instances are stored in session data contain static variables. Since all sessions of a particular Audium application share the same classloader, static variables would be shared across calls.

Yes, you are correct, the API code is all thread safe, so you don't need to worry about that.

Yeah, all this multi-threaded stuff can get confusing, but Audium makes it such that you really don't have to worry about working in a multi-threaded environments for most implementations.

We have definitely gotten requests for something like a "onServerStart" class and something like "application data" which can store information that spans all calls to an application, and its already in our feature request system. I will note there of more interest so hopefully it will be given higher priority to be implemented in a future Audium version.

Snippily yours,

Elam
Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community:

Quick Links