View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.filter.keepalive;
21  
22  import org.apache.mina.core.session.IoSession;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  /**
27   * Tells {@link KeepAliveFilter} what to do when a keep-alive response message
28   * was not received within a certain timeout.
29   *
30   * @author The Apache MINA Project (dev@mina.apache.org)
31   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
32   */
33  public interface KeepAliveRequestTimeoutHandler {
34      /**
35       * Do nothing.
36       */
37      static KeepAliveRequestTimeoutHandler NOOP = new KeepAliveRequestTimeoutHandler() {
38          public void keepAliveRequestTimedOut(
39                  KeepAliveFilter filter, IoSession session) throws Exception {
40              // Do nothing.
41          }
42      };
43  
44      /**
45       * Logs a warning message, but doesn't do anything else.
46       */
47      static KeepAliveRequestTimeoutHandler LOG = new KeepAliveRequestTimeoutHandler() {
48          private final Logger log =
49              LoggerFactory.getLogger(KeepAliveFilter.class);
50  
51          public void keepAliveRequestTimedOut(
52                  KeepAliveFilter filter, IoSession session) throws Exception {
53              log.warn("A keep-alive response message was not received within " +
54                      "{} second(s).", filter.getRequestTimeout());
55          }
56      };
57  
58      /**
59       * Throws a {@link KeepAliveRequestTimeoutException}.
60       */
61      static KeepAliveRequestTimeoutHandler EXCEPTION = new KeepAliveRequestTimeoutHandler() {
62          public void keepAliveRequestTimedOut(
63                  KeepAliveFilter filter, IoSession session) throws Exception {
64              throw new KeepAliveRequestTimeoutException(
65                      "A keep-alive response message was not received within " +
66                      filter.getRequestTimeout() + " second(s).");
67          }
68      };
69  
70      /**
71       * Closes the connection after logging.
72       */
73      static KeepAliveRequestTimeoutHandler CLOSE = new KeepAliveRequestTimeoutHandler() {
74          private final Logger log =
75              LoggerFactory.getLogger(KeepAliveFilter.class);
76  
77          public void keepAliveRequestTimedOut(
78                  KeepAliveFilter filter, IoSession session) throws Exception {
79              log.warn("Closing the session because a keep-alive response " +
80                      "message was not received within {} second(s).",
81                      filter.getRequestTimeout());
82              session.close();
83          }
84      };
85  
86      /**
87       * A special handler for the 'deaf speaker' mode.
88       */
89      static KeepAliveRequestTimeoutHandler DEAF_SPEAKER = new KeepAliveRequestTimeoutHandler() {
90          public void keepAliveRequestTimedOut(
91                  KeepAliveFilter filter, IoSession session) throws Exception {
92              throw new Error("Shouldn't be invoked.  Please file a bug report.");
93          }
94      };
95  
96      /**
97       * Invoked when {@link KeepAliveFilter} couldn't receive the response for
98       * the sent keep alive message.
99       */
100     void keepAliveRequestTimedOut(KeepAliveFilter filter, IoSession session) throws Exception;
101 }