tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bill Davidson <bill...@gmail.com>
Subject Re: Notification strategy for OutOfMemoryError
Date Fri, 24 Jan 2014 18:44:15 GMT
On 1/23/2014 5:21 PM, Christopher Schultz wrote:
 >If you'd care to post your code to either the list or onto the wiki, I'm
 >sure it would be useful to someone. Feel free to trim-out huge sections
 >of the code and say "make this fit your environment", etc. if you don't
 >want to show everyone how bad your email-assembling code looks ;)

Yeah, I don't really want to show my email code.  Sending email
will be left as an exercise for the reader.

The filter part of it is barely anything.  The method
handleError(Error,ServletRequest) does the work of logging and
sending email.  I synchronized that method so that only one can
run at a time in order to minimize memory usage by the filter
if I'm getting multiple Error's thrown.

ErrorNotifier.java

     /**
      * Catch all {@link Throwable}s and notify on {@link Error}s.
      *
      * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
      */
     public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain
) throws IOException, ServletException
     {
         try{
             chain.doFilter(request, response);
         }catch ( Throwable t ){
             if ( t instanceof Error ){
                 try{
                     handleError((Error)t, request);
                 }catch ( Throwable tx ){
                     m_log.fatal(tx.getMessage(), tx);
                 }
             }
             throw t;
         }
     }

     private static synchronized void handleError( Error error, ServletRequest request ) throws
UnsupportedEncodingException, MessagingException
     {
          <log and check the error and send email>
     }

There are also destroy() and init(FilterConfig) methods but they are stubs
that do nothing and are only there because they are required by the Filter
interface.

WEB-INF/web.xml:

     <!-- Filter to catch java.lang.Error and send emails -->
     <filter>
         <display-name>ErrorNotifier</display-name>
         <filter-name>ErrorNotifier</filter-name>
<filter-class>com.mydomain.filters.ErrorNotifier</filter-class>
     </filter>
     <filter-mapping>
         <filter-name>ErrorNotifier</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message