[ 
https://issues.apache.org/jira/browse/DIRMINA-92?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Trustin Lee resolved DIRMINA-92.
--------------------------------

       Resolution: Fixed
    Fix Version/s: 2.0.0-M1

This feature has been implemented finally.  Please refer to the following 
ViewVC page to browse the source code.

http://tinyurl.com/3ahxg6

I didn't write the JavaDoc and the tutorial yet (I will do soon).  Meanwhile, 
the following step-by-step  example will help you understand how to use the 
RequestResponseFilter.

0) Scenario: A client sends a MyRequest message with a certain ID, then the 
server will respond with MyResponse message with the same ID, to identify that 
the response message related to the request message with the same ID.

1) Implement ResponseInspector.

    public class MyResponseInspector implements ResponseInspector {
        public Object getRequestId(Object message) {
            if (!(message instanceof MyResponse)) {
                 return null;
            }

            MyResponse response = (MyResponse) message;
            return response.getId();
        }

        public ResponseType getResponseType(Object message) {
            // You can return PARTIAL or PARTIAL_LAST if the protocol has 
multiple responses per request.
            return ResponseType.WHOLE;
        }
    }

2) Insert the RequestResponseFilter.

    connector.getFilterChain().addLast(
            "reqres", new RequestResponseFilter(new MyResponseInspector()));

3) Implement IoHandler.

    import org.apache.mina.filter.reqres.Request;
    import org.apache.mina.filter.reqres.Response;

    public class MyHandler extends IoHandlerAdapter {
        public void sessionOpened(IoSession session) throws Exception {
            MyRequest req = ...;
            // Send the request with 5 seconds timeout.
            session.write(new Request(req.getId(), req, 5, TimeUnit.SECONDS));
        }

        public void messageReceived(IoSession session, Object message) throws 
Exception {
            Response res = (Response) message;
            MyRequest req = res.getRequest().getMessage();
            MyResponse res = res.getMessage();
            // Do something with the messages.
            ......
        }

        public void exceptionCaught(IoSession session, Throwable cause) throws 
Exception {
            if (cause instanceof RequestTimeoutException) {
                RequestTimeoutException rte = (RequestTimeoutException) cause;
                rte.getRequest();
                // Do something with the failed request.
                // RequestTimeoutException is thrown also when
                // a connection is closed but the related response is not 
received yet.
                ......
            }
        }
    }

Please give me feedback if the API doesn't make sense or needs improvement.

> Utility classes for asynchronous request-response protocols.
> ------------------------------------------------------------
>
>                 Key: DIRMINA-92
>                 URL: https://issues.apache.org/jira/browse/DIRMINA-92
>             Project: MINA
>          Issue Type: New Feature
>    Affects Versions: 0.7.0
>            Reporter: Trustin Lee
>         Assigned To: Trustin Lee
>             Fix For: 2.0.0-M1
>
>         Attachments: Protocol.zip, queryreply.zip, requestResponse.zip
>
>
> There are so many existing asynchronous protocols whose messages have 
> request-response structure.  A request message usually has a message ID, and 
> the corresponding response message, which makes a pair, contains the message 
> ID in the request message.
> It would be great if we can provide a common interface and classes to help 
> users implement this type of protocols easily.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to