Mat
Mat

Reputation: 181

Run program on a web server using Spring Tool Suite

Is it possible to run a program on a web server using STS? I currently use the MVC Framework so I guesse I need to do this in form of a controller? Or if not, what other ways are there?

So what I'd like to know is: How to write such a controller, alternatively what other ways there are.

I run the web server on Apache Tomcat/7.0.39 and I've got Windows 7 as my current OS.

Thanks a lot!

Upvotes: 0

Views: 650

Answers (2)

Mat
Mat

Reputation: 181

I ended up writing a controller that called a class which executed a .bat file in the CMD. This did the trick for me.

The controller contains this code:

@RequestMapping(value = "/execute", method = RequestMethod.GET)
public void execute(Model model){
    CommandExecution ce = new CommandExecution("path for .bat file");
    model.addAttribute("name", ce);
}

The class CommandExecution:

public class CommandExecution {
public CommandExecution(String commandline) {
    try {
        String line;
        Process p = Runtime.getRuntime().exec(commandline);
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        while ((line = input.readLine()) != null) {
            System.out.println(line);
        }
        input.close();
    } catch (Exception err) {
        err.printStackTrace();
    }
}

//This main method is only used to be able to se if the .bat file is properly written
public static void main(String argv[]) {
   new CommandExecution("path to .bat file");
}
}

Upvotes: 0

Amit Nath
Amit Nath

Reputation: 50

You have use TaskExecutor for the same

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html

The Spring Framework provides abstractions for asynchronous execution and scheduling of tasks with the TaskExecutor and TaskScheduler interfaces.

Upvotes: 1

Related Questions