1

I am using Jsp and servlets. In one of my jsp page I will call a .sh file which will execute for 2-3 minutes and after the process is completed It will redirect to another jsp page. My problem is the user has to wait for 2-3 mins and then after the process completion only he is re-directed to the next page. I don't want this to happen I want to start that process which must run in background and I want the page to be redirected immediatly.

process.jsp page

FileWriter fileWriter = new FileWriter(file3,true);
BufferedWriter bufferFileWriter  = new BufferedWriter(fileWriter);
fileWriter.append(userid+"\t"+movie_id[i]+"\t"+ratings[i]);
fileWriter.append('\n');
bufferFileWriter.close();

Process p=Runtime.getRuntime().exec("/home/yoganandhd/project.sh");
p.waitFor();

response.sendRedirect("login.jsp");

In the above code, to execute project.sh it will take 3 mins where it will do some datamining process and then only it is getting redirected to the login.jsp page. My requirement is the user cannot wait for 3 mins, he must be redirected to the next page immediatly by running the project.sh at the background.

I am a fresher I dont know much about java can anyone say how can I acheive this? Can I acheive this using thread? If yes how? Someone please explain me with an example of same scenario.

yoganandh
  • 247
  • 1
  • 6
  • 20
  • 2
    think about using a websocket to receive notification when your thread is done. Also avoid having code in jsp, use servlets instead – Scary Wombat Jun 17 '14 at 08:22
  • Agreed @ScaryWombat. Also, this is not very usable. No-one waits 3 minutes for a page to load. Consider a progress page, with AJAX updates. – NickJ Jun 17 '14 at 08:48
  • @ScaryWombat Thank you so much I'll take your advice and will use servlets. Can you give me a small example of how to use websocket to receive. – yoganandh Jun 17 '14 at 10:44
  • @NickJ Thank you for your valuable idea. I am new to java and I am a fresher with basic knowledge in java. can you suggest or give me a example on how to use AJAX for my problem. – yoganandh Jun 17 '14 at 10:47

1 Answers1

1

You can create a new thread and execute the shellscript separately.

public class MyThread extends Thread {

   public void run(){
      Process p=Runtime.getRuntime().exec("/home/yoganandhd/project.sh");
      p.waitFor();
   }
}

Now invoke this thread from your actual flow as below, instead of invoking the job directly.

MyThread myThread = new MyThread();
myThread .start();
Dinal
  • 661
  • 4
  • 9
  • this only solves half the problem though doesn't it. – Scary Wombat Jun 17 '14 at 08:49
  • @ScaryWombat, which is the other half that this doesn't solve? This will execute the job in a separate thread whereas the main thread will redirect to login.jsp. I think this is what OP wants. – Dinal Jun 17 '14 at 08:57
  • I am reading that some datamining is needed to show some data to the user. – Scary Wombat Jun 17 '14 at 08:58
  • @ScaryWombat, Read the question properly before downvoting the answer. He is telling that he needs to redirect to login.jsp immediately without waiting. He has nowhere mentioned that he needs to show some data to the user after datamining completes. – Dinal Jun 17 '14 at 09:05
  • @Dinal, yes your rite I want to redirect it immediatly. I have tried a similar code it is redirecting to the next page but it is not executing the shell script. when I tried to execute shell script manually in shell it is working fine but using thread it is not working. – yoganandh Jun 17 '14 at 09:48
  • @user3364049, are you getting any error? Have you tried to execute the code for invoking shellscript separately, say from a main() method? – Dinal Jun 17 '14 at 10:16
  • @Dinal yes I have tried i am not getting any error it directly gets redirected to the next jsp. – yoganandh Jun 17 '14 at 10:31
  • @Dinal this is what i did <%! public class MyThread extends Thread { public void run(){ try{ Process p=Runtime.getRuntime().exec("/home/yoganandhd/project.sh"); p.waitFor(); } catch(Exception e) { e.printStackTrace(); } } } %> <% MyThread myThread = new MyThread(); myThread.start(); response.sendRedirect("login.jsp"); %> – yoganandh Jun 17 '14 at 10:32
  • @user3364049, Remove that MyThread class content from java. Instead create a new class file named MyThread.java and move that scriptlet content in there. Then import the class in your jsp using <%@ page import="package1.myClass1" %> – Dinal Jun 17 '14 at 10:40
  • @Dinal I have created a class file named MyThread.java and moved the MyThread class content in that file. then i have imported that java file in my jsp page and then in jsp page I have created object for MyThread class and executed the program even then .sh file alone is not running in background. – yoganandh Jun 17 '14 at 11:26
  • @user3364049, Couldn't help much without seeing your code. Can you try using a servlet instead of these scriptlets – Dinal Jun 17 '14 at 11:31
  • @Dinal MyThread.java: package com; public class MyThread extends Thread { public void run(){ Process p=Runtime.getRuntime().exec("/home/yoganandhd/project.sh"); p.waitFor(); } } Jsp_Page <%@pageimport="com.MyThread"%> <% MyThread myThread = new MyThread(); myThread.start(); response.sendRedirect("login.jsp"); %> – yoganandh Jun 17 '14 at 11:34
  • @Dinal Thank you so much for your support. ok I'll try with servlet – yoganandh Jun 17 '14 at 11:36
  • @user3364049, I think you are having code to execute shell command. Thats the reason why I asked whether you tried to execute that part alone from a main() method. Check this link for executing shellscript http://stackoverflow.com/questions/525212/how-to-run-unix-shell-script-from-java-code . If you really want to thank me do it with an upvote..lol – Dinal Jun 17 '14 at 11:47
  • @Dinal Yes I tried it without using thread from my jsp it worked. Moreover for now to see whether thread is working properly I have created a shell file just to create a directory. When i run it seperately it is executing but not with threads. – yoganandh Jun 17 '14 at 11:50
  • @Dinal My jsp and shell script is working good without thread. Only problem is it will take too long to execute till then the user has to wait. For that purpose i am trying to run a thread at the background. But not getting it. – yoganandh Jun 17 '14 at 11:56
  • @user3364049, If thats the case have you tried removing `p.waitFor();`. This will actually put that thread to wait until the process is over. Try out that way and see whether it works. I just noticed this now. – Dinal Jun 17 '14 at 12:03
  • @Dinal I removed p.waitFor() even then it is not working.Now i am doing this process using servlets as you said. I found that it is entering into thread but after that it is not executing the shellscript.. Is there any other way that we must run our shell script in servlet?? – yoganandh Jun 18 '14 at 12:32
  • @Dinal Thank you for Your support. The code which you have posted is working in servlets. I have done a small mistake in shell script that is the problem. Sorry.. – yoganandh Jun 19 '14 at 09:31