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