Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *      Copyright (C) 2012 DataStax Inc.
   *
   *   Licensed under the Apache License, Version 2.0 (the "License");
   *   you may not use this file except in compliance with the License.
   *   You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *   Unless required by applicable law or agreed to in writing, software
  *   distributed under the License is distributed on an "AS IS" BASIS,
  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *   See the License for the specific language governing permissions and
  *   limitations under the License.
  */
 package com.datastax.driver.core;
 
 import java.util.Set;
 
A Cassandra node. This class keeps the information the driver maintain on a given Cassandra node.
 
 public class Host {
 
     private final InetAddress address;
     private final HealthMonitor monitor;
 
     private volatile String datacenter;
     private volatile String rack;
 
     private volatile boolean isUp;
     private final ConvictionPolicy policy;
 
     // Tracks reconnection attempts to that host so we avoid adding multiple tasks
 
 
     // ClusterMetadata keeps one Host object per inet address, so don't use
     // that constructor unless you know what you do (use ClusterMetadata.getHost typically).
     Host(InetAddress addressConvictionPolicy.Factory policy) {
         if (address == null || policy == null)
             throw new NullPointerException();
 
         this. = address;
         this. = policy.create(this);
         this. = new HealthMonitor();
         this. = new ExecutionInfo(ImmutableList.of(this));
     }
 
     void setLocationInfo(String datacenterString rack) {
         this. = datacenter;
         this. = rack;
     }

    
Returns the node address.

Returns:
the node java.net.InetAddress.
 
     public InetAddress getAddress() {
         return ;
     }

    
Returns the name of the datacenter this host is part of. The returned datacenter name is the one as known by Cassandra. It is also possible for this information to be unavailable. In that case this method returns null, and the caller should always be aware of this possibility.

Returns:
the Cassandra datacenter name or null if datacenter is unavailable.
 
     public String getDatacenter() {
         return ;
     }

    
Returns the name of the rack this host is part of. The returned rack name is the one as known by Cassandra. It is also possible for this information to be unavailable. In that case this method returns null, and the caller should always aware of this possibility.

Returns:
the Cassandra rack name or null if the rack is unavailable
 
     public String getRack() {
         return ;
     }

    
Returns whether the host is considered up by the driver.

Please note that this is only the view of the driver may not reflect the reality. In particular a node can be down but the driver hasn't detected it yet, or he can have been restarted but the driver hasn't detected it yet (in particular, for hosts to which the driver does not connect (because the LoadBalancingPolicy.distance method says so), this information may be durably inaccurate). This information should thus only be considered as best effort and should not be relied upon too strongly.

Returns:
whether the node is considered up.
    public boolean isUp() {
        return ;
    }

    
Returns the health monitor for this host. The health monitor keeps tracks of the known host state (up or down). A class implementing Host.StateListener can also register against the health monitor to be notified when this node is detected to be up or down

Deprecated:
you are encouraged not to use the HealthMonitor anymore. To test if a node is considered UP or not, you should use isUp instead. To register a Host.StateListener, you should do so at the Cluster level through Cluster.register(com.datastax.driver.core.Host.StateListener) (registering against the HealtMonitor does not work as intented: listeners will only be informed of onUp and onDown events (not onAdd and onRemove) and you need to manually register against every host. Cluster.register(com.datastax.driver.core.Host.StateListener) solves this).
Returns:
the host Host.HealthMonitor.
    @Deprecated
    public HealthMonitor getMonitor() {
        return ;
    }
    @Override
    public final int hashCode() {
        return .hashCode();
    }
    @Override
    public final boolean equals(Object o) {
        if(!(o instanceof Host))
            return false;
        return .equals(((Host)o).);
    }
    @Override
    public String toString() {
        return .toString();
    }
    void setDown() {
         = false;
    }

    
Resets the policy, setting the host as up and informing the registered listener that the node is up.
    void setUp() {
        .reset();
         = true;
        for (Host.StateListener listener : .)
            listener.onUp(this);
    }
    boolean signalConnectionFailure(ConnectionException exception) {
        boolean isDown = .addFailure(exception);
        if (isDown) {
            // For vague compatibility sake
            for (Host.StateListener listener : .)
                listener.onDown(this);
        }
        return isDown;
    }

    
Tracks the health of a node and notify listeners when a host is considered up or down.

Deprecated:
See Host.getMonitor().
    @Deprecated
    public class HealthMonitor {
        private Set<Host.StateListenerlisteners = new CopyOnWriteArraySet<Host.StateListener>();
        HealthMonitor() {}

        
Registers the provided listener to be notified on up/down events. Registering the same listener multiple times is a no-op.

Deprecated:
See Host.getMonitor(). Replaced by Cluster.register(com.datastax.driver.core.Host.StateListener).
Parameters:
listener the new Host.StateListener to register.
        @Deprecated
        public void register(StateListener listener) {
            .add(listener);
        }

        
Unregisters a given provided listener. This method is a no-op if listener hadn't previously be registered against this monitor.

Deprecated:
See Host.getMonitor(). Replaced by Cluster.unregister(com.datastax.driver.core.Host.StateListener).
Parameters:
listener the Host.StateListener to unregister.
        @Deprecated
        public void unregister(StateListener listener) {
            .remove(listener);
        }

        
Returns whether the host is considered up by this monitor.

Deprecated:
See Host.getMonitor(). Replaced by Host.isUp.
Returns:
whether the node is considered up.
        @Deprecated
        public boolean isUp() {
            return Host.this.;
        }
    }

    
Interface for listeners that are interested in hosts added, up, down and removed events.

It is possible for the same event to be fired multiple times, particularly for up or down events. Therefore, a listener should ignore the same event if it has already been notified of a node's state.

    public interface StateListener {

        
Called when a new node is added to the cluster.

The newly added node should be considered up.

Parameters:
host the host that has been newly added.
        public void onAdd(Host host);

        
Called when a node is determined to be up.

Parameters:
host the host that has been detected up.
        public void onUp(Host host);

        
Called when a node is determined to be down.

Parameters:
host the host that has been detected down.
        public void onDown(Host host);

        
Called when a node is removed from the cluster.

Parameters:
host the removed host.
        public void onRemove(Host host);
    }
New to GrepCode? Check out our FAQ X