This document was generated from CDN thread
Created by: null on 16-12-2005 04:14:41 AM
Hi
I have int's and int arrays that are initialized in my custom Action element. Once initialized in this element, the value of these int's and int arrays do not change during the life of the app, but need to be available to my other custom Action elemens for reading. How should I declare these and where?
Thanks,
Dan
Subject: RE: Variable scope
Replied by: Michael Bochynski on 19-12-2005 02:04:31 PM
Hi Dan,
If you want to make the data, either int's, arrays, or other objects, available for all the elements in the call flow, you can store them in a session data. You can use [b:be46cf9294]setSessionData(String key, Object data)[/b:be46cf9294] to store any object. Key, in this case, is your unique identifier, assigned to this object.
Hope this helps,
Michael
Subject: RE: Variable scope
Replied by: null on 19-12-2005 05:15:07 PM
Thanks Michael,
I have been uaing setSessioData in this manner for some of my variables. I was wondering if there was another way, posssibly involving less code? I have several int's and arrays that get initialized on app start and do not change. Is setSessionData the only way to make this data available across multiple custom action elements?
Thanks,
Dan
Subject: RE: Variable scope
Replied by: Vance Vagell on 19-12-2005 09:35:18 PM
Hi Dan,
If the data is variable at app start (e.g. drawn from a database or configuration file), then storing it in session data will allow other elements to access it.
On the other hand, if the data is truly constant (e.g. PI = 3.14...), then there is no need to store it in the session. One solution in this case would be to create a class that stores the constants as public static member variables. Then, you could reference the constants per standard Java practice in each of your elements, for example:
MyNewClass.MY_CONSTANT_NAME
However, this approach limits the availability of these values to Java code. In particular, if your application needs to access the values via substitution, then they should be stored in session data.
Hope this helps,
Vance
Subject: RE: Variable scope
Replied by: null on 20-12-2005 03:31:18 PM
Thanks Vance. Let me see if I have it right here...
1. When I draw int values and array int values from my text config file on the first run of the app, and these values do not change for the life of this run of app, I should store these in session data for access by my other custom action elements?
2. If I have an array of constants int's the values of which never change, not even between different starts of the app, I could use public static, but it would also be better to use session data in this case?
Thanks,
Dan
Subject: RE: Variable scope
Replied by: Vance Vagell on 20-12-2005 04:06:06 PM
Hi Dan,
That is largely correct, the only clarifiation that should be made is that it's not necessarily [i:9791699b4c]better[/i:9791699b4c] to store constants in session variables, it depends on how they need to be used. If these constants are only ever used in Java code, you can just as easily store them in a class as static member variables. However, if you can imagine a situation where you would like to access these constants in your callflow via substitution (e.g. in a prompt, or in a decision), then they should be stored in session variables so that they are accessible.
If you are not sure at this point how they will be accessed later in development, it would be safe to store all of these configuration values (constants and values from a properties file) in session variables.
Regards,
Vance
Subject: RE: Variable scope
Replied by: null on 20-12-2005 04:28:18 PM
Hi Vance,
All of my variables are used in Java code only. They are internal to the workings of my custom action elements and will not be needed outside of that. This includes the ones that are initialized from the config file data.
This being the case, I will use public static for the varibles known at compile time.
What about the ones initialized from the config file, which will be used only in Java code and never change after app start? Is session data still my only option there?
Thanks,
Dan
Subject: RE: Variable scope
Replied by: Vance Vagell on 20-12-2005 04:42:45 PM
Hi Dan,
The session is the means by which data can be associated with a particular call, which is what your application needs to do. However, since the data you need to store (from config files and constants) is only ever accessed within your custom Java code, you can minimize your interaction with the session; you may wish to create a data storage class, and store only a reference to an instance of this class in the session.
In this data storage class you can define all of the static constants we previously discussed. Additionally, you can read in and store all of the configuration values. Using this method, you can store all of your data in one place, and simply store a reference to this one object in the session. You may wish to make this class a singleton, so that only one copy of it is instanced at any given time, since that is all your application needs.
In your On Call Start class, you would instantiate this object, invoke a method on it to load the configuration values, and then store a reference to the object in the session. Then, when you need access to these variables in your other code, retrieve this object's reference from the session and call the appropriate accessor methods on it.
There are other ways to accomplish your goals, but this method ensures that your constants and variables are all in one place, and minimizes the need to retrieve data from the session.
Regards,
Vance
Subject: RE: Variable scope
Replied by: null on 20-12-2005 06:01:03 PM
Vance,
I like the sound of this approach much better. Less interaction with the session and most of my data in a single place.
Thanks again Vance, and if I don't write in the meantime, enjoy the Holidays.
Dan
Subject: RE: Variable scope
Replied by: Vance Vagell on 20-12-2005 06:11:12 PM
Hi Dan,
You're welcome, and Happy Holidays to you as well!
Regards,
Vance