• Post Reply Bookmark Topic Watch Topic
  • New Topic

Make a web services multiusers

 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey

I have a similar code like this (ignore sintaxis errors):



Each time 2 users call this web services, it will overwrite the files or not create them. I need each instance of the web service to be able to create them correctly.

How can I go on doing this correctly? A small pause and a



would be enough as I make a copy to a FTP file sever so where else so I have a copy. But Im not sure on how to go on with this....
 
Tim Moores
Saloon Keeper
Posts: 3263
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While it's not clear to me what the file operations are intended to do, if you don't want concurrent operations to stomp on one another you need to use file names that are unique to each WS invocation.
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's one of my main problems. They all have to be in the same place with the same name. That's why I don't mind having a small "pause" so they can get processed correctly.

If you need for information, please ask as I'm pretty much stuck and don't know where else to continue (or even look)
 
Tim Moores
Saloon Keeper
Posts: 3263
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you'll have to synchronize the file access code, maybe on some static object in the WS class.
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:Then you'll have to synchronize the file access code, maybe on some static object in the WS class.

How could I go on about doing that?

If you dont mind putting a simple example for this (those 3 files), I could problably modify that to my code.

Thank you for the help
 
Tim Moores
Saloon Keeper
Posts: 3263
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start here: http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html If you develop multi-threaded code (like web apps and web services) you need to knoe this stuff. Particularly the sections on "Synchronized Methods" and "Intrinsic Locks and Synchronization".
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:Start here: http://docs.oracle.com/javase/tutorial/essential/concurrency/index.html If you develop multi-threaded code (like web apps and web services) you need to knoe this stuff. Particularly the sections on "Synchronized Methods" and "Intrinsic Locks and Synchronization".

This is a one time thing.....I dont develop this on a daily bases and wont. Just need to know a example and from there I can (problably) figure it out and apply it to my code.
 
Tim Moores
Saloon Keeper
Posts: 3263
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then just make the methods in which file I/O is done synchronized. This will kill concurrent performance, but it will be thread-safe.
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, yes. Ive read this before........Didnt regconize the link until now.

Im just not sure how to adapt that to what I currently have

As a example. Lets say I have this:



How would I adapt that to the link you put?

Like I said, Ive read it several times but cant get the hang of it.

The web service is called (thru PHP), invokes somefunction and it passes that string to that function. After all that, PHP recieves a true or false some the function.
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:Then just make the methods in which file I/O is done synchronized. This will kill concurrent performance, but it will be thread-safe.

Performance is not really a worry right now.

How can I make the file input/output lines synchronized, with that example I put?
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Koldo Urrutia wrote:
Tim Moores wrote:Then just make the methods in which file I/O is done synchronized. This will kill concurrent performance, but it will be thread-safe.

Performance is not really a worry right now.

How can I make the file input/output lines synchronized, with that example I put?

I think a example with the code I put above (3 text files) and making them "synchronized" would not be much to ask and would help a lot..........


Thank you
 
Tim Moores
Saloon Keeper
Posts: 3263
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is dangerous to use language concepts without understanding what they do. I think it is not too much to ask of you to read up on synchronization, especially since I already posted where that information can be found; then it should be clear how to make that method synchronized.
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:It is dangerous to use language concepts without understanding what they do. I think it is not too much to ask of you to read up on synchronization, especially since I already posted where that information can be found; then it should be clear how to make that method synchronized.

Well, yeah, but I would basically being doing the same thing:



OK, thats the way the link you posted (thank you) puts it but I dont understand what it does (after reading multiple times before and again when you posted it) so it basically is the same thing.

Maybe you could give me a explaination where I would understand it better.

BTW my "somefunction" is currently:



Does THAT make a difference?
 
Tim Moores
Saloon Keeper
Posts: 3263
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the way to synchronize methods, yes. But synchronizing on "this" only works if "this" is the same for all threads, which may or may not be the case with the code you're using. To be safe you need to synchronize on something that's guaranteed to be the same for all threads, like "webservice.class".

That's what I meant by "It is dangerous to use language concepts without understanding what they do." If you're serious about developing multi-threaded code -and in this day and age of the web, who can afford not to be?- you really need to work through one of the eminent books about Java concurrency, either Java Threads by Oaks/Wong or Java Concurrency in Practice by Goetz et al.
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:That's the way to synchronize methods, yes. But synchronizing on "this" only works if "this" is the same for all threads, which may or may not be the case with the code you're using. To be safe you need to synchronize on something that's guaranteed to be the same for all threads, like "webservice.class".

That's what I meant by "It is dangerous to use language concepts without understanding what they do." If you're serious about developing multi-threaded code -and in this day and age of the web, who can afford not to be?- you really need to work through one of the eminent books about Java concurrency, either Java Threads by Oaks/Wong or Java Concurrency in Practice by Goetz et al.

I understand perfectly but like I mentioned, this is a one time thing which is not what I am oriented towards.....

Back on topic, how can I syncronize the entire class? I did not even know that was possible. As much as I like learning, Im not intrested in learning Java concurrency vs for example Cisco router, which does highly intrest me. Im not trying to be rude or anything (excuse me if I sound that way) I just want to see a example and apply it to my code. Nothing else.

"this" (Considering that code inbetween the open and close brackets) have the same file name but will NEVER have the same content. They do get deleted at the end however.


The only other way I can see how to do this (but not sure how to implant it again) is using Threads.

I MORE OR LESS understand how to do threads BUT I have 2 questions

1: How would I excute the code? Something like:



Would that be the correct way to do it? And still have my PHP code calle somefunction?

Thanks for the help



 
Tim Moores
Saloon Keeper
Posts: 3263
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you should absolutely not start a new thread, that just complicates matters without any gain. Putting "synchronized (webservice.class) {...}" around the code that is not thread-safe should be sufficient.
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:No, you should absolutely not start a new thread, that just complicates matters without any gain. Putting "synchronized (webservice.class) {...}" around the code that is not thread-safe should be sufficient.

Well Im pretty sure that by itself does not work as it is already syncronized to being with.......

The errors are mostly when I create/zip/move/delete/ftp/etc these files. The rest pretty much works good.........

Wouldnt making a thread (although complicated) allow the processes to run "separate" from each other? (Therfore avoid these types of things)

Currectly I have
The entire function that is called as a web service syncronized
I use object pools (generic ones) to create instances of I believe all the objects.

It fails like I commented on the file part.
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is only ONE thing I MAY not have done correctly: Close the generic object pools of the objects.

Should I close them, does this mean I have to "reopen them" to say it some way? I leave them because, even though a memory leak is obvious, I wanted to see if the process works............
 
Tim Moores
Saloon Keeper
Posts: 3263
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is already syncronized to being with

If you're certain about that, then what is this discussion about? If the code was synchronized properly there wouldn't be any problems with I/O, yes?

I don't know what the pool does, or how it is used, so I can't say whether it may be causing problems.

You should not create threads because the threads are created automatically by the container - the code already is multi-threaded. At least that's how every single WS implementation I've seen works (you didn't mention which one you're using).
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:
it is already syncronized to being with

If you're certain about that, then what is this discussion about? If the code was synchronized properly there wouldn't be any problems with I/O, yes?

I don't know what the pool does, or how it is used, so I can't say whether it may be causing problems.

You should not create threads because the threads are created automatically by the container - the code already is multi-threaded. At least that's how every single WS implementation I've seen works (you didn't mention which one you're using).

Using Axis2 and its SOAP implementation.

The reason Im certain is because I have it declared such as



Do I have to do something else that I am missing? Maybe in PHP?

If putting "synchronized" is all it takes, I might as well just syncronize all the methods in other classes that I have but I see some "overkill" in that.....that being said, if it resolves my problem, then OK. I mentioned that performance is not a top priority at this point in time.
 
Tim Moores
Saloon Keeper
Posts: 3263
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that's not sufficient. Using synchronized on a method causes it to synchronize on "this", which is -most likely- a different object for each invocation. So this does effectively nothing. You'd need to synchronize explicitly on an object that's identical for all threads, like the Class I mentioned before.

You don't need to do anything on the PHP side.
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:No, that's not sufficient. Using synchronized on a method causes it to synchronize on "this", which is -most likely- a different object for each invocation. So this does effectively nothing. You'd need to synchronize explicitly on an object that's identical for all threads, like the Class I mentioned before.

You don't need to do anything on the PHP side.

Then, indeed, I have to synchronize other methods......

Ill just synchronize all of them.....Let me test this out.....
 
Tim Moores
Saloon Keeper
Posts: 3263
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:Using synchronized on a method causes it to synchronize on "this", which is -most likely- a different object for each invocation. So this does effectively nothing.
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:
Tim Moores wrote:Using synchronized on a method causes it to synchronize on "this", which is -most likely- a different object for each invocation. So this does effectively nothing.

Well, it actually works now (after putting synchronized on ALL my methods/functions) but with some glitches:

1: Instead of completely what seems to be 3 calls (3 calls from a web browser to the web service), it only completed two.
2: Even though though n.txt and t.txt were generated, the contents of n.txt are those of t.txt

 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried removing synchronized on the "main" method called but nothing...........Just does one instead of 3.


Going back on a subject before, you mentioned that threads are automatically created by the container. These "threads" I by themselves cannot control them as I do not instance them (or tell them to run). Thats why I mentioned if using threads would alllow me to control this better by allowing only one thread and when that one is continue, continue to the next....

 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just saw a tutorial on YouTube and it uses not only synchronized but also uses thread.............. :S
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now tried just simply sync methods that use files which is where my main problem was...........Nothing.

I think Ive spent 1/2 month with this and still I find no solution at all.
 
Tim Moores
Saloon Keeper
Posts: 3263
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's unclear to me what your code now is or is not doing. As I've said twice by now, synchronizing methods is not sufficient. There could, of course, be bugs in the code that have nothing to do with concurrency.
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:It's unclear to me what your code now is or is not doing. As I've said twice by now, synchronizing methods is not sufficient. There could, of course, be bugs in the code that have nothing to do with concurrency.

Ill try to explain as best I can.

My entire code make calculations (things calculations as far as I see, do NOT get mixed up although I imagine that is because the entire function that the web service calls is synchronized) and based on those generates several files: 2 binary files and 1 jpg. These files are created in a structure which is

(name of folder)
bmpfile
(name of folder)
both binary files


The name of each folder autogenerates itself based on the time (with miliseconds). This structure (including the files) are zipped up in a file. This file is sent sent by email and the structure is also sent via FTP somewhere.

Between all of this is codes from creating, making the structure, moving, ziping, deleting, unzipping, moving, and deleteing again (basically, to sum it up thats what I do).

The problem lies (from what I gather from output) is the moving and deleting part, which sometimes when more than one call is made to the web service, gets mixed up. From what I see, NOTHING else gets mixed up as the binary files are different (and correct) and so are the BMPs (There are a total of 3 of that group: one on my local machine, another in a email and another FTPd).

So, I hope that helps out. Ask any questions and I will TRY to help out

I apoligize that I cannot understand correctly what synchronizing the files does but the link really explains basically nothing to me.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So since your explanations don't seem to be working for us, how about if you show us your actual code? What you originally posted as "similar" code (creating three files) is nothing like the part of your code which has the problem (moving and deleting files), from what I can see.
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:So since your explanations don't seem to be working for us, how about if you show us your actual code? What you originally posted as "similar" code (creating three files) is nothing like the part of your code which has the problem (moving and deleting files), from what I can see.

Strangly, Ive now made the made function like this



and........it seems to work :) Tried attacking with 6 requests and (although slow but like I said performance is NOT a requirement) it did them perfectly :)
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And (meaning all process complete, not each one) in 42 seconds which, all these considered, IMO is not that much........Over the internet this total process could take 2 minutes.
 
Koldo Urrutia
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, thanks for all the help but noone says anything else, I guess this is the correct way to do it.


Thanks to everyone for all their help, more so Tim Moores
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!