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   */
32  public interface KeepAliveRequestTimeoutHandler {
33      /**
34       * Do nothing.
35       */
36      static KeepAliveRequestTimeoutHandler NOOP = new KeepAliveRequestTimeoutHandler() {
37          public void keepAliveRequestTimedOut(
38                  KeepAliveFilter filter, IoSession session) throws Exception {
39              // Do nothing.
40          }
41      };
42  
43      /**
44       * Logs a warning message, but doesn't do anything else.
45       */
46      static KeepAliveRequestTimeoutHandler LOG = new KeepAliveRequestTimeoutHandler() {
47          private final Logger LOGGER =
48              LoggerFactory.getLogger(KeepAliveFilter.class);
49  
50          public void keepAliveRequestTimedOut(
51                  KeepAliveFilter filter, IoSession session) throws Exception {
52              LOGGER.warn("A keep-alive response message was not received within " +
53                      "{} second(s).", filter.getRequestTimeout());
54          }
55      };
56  
57      /**
58       * Throws a {@link KeepAliveRequestTimeoutException}.
59       */
60      static KeepAliveRequestTimeoutHandler EXCEPTION = new KeepAliveRequestTimeoutHandler() {
61          public void keepAliveRequestTimedOut(
62                  KeepAliveFilter filter, IoSession session) throws Exception {
63              throw new KeepAliveRequestTimeoutException(
64                      "A keep-alive response message was not received within " +
65                      filter.getRequestTimeout() + " second(s).");
66          }
67      };
68  
69      /**
70       * Closes the connection after logging.
71       */
72      static KeepAliveRequestTimeoutHandler CLOSE = new KeepAliveRequestTimeoutHandler() {
73          private final Logger LOGGER =
74              LoggerFactory.getLogger(KeepAliveFilter.class);
75  
76          public void keepAliveRequestTimedOut(
77                  KeepAliveFilter filter, IoSession session) throws Exception {
78              LOGGER.warn("Closing the session because a keep-alive response " +
79                      "message was not received within {} second(s).",
80                      filter.getRequestTimeout());
81              session.close(true);
82          }
83      };
84  
85      /**
86       * A special handler for the 'deaf speaker' mode.
87       */
88      static KeepAliveRequestTimeoutHandler DEAF_SPEAKER = new KeepAliveRequestTimeoutHandler() {
89          public void keepAliveRequestTimedOut(
90                  KeepAliveFilter filter, IoSession session) throws Exception {
91              throw new Error("Shouldn't be invoked.  Please file a bug report.");
92          }
93      };
94  
95      /**
96       * Invoked when {@link KeepAliveFilter} couldn't receive the response for
97       * the sent keep alive message.
98       */
99      void keepAliveRequestTimedOut(KeepAliveFilter filter, IoSession session) throws Exception;
100 }