Author |
Message
|
sush1988_07 |
Posted: Fri Oct 05, 2012 11:25 am Post subject: shared Variables confusion |
|
|
Newbie
Joined: 03 Oct 2012 Posts: 5
|
Hi!! I have read quite a few times what shared variable is. But I'm not being able to relate to it with an example. Can anybody explain it to me using a small example? I just need to see a use of it to understand it. Thank you |
|
Back to top |
|
 |
mqjeff |
Posted: Fri Oct 05, 2012 11:28 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
Suppose you need to keep a count of messages processed over a given day.
You could use a shared variable to store the count, use an atomic block to ensure that it's incremented correctly across threads, and then another shared variable to store what day it is and find out if you need to reset the count when it's a new day or not. |
|
Back to top |
|
 |
lancelotlinc |
Posted: Fri Oct 05, 2012 11:30 am Post subject: |
|
|
 Jedi Knight
Joined: 22 Mar 2010 Posts: 4941 Location: Bloomington, IL USA
|
Shared variables have a long lifetime and are visible to multiple messages. They are useful for storing a message counter, for example. You cannot share variables across execution groups. It is not wise to overuse shared variables.
If you have alot of data you want to cache between messages, use a database, a Singleton, or an in-memory solution like solidDb. _________________ http://leanpub.com/IIB_Tips_and_Tricks
Save $20: Coupon Code: MQSERIES_READER |
|
Back to top |
|
 |
mqjeff |
Posted: Fri Oct 05, 2012 11:32 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
lancelotlinc wrote: |
If you have alot of data you want to cache between messages, use a database, a Singleton, or an in-memory solution like solidDb. |
Or the global cache that comes with 8.0.0.1 and later... |
|
Back to top |
|
 |
sush1988_07 |
Posted: Fri Oct 05, 2012 11:49 am Post subject: |
|
|
Newbie
Joined: 03 Oct 2012 Posts: 5
|
Thanks guys. I understand when u say it can be used to store a count but then initialized to begin a new one. But when it says it is visible to multiple messages then what use is made out of it? |
|
Back to top |
|
 |
mqjeff |
Posted: Fri Oct 05, 2012 11:53 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
sush1988_07 wrote: |
Thanks guys. I understand when u say it can be used to store a count but then initialized to begin a new one. But when it says it is visible to multiple messages then what use is made out of it? |
Each message received by an input node is processed in an independent thread.
A shared variable persists across threads. No other ESQL variables are visible in any other thread than the one processing the current message.
So if you just use a regular variable in a Compute node, and increment it each time you process a message, then the count will only ever by '1'. |
|
Back to top |
|
 |
lancelotlinc |
Posted: Fri Oct 05, 2012 11:54 am Post subject: |
|
|
 Jedi Knight
Joined: 22 Mar 2010 Posts: 4941 Location: Bloomington, IL USA
|
sush1988_07 wrote: |
it can be used to store a count |
SET I = I + 1;
sush1988_07 wrote: |
initialized to begin a new one. |
SET I = 1;
sush1988_07 wrote: |
what use is made out of it? |
SET OutputRoot.MQMD.ApplIdentityData = I; _________________ http://leanpub.com/IIB_Tips_and_Tricks
Save $20: Coupon Code: MQSERIES_READER |
|
Back to top |
|
 |
sush1988_07 |
Posted: Fri Oct 05, 2012 1:00 pm Post subject: |
|
|
Newbie
Joined: 03 Oct 2012 Posts: 5
|
ok Thanks mqjeff and lancelotlinc. one last question, how can i see a problem and say this needs a shared variable? |
|
Back to top |
|
 |
Vitor |
Posted: Fri Oct 05, 2012 1:21 pm Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
sush1988_07 wrote: |
how can i see a problem and say this needs a shared variable? |
Why a problem? You're more likely to see a design requirement similar to the scenarios described above. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
sush1988_07 |
Posted: Fri Oct 05, 2012 1:27 pm Post subject: |
|
|
Newbie
Joined: 03 Oct 2012 Posts: 5
|
hi. so the use is just for counts? |
|
Back to top |
|
 |
mqjeff |
Posted: Fri Oct 05, 2012 1:35 pm Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
The use is for any kind of data that you need to manage across instances of a single message flow.
You will identify a problem where you can use a shared variable through the combination of your intelligence and your experience. |
|
Back to top |
|
 |
sush1988_07 |
Posted: Fri Oct 05, 2012 1:40 pm Post subject: |
|
|
Newbie
Joined: 03 Oct 2012 Posts: 5
|
thanks guys!!  |
|
Back to top |
|
 |
manan.patel |
Posted: Mon Oct 08, 2012 12:39 am Post subject: |
|
|
Apprentice
Joined: 01 Oct 2012 Posts: 25
|
can anybody please post an example.... |
|
Back to top |
|
 |
mqsiuser |
Posted: Mon Oct 08, 2012 12:51 am Post subject: |
|
|
 Yatiri
Joined: 15 Apr 2008 Posts: 637 Location: Germany
|
|
Back to top |
|
 |
lancelotlinc |
Posted: Wed Oct 10, 2012 5:33 am Post subject: |
|
|
 Jedi Knight
Joined: 22 Mar 2010 Posts: 4941 Location: Bloomington, IL USA
|
manan.patel wrote: |
can anybody please post an example.... |
No. do your own research and homework. Attend the requisite training. _________________ http://leanpub.com/IIB_Tips_and_Tricks
Save $20: Coupon Code: MQSERIES_READER |
|
Back to top |
|
 |
|