File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Identifying instances Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Identifying instances" Watch "Identifying instances" New topic
Author

Identifying instances

Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
All,

We have an interesting design problem. We have a requirement where a state of object is controlled from different components of an application. Typically created by some component and destructed by some component.
(We need to start timer in some component, stop timer in some other component , calculate time elapsed and discard that timer)

We thought of using look up tables for this. However, there won't be any primary key to identify the specific instance of timer. (Same timer need to be stopped as the one which is intended)

Is there any better solution for this? Also is there any design pattern that addresses similar problem?

TIA,
Manohar
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
How do you decide which component starts/stops which timer?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
In the rare case where everything is single threaded and symmetrical you can always guarantee timers will end in reverse order they were started and you can just use a stack of timers. I once had a PowerBuilder application where that actually worked ... for a while.

Otherwise I think the starter and stopper are going to need some common reference or key to match things up. It's nice if start() creates the id and stop() uses the same id, but it sounds like you can't pass something from the starter to the stopper. It would be easy enough to make up a string id or name for each timer, but in a multi-threaded or recursive situation that wouldn't be enough.

What all have you tried?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
Preuss/James,

Thanks for your replies.

How do you decide which component starts/stops which timer?


I can answer one half of the question, i.e which component..I know from my requirements, which component can stop timer..Regarding "Which timer", this is the place where I am facing an issue. As my application is multithreaded, I cannot guarantee that I am going to stop intended timer.

use a stack of timers


As my application is multithreaded and need not be a symetrical one, I cannot use this approach.

I am interested in knowing any desing pattern address similar problem. I believe this is structural issue. I am planning to generate a unique key by some complex alogorithm.

Any better solutions?

TIA,
Manohar
Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
Just got an idea..Can I use current threadID as a key?? Any issues in doing so? Though my application is multithreaded, it appears to me that single thread is gonna start and stop timers.

Any suggestions?

Thanks,
Manohar
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I'm sorry, I'm not sure I understand what you are trying to do.

Do you have one component that is supposed to randomly stop one timer? Or one component per timer? Or what? ?
Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
Ok..I will try to give more detailed requirement..

We need to mesure time elapsed in backend which we are querying..(through scokets..)

There is a component which writes to the socket (say SocketWriter) and there is a component which listens socket (Say SocketListener). We start timer just before writing to socket in SocketWriter and stops timer in SocketListener and time elapsed will be time difference..

However issue is this is multithreaded program and simultaneously you can n 'timers' running..So stack based approach is ruled out..Secondly to identify these timers by generating some key, issue is SocketListener has no idea what key to use.

Hope I am clear..Let me know if you need more details on requirements.

TIA,
Manohar
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Are the sending and recieving sockets in the same JVM? Presumably they are because you reckon you can access the same timer object from both.

When you talk about "some key", I'm guessing this is so that you can send the key across the socket link. Likewise, I'm guessing that you either already have or plan to have a Map of key=>timer object available to both the sender and reciever.

If this is the case, all you need is some sort of unique key. There are plenty of unique key generators available, but something as simple as:



would probably do the job.

Each time you start a new Timer, add it to the map:



When you wish to stop a timer:



Of course, if only one activity occurs on each socket at a time, you could just use the socket itself as the key.

Or have I missed the point?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
My app is multi threaded..

For instance timer1, timer2, timer 3 are started in that order and timer3 may be required to be stopped before 1&2.

And my stopping component (SocketListener) have no clue regarding which timer to stop.. (Or which key to use to get timer)..

I am thinking of using thread ID as key. (Again assuming, that same thread will try to start and stop timers)

Anything wrong in assuming so?

Regards,
Manohar
Don Morgan
Ranch Hand

Joined: Jul 24, 2003
Posts: 84
I am curious what you need the timing information for? If all you are doing is trying to measure the performance of your system or something like that, it may be much simpler to use timestamped log messages for analysis. Is there something unique in the business logic, say like an order number, which you could use to match up the requests and responses from the backend?

Not sure if you already have a db in your system, but if you do, another approach is to insert timestamped events into the database (with rows of data something like UniqueID, TimeStamp, EventID), and then you can use simple queries to get your timing information.


Don Morgan, Founder
www.DeveloperAdvantage.com - FREE Audiobooks for Software Developers
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Manohar Karamballi:
There is a component which writes to the socket (say SocketWriter) and there is a component which listens socket (Say SocketListener). We start timer just before writing to socket in SocketWriter and stops timer in SocketListener and time elapsed will be time difference..
[/QB]


OK, so the SocketWriter needs to start the timer, and the SocketListener to stop it?

Then why don't you just give them both the same timer to work on?

Pseudocode:
Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
Looks like my problem is not understood properly. Here is the code snippet where I am having issue.

public class BackendInvoker
{
public synchronized void writeToSocket(request)
{
//Write to socket logic
timer.start();
//Other business

}

public sychronized Response readFromScoket()
{
timer.stop() //Well..Issue is which timer to stop
//read from socket
}

}

Note that write and read methods are executed by different threads. write is executed when client requests. Where as read is executed from a thread which contantly polls sokcet for response.

Can anybody suggest the best way to handle this?

Also let me know if I am not clear in describing about problem?

Thanks in advance.
Manohar
Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
Also my assumption that same thread is going to start and stop timers is incorrect.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
How many BackendInvoker instances do you have? Does one instance have to handle more than one timer?
Manohar Karamballi
Ranch Hand

Joined: Jul 17, 2001
Posts: 227
We have pool of BackendInvoker instances which will be pooled. (Start size =2, incre =2 ; Max = 8). Potentially there can be 12 requests max at a time. And read thread polls for every 3 ms.

Please let me know if more information is required.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
So one BackendInvoker handles more than one request at a time? And a timer is associated with a request?

How do you know which request a response is for?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Identifying instances
 
Similar Threads
Accessing the object X by a method of object Y (Y instantiated inside X)
Correct usage of threads ?
Exception in timer : IllegalStateException
JSF : Component Binding Vs find component in the tree
ideas for a servlet design problem