-1

Apache's org.apache.http.entity.mime.content.FileBody is @NotThreadSafe. Can I safely pass multiple post requests having such FileBody over the network. A sample would be via org.apache.http.impl.client.DefaultHttpClient wherein the receiving servlet is a javax.servlet.http.HttpServlet? I'm wondering if the receiving servlet might mix up request contents during FileBody processing by org.apache.commons.fileupload.servlet.ServletFileUpload.

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
...//execute this multiple times in different jvm's 
MultipartEntity entity = new MultipartEntity();
... //add other entity parts here
FileBody body = new FileBody(new File(file));
entity.add("file", body);
HttpPost post = new HttpPost();
... //add other post elements here
post.setEntity(entity);
new DefaultHttpClient().execute(post);

Also, which part of FileBody can be @NotThreadSafe? I don't see any relevant static method in it.

damat-perdigannat
  • 5,780
  • 1
  • 17
  • 33
  • I think you should read http://stackoverflow.com/questions/2033879/what-does-threadsafe-mean – Sotirios Delimanolis Aug 14 '14 at 04:29
  • @SotiriosDelimanolis why do you think so? Anyway, some containers process each request on a different thread, so... – damat-perdigannat Aug 14 '14 at 05:03
  • Because you don't seem to understand what thread safety is. You should also read up about what HTTP is and how the transport layer of a network works. – Sotirios Delimanolis Aug 14 '14 at 05:05
  • @SotiriosDelimanolis what I know with thread safe is that it ideally runs safely on multithreading environments. could I be missing the definition? If the server in my question is multithreaded, will processing an HttpRequest with a NotThreadSafe component possibly run erroneously? – damat-perdigannat Aug 14 '14 at 05:08

1 Answers1

1

The concept of multithreading applies to a single process. Take your client application for example. You create a FileBody object in one thread. As a @NotThreadSafe class, you could not predict its behavior if it was passed around and used by other threads. Your server plays no part in this.

Also, which part of FileBody can be @NotThreadSafe? I don't see any relevant static method in it.

I'm not sure which version of HttpClient you are using. Version 4.3.5 of the library does not have a FileBody type annotated with @NotThreadSafe. In any case, there is no relation between thread safety and static methods. A type could be thread unsafe and not have any static methods.


FileBody is a container/descriptor for the body of an HTTP request. When the HttpClient is ready to send the request, it will get the InputStream from the FileBody and write its bytes to the underlying TCP socket/connection as the body of the HTTP request. These are just bytes. The HTTP server will receive bytes.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • I'm still quite wondering. I was thinking what about if the request was serialized. Anyway, apache-httpclient does not serialize its HTTP requests, no? – damat-perdigannat Aug 14 '14 at 05:27
  • @bimboxX What do you mean _serialize its HTTP requests_? I added an edit to explain what `FileBody` is used for and how. – Sotirios Delimanolis Aug 14 '14 at 05:30
  • What I meant was that when a client object implements Serializable or something, its states could be passed over to the other side. So, I was confirming if FileBody's bytes will not be affected during the server processing by `org.apache.commons.fileupload.servlet.ServletFileUpload`. – damat-perdigannat Aug 14 '14 at 05:40
  • @bimboxX Again, even if it was `Serializable`, which it isn't, thread safety applies to a **single** process. Your client is on one physical machine running in a process. Your server is on one physical machine running in a different process. – Sotirios Delimanolis Aug 14 '14 at 05:47