Hey

I want to make a multithreaded and multiuser web service in Java. Where do I even start looking?

Currently I have a web service that is one user and one thread. I think Ill problably have to completely rewrite it from almost scratch so....

Where can I see some ways how to do it?

Thanks!

Recommended Answers

All 12 Replies

Not enough details. When you say web service, what does your current service do? "Official" web services fall under two categories: SOAP and REST with SOAP IMO being the more complicated one. These typically run in a web container which automatically helps you take advantage of the servlet specification and implementation (which is also used by web applications). As a starting point look at Resteasy and Restlet frameworks.

Not enough details. When you say web service, what does your current service do? "Official" web services fall under two categories: SOAP and REST with SOAP IMO being the more complicated one. These typically run in a web container which automatically helps you take advantage of the servlet specification and implementation (which is also used by web applications). As a starting point look at Resteasy and Restlet frameworks.

OK, great, Ill give you some details of what my web service does and I hope it helps...

It is not a servlet. It uses the Axis2 framework.

First off, this is based of a existing C++ application. Its basically a straight port from a C++ application to a Java web service (only thing changing is synaxis). No GUI, just internal programming (2+2, nothing like drawing the GUI was ported)

There are a lot of math calculations. Most of the program is just based on that.

It access a MySQL server (for read/write). This point I believe will bring several problems to multiuser. Also does FTP transfers (it only sends files, never recieves).

The sum up of the program is basically this:

1: When the web service is called, it checks to verify that certain parameters have been sent correctly.
2: Once thats done it gets a parameter that was sent called ID to read certain fields in the data base that match that ID.
3: With the parameters that data from the data base, it does all its math calculations. I would say that this is more than 50% of the web services. The calculations are split into several files in a different package. Example in the package "math" we have "add.java" and "multiply.java" and similar...
4: After these calculations, we draw a picture and save it as a BMP. Again, in a package called "image" with things like "calculatebmp.java" and "drawbmp.java"
5: Further calculations (with read/write to database)
6: I send a email with binary files based on those calculations
7: I also send these files to a FTP server
8: I return "TRUE" if everything has gone good or "FALSE" with some debug info if something went wrong.

Thats basically the sum of my web service. The end user NEVER sees anything of this. I see debug lines running in a console, but thats it.

if you need further information or details, please ask and I will try to give them.

Thank you for your reply

It is not a servlet. It uses the Axis2 framework

AFAIK, Axis2 uses servlet specification under the hood for serving the content i.e. it runs on a servlet container like Tomcat, Jetty etc. Aren't you using one of these for hosting your web service?

Come to think of it, what makes you say that the current service is one thread / one user? As always, you just have to make sure you are properly handling the shared resources (databases, file systems etc.) and the concurrency can be easily taken care by the framework.

AFAIK, Axis2 uses servlet specification under the hood for serving the content i.e. it runs on a servlet container like Tomcat, Jetty etc. Aren't you using one of these for hosting your web service? Come to think of it, what makes you say that the current service is one thread / one user? As always, you just have to make sure you are properly handling the shared resources (databases, file systems etc.) and the concurrency can be easily taken care by the framework.

Yes, its a servlet like you mentioned (Tomcat). I apoligize for the misunderstanding.

Well it makes me say this because Ive ran it with 2 users and it errors out :P I believe the main reason is like you mentioned: shared resources, more so the databases. So Im not sure how I would have to implement this with databases.

Maybe with some simple examples with databases I might be able to implement it but Im not so sure where to even start.

And like I mentioned, I also deal with file systems (writing/reading/etc files/folders) so yes, those are 2 shared resources I use which may cause problems.

Without specifics, it's kind of hard to suggest what you should try out. As already mentioned, web services implemented on the Java platform are by default multithread ready. You should try to investigate the errors you get and dig out the underlying problem.

And like I mentioned, I also deal with file systems (writing/reading/etc files/folders) so yes, those are 2 shared resources I use which may cause problems.

Just to be clear here, using the FileSystem doesn't cause any problems. As long as the different threads are writing to different files, the OS takes care of the concurrency. The problem comes when two or more users try to access the same resource. If your code is simply writing to a file which has a unique name for each user request, you should be good to go.

Without specifics, it's kind of hard to suggest what you should try out. As already mentioned, web services implemented on the Java platform are by default multithread ready. You should try to investigate the errors you get and dig out the underlying problem. And like I mentioned, I also deal with file systems (writing/reading/etc files/folders) so yes, those are 2 shared resources I use which may cause problems. Just to be clear here, using the FileSystem doesn't cause any problems. As long as the different threads are writing to different files, the OS takes care of the concurrency. The problem comes when two or more users try to access the same resource. If your code is simply writing to a file which has a unique name for each user request, you should be good to go.

" The problem comes when two or more users try to access the same resource."

Exactly. My code tries to access the database continually which is were the main problem is.

When interacting with the file system, all names are unique. They are generated via timestamp so there isnt any problem. I DO though use a temp file called temp.txt (literally) which is sometimes when I try to access it with more than one user at a time, it spits out a error because it cant be found.

The routine of that file is is create it, write to it, read from it, and delete it.

Well I already tried to specify what my web service does :) What other information do you need? Ill try to give it to you.

I DO though use a temp file called temp.txt (literally) which is sometimes when I try to access it with more than one user at a time, it spits out a error because it cant be found.

That's not good; using a common file name which can be used by multiple threads is not a good idea. The java.io.File class has a method for creating a random named temporary file. Use that if all you want to do is access the file just for a single request.

Exactly. My code tries to access the database continually which is were the main problem is.

Accessing the database continuously isn't the problem. If you have used the stock JDBC code, it should just work. Make sure you use transactions if you want a certain sequence of steps to be atomic. Reading through the JDBC official tutorials should be a good start in case you don't know about them.

What other information do you need? Ill try to give it to you.

I don't need any other information. Unless you post specific exception stack traces of specific parts of the code which you are stuck with, there isn't much I can help with.

That's not good; using a common file name which can be used by multiple threads is not a good idea. The java.io.File class has a method for creating a random named temporary file. Use that if all you want to do is access the file just for a single request.

The problem is that I need to know that file name to later discard it.

"Later" as in "later in the same run of the program" or "when the program is run again some time later"?

The problem is that I need to know that file name to later discard it.

Two solutions:

  • If you really need to discard it as soon as the operation/request is done, make sure you pass around the file name "across" the layers of your code (probably wrapped up in some request object). Plus, temporary file is just like any other file; it has a name and other attributes.
  • If the files are small and if you can affrod to cleanup all the files later on i.e. after the JVM/server terminates gracefully, invoke the deleteOnExit method on that file object which will ensure that the files are physically deleted when the JVM exits. Another alternative would be to cleanup the temp area by a batch job/script when the server is restarted.

Later in the same run of the program

Im going to try to read a bit more of the code to say what I do with that temp file and try to describe it better.

In that case all you need is to keep a File variable that's set to the temporary file, then you can use that to delete it afterwards.
edit: that's probably one File variable per Thread or per User.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.