org.apache.kafka.clients
Class NetworkClient

java.lang.Object
  extended by org.apache.kafka.clients.NetworkClient
All Implemented Interfaces:
KafkaClient

public class NetworkClient
extends java.lang.Object
implements KafkaClient

A network client for asynchronous request/response network i/o. This is an internal class used to implement the user-facing producer and consumer clients.

This class is not thread-safe!


Constructor Summary
NetworkClient(Selectable selector, Metadata metadata, java.lang.String clientId, int maxInFlightRequestsPerConnection, long reconnectBackoffMs, int socketSendBuffer, int socketReceiveBuffer)
           
 
Method Summary
 void close()
          Close the network client
 int inFlightRequestCount()
          Get the number of in-flight requests
 boolean isReady(Node node, long now)
          Check if the node with the given id is ready to send more requests.
 Node leastLoadedNode(long now)
          Choose the node with the fewest outstanding requests which is at least eligible for connection.
 RequestHeader nextRequestHeader(ApiKeys key)
          Generate a request header for the given API key
 java.util.List<ClientResponse> poll(java.util.List<ClientRequest> requests, long timeout, long now)
          Initiate the given requests and check for any new responses, waiting up to the specified time.
 boolean ready(Node node, long now)
          Begin connecting to the given node, return true if we are already connected and ready to send to that node.
 void wakeup()
          Interrupt the client if it is blocked waiting on I/O.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NetworkClient

public NetworkClient(Selectable selector,
                     Metadata metadata,
                     java.lang.String clientId,
                     int maxInFlightRequestsPerConnection,
                     long reconnectBackoffMs,
                     int socketSendBuffer,
                     int socketReceiveBuffer)
Method Detail

ready

public boolean ready(Node node,
                     long now)
Begin connecting to the given node, return true if we are already connected and ready to send to that node.

Specified by:
ready in interface KafkaClient
Parameters:
node - The node to check
now - The current timestamp
Returns:
True if we are ready to send to the given node

isReady

public boolean isReady(Node node,
                       long now)
Check if the node with the given id is ready to send more requests.

Specified by:
isReady in interface KafkaClient
Parameters:
node - The given node id
now - The current time in ms
Returns:
true if the node is ready

poll

public java.util.List<ClientResponse> poll(java.util.List<ClientRequest> requests,
                                           long timeout,
                                           long now)
Initiate the given requests and check for any new responses, waiting up to the specified time. Requests can only be sent for ready nodes.

Specified by:
poll in interface KafkaClient
Parameters:
requests - The requests to initiate
timeout - The maximum amount of time to wait (in ms) for responses if there are none immediately
now - The current time in milliseconds
Returns:
The list of responses received

inFlightRequestCount

public int inFlightRequestCount()
Get the number of in-flight requests

Specified by:
inFlightRequestCount in interface KafkaClient

nextRequestHeader

public RequestHeader nextRequestHeader(ApiKeys key)
Generate a request header for the given API key

Specified by:
nextRequestHeader in interface KafkaClient
Parameters:
key - The api key
Returns:
A request header with the appropriate client id and correlation id

wakeup

public void wakeup()
Interrupt the client if it is blocked waiting on I/O.

Specified by:
wakeup in interface KafkaClient

close

public void close()
Close the network client

Specified by:
close in interface KafkaClient

leastLoadedNode

public Node leastLoadedNode(long now)
Choose the node with the fewest outstanding requests which is at least eligible for connection. This method will prefer a node with an existing connection, but will potentially choose a node for which we don't yet have a connection if all existing connections are in use. This method will never choose a node for which there is no existing connection and from which we have disconnected within the reconnect backoff period.

Specified by:
leastLoadedNode in interface KafkaClient
Parameters:
now - The current time in ms
Returns:
The node with the fewest in-flight requests.