• Post Reply Bookmark Topic Watch Topic
  • New Topic

Handling real-time servlet data  RSS feed

 
Steve Booth
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help! I've taken a basic JAVA class and am over my head.

I've got the basic logic of taking data via a HttpServletRequest and storing it into a file. The problem I have is that there are multiple threads and my naming convention: yyyymmdd_hhmmss_ms.xml isn't robust enough. Multiple requests can and do come in at the same instant (down to the millisecond). I've thought about using a random number but the seed for it is the date/time and so I'm back to Square 1.

My guess is that there must be something in the HttpServletRequest that I can use to append to create uniqueness, but I'm stumped as to what...

Suggestions?

Thanks in advance

Steve Booth
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if you combined the timestamp with an instance variable that is accessed and incremented from a synchronized method?
 
Steve Booth
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like I've got some readin' to do...

I know one meaning for those words,

But the way you've used them... well, I don't know how to do it (yet). Any suggestions as to where I can get my education in this area enhanced would be greatly appreciated.

Thanks again,

Steve...
 
Tim Holloway
Bartender
Posts: 18663
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the case of a single servlet JVM, you can create a serial number as a synchronized class variable and increment it as needed.

In the case where you have multiple JVMs with instances (clustering, for example), you'd have to do something fancier, such as a service EJB.
 
Steve Booth
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I'm investigating as I type. The tunnel is a bit less dark... and the light... why... I don't think it's another train!

Initially we are simply looking at a single machine... clustering may be in our future, though.

There's a lot to learn.

Thanks again.

Steve...
 
Steve Booth
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got the short term answer...

Simply prefaced the doPost method with "synchronized" word.

Phase II - Clustering...

Thanks, Guys!

Steve...
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Booth:
Got the short term answer...

Simply prefaced the doPost method with "synchronized" word.

Phase II - Clustering...

Thanks, Guys!

Steve...


That will impact performance.
You were heading in a better direction when you said you have some reading to do. There are some concepts that are not extremely complicated but important, that you really need to understand to keep yourself out of trouble.
There are dozens of good books and probably a hundred good tutorials out on the web. It's well worth it to dig in and make sure you understand things before attempting any kind of real world application.

Good-Luck and welcome to Servlets.
-Ben
[ January 25, 2005: Message edited by: Ben Souther ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some other values you might try to get a unique (not random) value to append to your filename. See if any of these sound like they'd work: thread name, local "new anything[0].toString()", session id, client IP address. Some of them might even be useful to human readers.

This would be overkill unless you need it other places ... to generate unique database keys in a cluster I get a sequence number from a database for the first 10 digits and then run a static counter for another 5 digits. Every machine in the cluster gets a unique first part and maintains its own second part. The keys look like "00000-00001-00001".

Let us know what you wind up with!
 
sawan parihar
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Don't know if this is goin to help but you can use the combination of timestamp and sessionid. Else you can have a database sequence.

Hope taht helps
 
Steve Booth
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you for your suggestions and I will continue to investigate as time permits. I know that using the "synchronized" is a kludge. Phase II (probably in about a month and a half) will be a more appropriate solution. Part of dealing with a "real-world solution" is making choices. What I have so far does appear to work, but I will continue to investigate.

Thanks again!
 
Steve Booth
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have revisited this and have been able to use the Thread Name to build a unique filename. This should work for the forseeable future. We are planning on Load Balancing and Clustering the Servers so I may have to readdress this to ensure uniqueness but I'm confident on this one.

Thanks again, guys...

Steve Booth
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!