This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
is it possible to create variable names at runtime, for example, I want to create an int variable with name currentSize+<aQueueName> and initialize it to 0. so the final variable must look like int currentSizeQueueName1=0;
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." <br />Linus Torvalds
Originally posted by pascal betz: no this is not possible.
It is possible to generate Java code, compile it, load it, and run it, on the fly. It might therefore be possible to do what the original poster wants.
I agree with you, though, that the original poster is very likely confused about the real problem and that a solution using less esoteric techniques, is very likely to be possible and far preferable. [ November 24, 2006: Message edited by: Peter Chase ]
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Usually when people ask questions like this, it's because they don't really understand how arrays and collections can be used. If you explain to us what you need to accomplish, we can tell you the right way to do it.
Sorry for having replied so late. The problem so far, I have several printer queues(a data structure that extends ArrayList and has other parameters like a name,the number of jobs processed by that queue and the average processing time for that queue). These printer queues are maintained in another ArrayList from which printer jobs maintained by each queue is executed by a Thread pool in a round robim fashion. Using JMX, I have provided functionalities that enable the end user to add printer queues during execution time. In the JMX console, MC4J(name of the console), I have the possibility to plot graphs based on attributes, in this case,the number of jobs processed by that queue and the average processing time for that queue. The attributes accepted by the JMX console are primitive datatypes and arrays of those. The Problem's kernel:I was thinking about having int variables, like int CurrentJobs<queueName> = <some value>; int AvgProcessingTime<queueName> = <some value>; which keep getting updated and eventually show up in the JMX console as attributes. This is good for a fixed number of queues, but my problem is when the user adds queues dynamically, which means 2 int variables for that <queueName> need to be created, which can be eventually refreshed and shown up on the console. Hope this description of my problem helps.
Why do you need a separate number for those? The queue itself should have a method for returning the number of jobs it contains for example, and the queues are all stored in a Collection.
Joined: Sep 08, 2005
thanks Jeroen, but the queue is a seperate object in itself, which extends ArrayList and also has instance variables, one of which holds the total number of jobs that were processed by the queue since startup of that application (or) since creation of that queue, although I get the number of jobs currently in the queue with the <queueName>.size() method.
First, a small rant. IMHO, I don't think it is a good idea to do it this way. It is very easy to create and destroy mbeans on the fly, so why not create them as the queues are being created. From the console, you'll see the queue as another mbean, but isn't that better? As you'll see the queues and their known attributes, instead of one queue, with a long list of weird combination attributes? End-of-rant.
Back to the question... JMX is capable of sending notifications for attribute changes for attributes that are created on the fly. Notice that the attribute name (of the notification object) is a string, that can be set at runtime.
The problem here is that you are trying to store them with variables, that you want to create dynamically -- there is absolutely no reason to do that. Instead, you can store the values in a CurrentJob and ProcessingTime hashmap, and let the keys be the queue name.
This is good for a fixed number of queues, but my problem is when the user adds queues dynamically, which means 2 int variables for that <queueName> need to be created, which can be eventually refreshed and shown up on the console.
As the earlier messages were trying to tell you, storing objects with dynamically created names is a job for collections implementing the java.util.Map Interface. There are several implementations to choose from depending on how you want the collection to be organized. If queMap is one of these collections:
String key = "some unique name"; int qvar = new int ; // your two int variables queMap.put( key, qvar );
Now you can iterate through all the keys in queMap to make the display or retrieve the int to update the variables.
I never thought about having MBeans for each printer queue created dynamically, Henry wong, thanks for the suggestion, I tried that and it worked like a charm, each of the attributes being instance variables of the printerQueue object. Thanks all for your help, I have managed to solve this problem without the use of having to create variables dynamically.