Design pattern to handle an asynchronous response in Java

前端 未结 5 2078
遥遥无期
遥遥无期 2021-01-30 18:51

I read answers from similar Q&A

How do you create an asynchronous HTTP request in JAVA? | Asynchronous programming design pattern |
AsyncTask Android - Design P

5条回答
  •  北恋
    北恋 (楼主)
    2021-01-30 19:00

    Essentially you need a "listener" of sorts no matter what. This is because you do not know WHEN your return message will come back, if at all (that is one of the downsides of asynchronous processing...what to do if you do not get a return message).

    So you either need to implement a listener that waits for events (ie, it is nudged by the returning message to be processed).

    Or you could do a hybrid on that by having a separate thread that "polls" (or pulls) a response area on your service to see if the return message exists.

    So it really comes down to whether you want more of a "pull" or "push" method of retrieving messages.

    The SCA (Service Component Architecture) framework might be something to consider, but depending on what you are doing, it could be overkill too. But something to consider.

    EDIT:

    I just found this in the Java SE 6 Javadocs that may be helpful. The interface CompletionService which abstracts the very thing you care about --> asynchronous work. I suggest you take a look.

提交回复
热议问题