Java: asynchronous task

For some HTTP requests from clients, there is very complex business logic on the server side. Some of these business logics do not require an immediate customer response, such as sending email to someone. Can I put these tasks in an asynchronous method, so I only need to make sure they have been completed, I don't have to wait for all tasks to complete to respond to user requests.

Updated: some people have asked about the framework used. I am using Struts2 + Spring.

+2


a source to share


9 replies


I don't know what framework you are using, but in basic Java you can simply create a new Thread:



interface MyTaskCallback {
    void myTaskCallback();
}
class MyTask implements Runnable {  
    MyTaskCallback callback;
    Thread me;
    public MyTask(MyTaskCallback callback) {
        this.callback = callback;
        this.me = new Thread();
    }

    public void start() {
        this.me = new Thread(this);
        this.me.start();
    }

    public void stop() {
        try {
            this.me.join(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        // Calls here will not block the other threads
        sendEmailRequest();
        callback.myTaskCallback();
    }
}

class Main implements MyTaskCallback {
    public void foo() {
        MyTask m = new MyTask(this);
        m.start();
    }

    public void myTaskCallback() {
        // called when MyTask completes
    }
}

      

0


a source


You can use the following fire and forget pattern:

new Thread(new Runnable(){
    public void run(){
        System.out.println("I Am Sending Email");
        sendEmailFunction();
    }
}).start();

      



But too many of these flows will lead to trouble. If you are going to do this, you should use a ThreadPoolExecutor to make sure you have some control over the production of threads. At least put the maximum number of threads.

+2


a source


Yes. Read about concurrency .

Maybe you can, for example, set up an asynchronous producer / consumer queue.

0


a source


there is no "asynchronous method" in java, but you will be using Threads (perhaps via a framework like Quartz: http://www.quartz-scheduler.org/ ) or a message queue like JMS http://java.sun.com/ products / jms /

0


a source


You want to look at java.util.concurrent.Executors. One way to solve your problem is to have a ScheduledExecutorService that keeps a queue and runs as often. There are many different ways to offload work available in parallel utilities, however it depends on your requirements, how expensive the tasks are, how fast they need to be executed, etc.

0


a source


You must respond to all HTTP requests immediately, otherwise the client may think the server is not responding or timed out. However, you can start other threads or processes to perform tasks in the background before responding to a client request.

You can also keep submitting 100 responses until the task is complete.

0


a source


Yes, Servlet 3.0 has excellent asynchronous support.

Watch this really great resource of his, you can watch the entire cast if you're not familiar with Servlet 3.0.

Nice run in it here .

api docs .

0


a source


Spring has good support for Quartz scheduling as well as Java Threading. This link will give you a better idea of ​​it.

0


a source


Can I put these tasks in an asynchronous method so that I don't have to wait for all tasks to complete to respond to the user?

YES

-1


a source







All Articles