Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2011, Red Hat, Inc., and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.jboss.as.host.controller.mgmt;
 
 
Coordinates periodic pinging of a slave Host Controller to validate its connection

Author(s):
Brian Stansberry (c) 2011 Red Hat Inc.
 
 public class SlaveHostPinger {
 
     public static final long STD_INTERVAL;
     public static final long STD_TIMEOUT;
     public static final long SHORT_TIMEOUT = 10000;
 
     static {
         long interval = -1;
         try {
             interval = Long.parseLong(WildFlySecurityManager.getPropertyPrivileged("jboss.as.domain.ping.interval""15000"));
         } catch (Exception e) {
             // TODO log
         } finally {
             // add 500 ms to provided interval to help allow the slave pings to prevent the need for master pings
              = interval > 0 ? interval + 500 : 15500;
         }
         long timeout = -1;
         try {
             timeout = Long.parseLong(WildFlySecurityManager.getPropertyPrivileged("jboss.as.domain.ping.timeout""30000"));
         } catch (Exception e) {
             // TODO log
         } finally {
              = timeout > 0 ? timeout : 30000;
         }
     }
 
     private final String hostName;
     private final ManagementChannelHandler channelHandler;
     private final ScheduledExecutorService scheduler;
 
     private volatile Long remoteConnectionID;
     private volatile boolean cancelled;
 
     public SlaveHostPinger(String hostNameManagementChannelHandler channelHandlerScheduledExecutorService schedulerlong remoteConnectionID) {
         this. = hostName;
         this. = channelHandler;
         this. = scheduler;
         this. = remoteConnectionID;
     }
 
     public void schedulePing(long timeoutlong delay) {
         PingTask task = new PingTask(timeoutdelay);
         .schedule(taskdelay.);
     }
 
     public boolean isCancelled() {
         return ;
     }
 
     public void cancel() {
         this. = true;
     }
 
     private class PingTask implements Runnable {
 
        private final long timeout;
        private final long interval;
        private PingTask(long timeoutlong interval) {
            this. = timeout;
            this. = interval;
        }
        @Override
        public void run() {
            if (!) {
                boolean fail = false;
                AsyncFuture<Longfuture = null;
                try {
                    if ( < 1 || System.currentTimeMillis() - .getLastMessageReceivedTime() > ) {
                        future = .executeRequest(.null).getResult();
                        Long id = future.get(.);
                        if (! &&  != null && !.equals(id)) {
                            ..slaveHostControllerChanged();
                            fail = true;
                        } else {
                             = id;
                        }
                    }
                } catch (IOException e) {
                    ..debug("Caught exception sending ping request"e);
                } catch (InterruptedException e) {
                    safeCancel(future);
                    Thread.currentThread().interrupt();
                } catch (ExecutionException e) {
                    ..debug("Caught exception sending ping request"e);
                } catch (TimeoutException e) {
                    if (!) {
                        fail = true;
                        ..slaveHostControllerUnreachable();
                    }
                    safeCancel(future);
                } finally {
                    if (fail) {
                        Channel channel = null;
                        try {
                            channel = .getChannel();
                        } catch (IOException e) {
                            // ignore; shouldn't happen as the channel is already established if this task is running
                        }
                        StreamUtils.safeClose(channel);
                    } else if (! &&  > 0) {
                        .schedule(this.);
                    }
                }
            }
        }
        void safeCancel(Future<?> future) {
            if (future != null) {
                future.cancel(true);
            }
        }
    }
New to GrepCode? Check out our FAQ X