public class ZPoller extends Object implements Closeable
- the traditional one, where you make something like
ZPoller poller = ...
poller.register(socket, ZPoller.POLLIN);
poller.register(channel, ZPoller.OUT);
int events = poller.poll(-1L);
if (poller.isReadable(socket)) {
...
}
if (poller.writable(channel)) {
...
}
- the event-driven way
ZPoller poller = ...
poller.setGlobalHandler(...)
ZPoller.EventsHandler handler = ...
// The events method of the handler will be called
poller.register(channel, handler, ZPoller.IN);
// The events method of the global handler will be called
poller.register(socket, ZPoller.POLLOUT);
poller.poll(-1L);
// handlers have been called
The motivations of this rewriting are:
- the bare poller use this method
who does not allow
to choose the selector used for polling, relying on a ThreadLocal, which is dangerous.
- the bare poller use algorithms tailored for languages with manual allocation.
No need here as Java allows more flexibility. TODO There still may be a small penalty cost.
Modifier and Type | Class and Description |
---|---|
static interface |
ZPoller.EventsHandler |
static interface |
ZPoller.ItemCreator |
static interface |
ZPoller.ItemHolder |
static class |
ZPoller.ZPollItem |
Modifier and Type | Field and Description |
---|---|
static int |
ERR |
static int |
IN |
static int |
OUT |
static int |
POLLERR |
static int |
POLLIN
These values can be ORed to specify what we want to poll for.
|
static int |
POLLOUT |
Constructor and Description |
---|
ZPoller(Selector selector)
Creates a new poller with a given selector for operational polling.
|
ZPoller(ZPoller.ItemCreator creator,
Selector selector)
Creates a new poller.
|
ZPoller(ZPoller.ItemCreator creator,
ZPoller poller)
Creates a new poller based on the current one.
|
ZPoller(ZPoller poller)
Creates a new poller based on the current one.
|
Modifier and Type | Method and Description |
---|---|
protected boolean |
add(Object socketOrChannel,
ZPoller.ItemHolder holder) |
void |
close()
Destroys the poller.
|
protected ZPoller.ItemHolder |
create(SelectableChannel channel,
ZPoller.EventsHandler handler,
int events) |
protected ZPoller.ItemHolder |
create(ZMQ.Socket socket,
ZPoller.EventsHandler handler,
int events) |
protected Set<ZPoller.ItemHolder> |
createContainer(int size) |
void |
destroy()
Destroys the poller and the associated selector without exception.
|
boolean |
dispatch() |
protected boolean |
dispatch(Collection<ZPoller.ItemHolder> all,
int size)
Dispatches the polled events.
|
boolean |
error(Object socketOrChannel) |
boolean |
error(SelectableChannel channel) |
boolean |
error(ZMQ.Socket socket) |
protected PollItem |
filter(Object socketOrChannel,
int events) |
ZPoller.EventsHandler |
getGlobalHandler()
Returns the global events handler for all registered sockets.
|
boolean |
isError(SelectableChannel channel)
Tells if a channel is in error from this poller.
|
boolean |
isError(ZMQ.Socket socket)
Tells if a socket is in error from this poller.
|
boolean |
isReadable(SelectableChannel channel)
Tells if a channel is readable from this poller.
|
boolean |
isReadable(ZMQ.Socket socket)
Tells if a socket is readable from this poller.
|
boolean |
isWritable(SelectableChannel channel)
Tells if a channel is writable from this poller.
|
boolean |
isWritable(ZMQ.Socket socket)
Tells if a socket is writable from this poller.
|
protected Collection<ZPoller.ItemHolder> |
items() |
protected Iterable<ZPoller.ItemHolder> |
items(Object socketOrChannel) |
int |
poll(long timeout)
Issue a poll call, using the specified timeout value.
|
protected int |
poll(long timeout,
boolean dispatchEvents)
Issue a poll call, using the specified timeout value.
|
protected int |
poll(Selector selector,
long tout,
Collection<PollItem> items) |
boolean |
readable(Object socketOrChannel) |
boolean |
readable(SelectableChannel channel) |
boolean |
readable(ZMQ.Socket socket) |
boolean |
register(SelectableChannel channel,
int events) |
boolean |
register(SelectableChannel channel,
ZPoller.EventsHandler handler) |
boolean |
register(SelectableChannel channel,
ZPoller.EventsHandler handler,
int events)
Register a SelectableChannel for polling on specified events.
|
boolean |
register(ZMQ.Socket socket,
int events) |
boolean |
register(ZMQ.Socket socket,
ZPoller.EventsHandler handler) |
boolean |
register(ZMQ.Socket socket,
ZPoller.EventsHandler handler,
int events)
Register a Socket for polling on specified events.
|
boolean |
register(ZPoller.ItemHolder item)
Register an ItemHolder for polling on specified events.
|
void |
setGlobalHandler(ZPoller.EventsHandler globalHandler)
Sets the global events handler for all registered sockets.
|
boolean |
unregister(Object socketOrChannel)
Unregister a Socket or SelectableChannel for polling on the specified events.
|
boolean |
writable(Object socketOrChannel) |
boolean |
writable(SelectableChannel channel) |
boolean |
writable(ZMQ.Socket socket) |
public static final int POLLIN
public static final int POLLOUT
public static final int POLLERR
public static final int IN
public static final int OUT
public static final int ERR
public ZPoller(ZPoller poller)
poller
- the main poller.public ZPoller(Selector selector)
selector
- the selector to use for polling.public ZPoller(ZPoller.ItemCreator creator, ZPoller poller)
creator
- the items creatorpoller
- the main poller.public ZPoller(ZPoller.ItemCreator creator, Selector selector)
creator
- the items creatorselector
- the selector to use for polling.protected ZPoller.ItemHolder create(ZMQ.Socket socket, ZPoller.EventsHandler handler, int events)
protected ZPoller.ItemHolder create(SelectableChannel channel, ZPoller.EventsHandler handler, int events)
public void setGlobalHandler(ZPoller.EventsHandler globalHandler)
globalHandler
- the events handler to setpublic ZPoller.EventsHandler getGlobalHandler()
public final boolean register(ZMQ.Socket socket, ZPoller.EventsHandler handler, int events)
socket
- the registering socket.handler
- the events handler for this socketevents
- the events to listen to, as a mask composed by ORing POLLIN, POLLOUT and POLLERR.public final boolean register(ZMQ.Socket socket, ZPoller.EventsHandler handler)
public final boolean register(ZMQ.Socket socket, int events)
public final boolean register(SelectableChannel channel, ZPoller.EventsHandler handler, int events)
channel
- the registering channel.handler
- the events handler for this socketevents
- the events to listen to, as a mask composed by XORing POLLIN, POLLOUT and POLLERR.public final boolean register(SelectableChannel channel, ZPoller.EventsHandler handler)
public final boolean register(SelectableChannel channel, int events)
public final boolean register(ZPoller.ItemHolder item)
item
- the registering item.public final boolean unregister(Object socketOrChannel)
socketOrChannel
- the Socket or SelectableChannel to be unregisteredpublic int poll(long timeout)
Since ZeroMQ 3.0, the timeout parameter is in milliseconds, but prior to this the unit was microseconds.
timeout
- the timeout, as per zmq_poll ();
if -1, it will block indefinitely until an event
happens; if 0, it will return immediately;
otherwise, it will wait for at most that many
milliseconds/microseconds (see above).protected int poll(long timeout, boolean dispatchEvents)
timeout
- the timeout, as per zmq_poll ();dispatchEvents
- true to dispatch events using items handler and the global one.protected int poll(Selector selector, long tout, Collection<PollItem> items)
protected boolean dispatch(Collection<ZPoller.ItemHolder> all, int size)
all
- the items used for dispatchingsize
- the number of items to dispatchpublic boolean dispatch()
public boolean isReadable(SelectableChannel channel)
channel
- the channel to ask for.public boolean readable(SelectableChannel channel)
public boolean isReadable(ZMQ.Socket socket)
socket
- the socket to ask for.public boolean readable(ZMQ.Socket socket)
public boolean readable(Object socketOrChannel)
public boolean isWritable(SelectableChannel channel)
channel
- the channel to ask for.public boolean writable(SelectableChannel channel)
public boolean isWritable(ZMQ.Socket socket)
socket
- the socket to ask for.public boolean writable(ZMQ.Socket socket)
public boolean writable(Object socketOrChannel)
public boolean isError(SelectableChannel channel)
channel
- the channel to ask for.public boolean error(SelectableChannel channel)
public boolean isError(ZMQ.Socket socket)
socket
- the socket to ask for.public boolean error(ZMQ.Socket socket)
public boolean error(Object socketOrChannel)
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
public void destroy()
protected boolean add(Object socketOrChannel, ZPoller.ItemHolder holder)
protected Set<ZPoller.ItemHolder> createContainer(int size)
protected Collection<ZPoller.ItemHolder> items()
protected Iterable<ZPoller.ItemHolder> items(Object socketOrChannel)
Copyright © 2021. All rights reserved.