I use play to develop my project and embedded netty3 as my application server
Please check the following test code:
package controllers;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.io.FileUtils;
import play.Logger;
import play.Play;
import play.mvc.Controller;
import play.mvc.results.RenderText;
public class Upload extends Controller {
private static Integer counter = 0;
private static final Integer MAX = 1;
public static void index() {
render("/upload.html");
}
public static void upload(File file) {
System.out.println("start " + Thread.currentThread());
synchronized (counter) {
System.out.println("middle " + Thread.currentThread());
if (counter > MAX) {
renderText("Sorry, the max upload thread is " + MAX);
} else {
counter++;
uploadFile(file);
counter--;
renderText("Upload success");
}
}
System.out.println("end " + Thread.currentThread());
}
static void uploadFile(File imgFile) {
File file = Play.getFile("/uploads");
try {
FileUtils.copyFileToDirectory(imgFile, file);
} catch (IOException e) {
Logger.error("upload file error", e);
}
}
}
When I opened two browsers(Firefox and Chrome) to upload the files at the same time, I debugged breakpoint in the ‘ upload(File file)’ method. But I found only 1 thread was processing.
After that, then the second request came.
The output is :
start Thread[play-thread-1,5,main]
middle Thread[play-thread-1,5,main]
start Thread[play-thread-1,5,main]
middle Thread[play-thread-1,5,main]
But in Tomcat/Jetty, there were two threads output in the console.
Did any body meet the same problem before ?
I assume that you are running in Dev mode?
The play document says that to make debugging easier, by default Play runs in a single thread model in Dev mode, and in production mode it runs as
numbers_of_cores + 1.You can override this in the application.conf.