| Author |
Shared object used in all HTTP Requests: how to make it dynamic
|
Dave Anderson
Greenhorn
Joined: Nov 09, 2009
Posts: 29
|
|
Suppose I am using Apache Tomcat, and as Tomcat starts up for the first time I instantiate a java class that I will pass as a shared object for all messages that arrive. As new HTTP Requests arrive, request processing methods are passed this shared object along with the HTTP Request object, and the required work is done.
This set up works well, but I would like to change it, and I would like your advice and opinion on the wisdom of doing so.
First: the shared object (I'll call it objectX) has an array of names that are allocated for on startup (String[ ] names = new String[nnames]). If new names are to be added to the array and objectX, then Tomcat must be brought down, and when it is restarted, the existing names and all new names are read and put in the array of names to be shared.
I would like to make adding names dynamic, so that I can receive a message (an HTTP Request) that says "addThisName", and it adds it to objectX. I do not believe the existing array can be added to, since the size of the array was allocated at startup. However, it seems to me that instead of an pre-allocated array of names, I could use a Vector to hold the names instead, and new names could be added to the Vector in real time, such that all subsequent users would see the new name.
Would it be dangerous to use a Vector on such a shared object that is used during all message processing? Any tips or warnings will be greatly appreciated.
David John
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26193
|
|
Dave,
It depends on exactly how you are using it. If an object is read only, it is fine. If it is mostly read only with a few additions to the end and you don't care exactly (to the second) when the new additions are picked up, that is fine too. This sounds to me like your scenario.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Dave Anderson
Greenhorn
Joined: Nov 09, 2009
Posts: 29
|
|
Jeanne,
Thanks for the reply. This shared object is read only, but with the caveat that when a message arrives that says "addThisName" to the Vector (using the addElement() method), I want to add the name and, of course, have it available to all subsequent users as soon as possible.
It seems to me that if I use a Vector to store this list of names (instead of an array), then adding a name to the Vector using the addElement() method would make the change available almost immediately for all new messages. Right? Isn't the name added to some "shared memory" in the instance of the shared object? My fear: adding to the shared object causes some memory corruption or some kind of exception.
David
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56215
|
|
|
Vector is synchronized, and there is a risk of a ConcurrentModificationException if one thread is iterating over the Vector while another thread is modifying it. Your code will need to be able to accommodate this situation.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Charles 'King
Ranch Hand
Joined: Jul 05, 2009
Posts: 56
|
|
Another option is to employ a CopyOnWriteArrayList. I've used it under heavily load in my applications without ever receiving the death exception: ConcurrentModificationException - One advantage is you don't have to lock it during interation. Your options are endless...
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56215
|
|
Brilliant! Seems prefect for this situation.
|
 |
 |
|
|
subject: Shared object used in all HTTP Requests: how to make it dynamic
|
|
|