views:

163

answers:

9

For some of HTTP requests from clients, there're very complex business logic in server side. Some of these business logics doesn't require to response to the client immediately, like sending a email to somebody. Can I put those tasks in an asynchronous method,so I only need to ensure that they had been executed,I don't need to wait all tasks complete to respond to the user.

Updated: Some people asked about the framework I am using. I am using Struts2 + Spring.

A: 

Yes. Read about concurrency.

You can probably set up an asynchronous producer/consumer queue, for example.

WhirlWind
A: 

I don't know what framework you're using, but, in basic Java, you can just 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
    }
}
Matt
A: 

there is no "asynchroneous method" in java, but you will either use Threads (possibly through a framework like Quartz: http://www.quartz-scheduler.org/ ) or a message queue like JMS http://java.sun.com/products/jms/

seanizer
A: 

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

Paul Sanwald
A: 

You should respond to all HTTP requests immediately, otherwise the client may think the server is not responding or timeout. However, you could start up other threads or processes to complete tasks in the background before you respond to the client.

You could also continue to send 100 responses until the task was complete.

Marcus Adams
A: 

Yes you can Servlet 3.0 has great asynchronous support.

Watch this its a really great resource, you can watch the entire cast if you are unfamiliar with Servlet 3.0.

A good run down of it here.

The api docs.

Justin
A: 

Can I put those tasks in an asynchronous method,so I don't need to wait all tasks complete to respond to the user ?

YES

OscarRyz
A: 

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 such threads will lead to trouble. If you are going to do this, then you should use a ThreadPoolExecutor to ensure that you have some control over thread production. At the very least, place a maximum on the number of threads.

Fred Haslam
A: 

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

Jaydeep