ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » Java instance variables(double) in JCN never got reset to 0

Post new topic  Reply to topic
 Java instance variables(double) in JCN never got reset to 0 « View previous topic :: View next topic » 
Author Message
jamesyu
PostPosted: Mon Sep 17, 2012 10:47 am    Post subject: Java instance variables(double) in JCN never got reset to 0 Reply with quote

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
View user's profile Send private message
Vitor
PostPosted: Mon Sep 17, 2012 10:57 am    Post subject: Re: Java instance variables(double) in JCN never got reset t Reply with quote

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
View user's profile Send private message
lancelotlinc
PostPosted: Mon Sep 17, 2012 11:23 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
Vitor
PostPosted: Mon Sep 17, 2012 11:51 am    Post subject: Reply with quote

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
View user's profile Send private message
rekarm01
PostPosted: Mon Sep 17, 2012 12:22 pm    Post subject: Re: Java instance variables(double) in JCN never got reset t Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » Java instance variables(double) in JCN never got reset to 0
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.