Author |
Message
|
jamesyu |
Posted: Mon Sep 17, 2012 10:47 am Post subject: Java instance variables(double) in JCN never got reset to 0 |
|
|
Acolyte
Joined: 31 Jan 2007 Posts: 70
|
I have a simple message flow with one JCN created like this:
public class test_JavaCompute extends MbJavaComputeNode {
private double x = 0.0;
public void evaluate(MbMessageAssembly assembly) throws MbException {
x = x + 1;
System.out.println("x = " + x);
}
}
I hope the variable "x" got reset to 0 for each input message. But in fact, it is not. The value in "x" keeps accumulating for all input messages till I stopped the broker. Why is that?
All the input messages use the same instance of JCN? |
|
Back to top |
|
 |
Vitor |
Posted: Mon Sep 17, 2012 10:57 am Post subject: Re: Java instance variables(double) in JCN never got reset t |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
jamesyu wrote: |
All the input messages use the same instance of JCN? |
See here:
Quote: |
Only one instance of the JavaCompute node is created regardless of the number of threads running against the flow (either as a result of additional instances or multiple input nodes). Therefore all of your user Java code must be threadsafe and reentrant |
A much more knowledgeable Java person will undoubtably be along in a minute. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
lancelotlinc |
Posted: Mon Sep 17, 2012 11:23 am Post subject: |
|
|
 Jedi Knight
Joined: 22 Mar 2010 Posts: 4941 Location: Bloomington, IL USA
|
Vitor is correct. Stellar quote also, Vitor. The JVM running is ambiguous to any message flows and class init is only called once no matter how many messages are processed. Therefore, at the completion of each message, if you want to reset X, set the X to null and reinit X (ie. new X) on each invocation of 'evaluate'. _________________ http://leanpub.com/IIB_Tips_and_Tricks
Save $20: Coupon Code: MQSERIES_READER |
|
Back to top |
|
 |
Vitor |
Posted: Mon Sep 17, 2012 11:51 am Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
lancelotlinc wrote: |
Vitor is correct. |
About a Java question yet.
lancelotlinc wrote: |
Stellar quote also, Vitor. |
I'm sure the IBM guy who wrote that in the link I provided will be delighted to hear it. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
rekarm01 |
Posted: Mon Sep 17, 2012 12:22 pm Post subject: Re: Java instance variables(double) in JCN never got reset t |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 1415
|
jamesyu wrote: |
I hope the variable "x" got reset to 0 for each input message. But in fact, it is not. |
In order for the class to be threadsafe and reentrant, "x" is better declared as a local variable, inside the evaluate() method, rather than as an instance variable outside the method. |
|
Back to top |
|
 |
|