スケジューラでHttpServletResponseを使用しても値が返されません。次のコードでは、コンソールに "RETURNED"が表示されます。しかし、out.println(previousLastLine)である次の行は返されません。どんな提案も感謝します。前もって感謝します。Httpサーブレットが値を返さない
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(value="/notifyController")
public class NotificationController extends HttpServlet{
int rep = 0;
String previousTime = null;
String previousLastLine = null;
String lastText = null;
int count = 0;
boolean changeFlag = false;
final long timeInterval = 1000;
HttpServletResponse response = null;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
response = resp;
if(rep == 0){
Runnable runnable = new Runnable() {
public void run() {
while (true) {
changeFlag = scheduleTask();
if(changeFlag == true){
try {
System.out.println("RETURNED");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(previousLastLine);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable);
thread.start();
rep++;
}
}
public boolean scheduleTask(){
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("D:/messagedetails.txt"));
while ((sCurrentLine = br.readLine()) != null) {
if(lastText != sCurrentLine){
lastText = sCurrentLine;
}
}
if(count == 1 && !previousLastLine.equalsIgnoreCase(lastText)){
previousLastLine = lastText;
return true;
}
previousLastLine = lastText;
count = 1;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return false;
}
}
ログに例外はありますか? – brso05
長いポーリングを実装しようとしていますか?もしそうなら、@ WebServletアノテーションにasyn属性がありません。これを確認するhttp://stackoverflow.com/questions/8081895/implementing-long-polling-in-an-asynchronous-fashion – Walfrat