|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.kafka.common.network.Selector
public class Selector
A selector interface for doing non-blocking multi-connection network I/O.
This class works with NetworkSend
and NetworkReceive
to transmit size-delimited network requests and
responses.
A connection can be added to the selector associated with an integer id by doing
selector.connect(42, new InetSocketAddress("google.com", server.port), 64000, 64000);The connect call does not block on the creation of the TCP connection, so the connect method only begins initiating the connection. The successful invocation of this method does not mean a valid connection has been established. Sending requests, receiving responses, processing connection completions, and disconnections on the existing connections are all done using the
poll()
call.
List<NetworkRequest> requestsToSend = Arrays.asList(new NetworkRequest(0, myBytes), new NetworkRequest(1, myOtherBytes)); selector.poll(TIMEOUT_MS, requestsToSend);The selector maintains several lists that are reset by each call to
poll()
which are available via
various getters. These are reset by each call to poll()
.
This class is not thread safe!
Constructor Summary | |
---|---|
Selector(Metrics metrics,
Time time)
Create a new selector |
Method Summary | |
---|---|
void |
close()
Close this selector and all associated connections |
java.util.List<NetworkReceive> |
completedReceives()
The list of receives that completed on the last poll() call. |
java.util.List<NetworkSend> |
completedSends()
The list of sends that completed on the last poll() call. |
void |
connect(int id,
java.net.InetSocketAddress address,
int sendBufferSize,
int receiveBufferSize)
Begin connecting to the given address and add the connection to this selector associated with the given id number. |
java.util.List<java.lang.Integer> |
connected()
The list of connections that completed their connection on the last poll()
call. |
void |
disconnect(int id)
Disconnect any connections for the given id (if there are any). |
java.util.List<java.lang.Integer> |
disconnected()
The list of connections that finished disconnecting on the last poll()
call. |
void |
poll(long timeout,
java.util.List<NetworkSend> sends)
Do whatever I/O can be done on each connection without blocking. |
void |
wakeup()
Interrupt the selector if it is blocked waiting to do I/O. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Selector(Metrics metrics, Time time)
Method Detail |
---|
public void connect(int id, java.net.InetSocketAddress address, int sendBufferSize, int receiveBufferSize) throws java.io.IOException
Note that this call only initiates the connection, which will be completed on a future poll(long, List)
call. Check connected()
to see which (if any) connections have completed after a given poll call.
connect
in interface Selectable
id
- The id for the new connectionaddress
- The address to connect tosendBufferSize
- The send buffer for the new connectionreceiveBufferSize
- The receive buffer for the new connection
java.lang.IllegalStateException
- if there is already a connection for that id
java.io.IOException
- if DNS resolution fails on the hostname or if the broker is downpublic void disconnect(int id)
poll()
call.
disconnect
in interface Selectable
public void wakeup()
wakeup
in interface Selectable
public void close()
close
in interface Selectable
public void poll(long timeout, java.util.List<NetworkSend> sends) throws java.io.IOException
The provided network sends will be started.
When this call is completed the user can check for completed sends, receives, connections or disconnects using
completedSends()
, completedReceives()
, connected()
, disconnected()
. These
lists will be cleared at the beginning of each poll(long, List)
call and repopulated by the call if any
completed I/O.
poll
in interface Selectable
timeout
- The amount of time to wait, in milliseconds. If negative, wait indefinitely.sends
- The list of new sends to begin
java.lang.IllegalStateException
- If a send is given for which we have no existing connection or for which there is
already an in-progress send
java.io.IOException
public java.util.List<NetworkSend> completedSends()
Selectable
poll()
call.
completedSends
in interface Selectable
public java.util.List<NetworkReceive> completedReceives()
Selectable
poll()
call.
completedReceives
in interface Selectable
public java.util.List<java.lang.Integer> disconnected()
Selectable
poll()
call.
disconnected
in interface Selectable
public java.util.List<java.lang.Integer> connected()
Selectable
poll()
call.
connected
in interface Selectable
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |