public class ZPoller
extends java.lang.Object
implements java.io.Closeable
This poller can be used in two ways:
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)) {
...
}
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
ZMQ.poll(Selector, PollItem[], int, long). This method did not allow
to choose the selector used for polling, relying on a ThreadLocal, which is dangerous.
It’s thread safe, but newly registered items will only be handled at the next poll iteration,
so if poller.poll(-1L) is used, adding items will have no effect. The situation is the same with the global handler
| Modifier and Type | Class and Description |
|---|---|
static class |
ZPoller.ComposeEventsHandler |
private class |
ZPoller.CompositePollItem |
static interface |
ZPoller.EventsHandler |
static interface |
ZPoller.ItemCreator |
static interface |
ZPoller.ItemHolder |
static class |
ZPoller.SimpleCreator |
static class |
ZPoller.ZPollItem |
| Modifier and Type | Field and Description |
|---|---|
private ZPoller.ItemCreator |
creator |
private java.lang.ThreadLocal<ZPoller.EventsHandler> |
currentGlobalHandler |
static int |
ERR |
private java.util.concurrent.atomic.AtomicReference<ZPoller.EventsHandler> |
globalHandler |
static int |
IN |
private java.util.Map<java.lang.Object,ZPoller.CompositePollItem> |
items |
static int |
OUT |
static int |
POLLERR |
static int |
POLLIN |
static int |
POLLOUT |
static int |
READABLE |
private java.nio.channels.Selector |
selector |
static int |
WRITABLE |
| Modifier | Constructor and Description |
|---|---|
|
ZPoller(java.nio.channels.Selector selector)
Creates a new poller with a given selector for operational polling.
|
|
ZPoller(ZContext context)
Creates a new poller attached to a given context that will provide
selector for operational polling.
|
private |
ZPoller(ZPoller.ItemCreator creator,
java.nio.channels.Selector selector)
Creates a new poller.
|
|
ZPoller(ZPoller.ItemCreator creator,
ZContext context)
Creates a new poller attached to a given context that will provide
selector for operational polling.
|
|
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(java.lang.Object socketOrChannel,
ZPoller.ItemHolder holder) |
void |
close()
Destroys the poller.
|
private java.lang.Object |
computeSocketOrChannel(ZPoller.ItemHolder holder) |
protected ZPoller.ItemHolder |
create(java.nio.channels.SelectableChannel channel,
ZPoller.EventsHandler handler,
int events) |
protected ZPoller.ItemHolder |
create(ZMQ.Socket socket,
ZPoller.EventsHandler handler,
int events) |
protected java.util.Set<ZPoller.ItemHolder> |
createContainer(int size)
Deprecated.
useless
|
void |
destroy()
Destroys the poller without exception.
|
boolean |
dispatch()
dispatches all the polled events to their respective handlers
|
protected boolean |
dispatch(java.util.Collection<? extends ZPoller.ItemHolder> allDispatched)
Dispatches the polled events.
|
protected boolean |
dispatch(java.util.Collection<? extends ZPoller.ItemHolder> all,
int size)
Deprecated.
Uses
dispatch(Collection) instead, as size is unused |
private boolean |
dispatch(java.util.Set<ZPoller.CompositePollItem> allPolled) |
boolean |
error(java.lang.Object socketOrChannel) |
boolean |
error(java.nio.channels.SelectableChannel channel) |
boolean |
error(ZMQ.Socket socket) |
protected PollItem |
filter(java.lang.Object socketOrChannel,
int events) |
ZPoller.EventsHandler |
getGlobalHandler()
Returns the global events handler for all registered sockets.
|
boolean |
isError(java.nio.channels.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(java.nio.channels.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(java.nio.channels.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 java.util.Collection<ZPoller.ItemHolder> |
items()
Return a copy of all the items of this poller.
|
protected java.lang.Iterable<ZPoller.ItemHolder> |
items(java.lang.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(java.nio.channels.Selector selector,
long tout,
java.util.Collection<PollItem> items) |
boolean |
pollerr(java.nio.channels.SelectableChannel channel) |
boolean |
pollerr(ZMQ.Socket socket) |
boolean |
pollin(java.nio.channels.SelectableChannel channel) |
boolean |
pollin(ZMQ.Socket socket) |
boolean |
pollout(java.nio.channels.SelectableChannel channel) |
boolean |
pollout(ZMQ.Socket socket) |
boolean |
readable(java.lang.Object socketOrChannel) |
boolean |
readable(java.nio.channels.SelectableChannel channel) |
boolean |
readable(ZMQ.Socket socket) |
boolean |
register(java.nio.channels.SelectableChannel channel,
BiFunction<java.nio.channels.SelectableChannel,java.lang.Integer,java.lang.Boolean> handler,
int events)
Registers a SelectableChannel for polling on specified events.
|
boolean |
register(java.nio.channels.SelectableChannel channel,
int events)
Registers a SelectableChannel for polling on specified events.
|
boolean |
register(java.nio.channels.SelectableChannel channel,
ZPoller.EventsHandler handler)
Registers a SelectableChannel for polling on all events.
|
boolean |
register(java.nio.channels.SelectableChannel channel,
ZPoller.EventsHandler handler,
int events)
Registers a SelectableChannel for polling on specified events.
|
boolean |
register(ZMQ.Socket socket,
BiFunction<ZMQ.Socket,java.lang.Integer,java.lang.Boolean> handler,
int events)
Register a Socket for polling on specified events.
|
boolean |
register(ZMQ.Socket socket,
int events)
Register a Socket for polling on specified events, using the global
ZPoller.EventsHandler. |
boolean |
register(ZMQ.Socket socket,
ZPoller.EventsHandler handler)
Register a Socket for polling on all events.
|
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.
|
int |
registered() |
void |
setGlobalHandler(ZPoller.EventsHandler globalHandler)
Sets the global events handler for all registered sockets.
|
private boolean |
subDispatch(java.util.Collection<? extends ZPoller.ItemHolder> allDispatched) |
boolean |
unregister(java.lang.Object socketOrChannel)
Unregister a Socket or SelectableChannel for polling on the specified events.
|
boolean |
writable(java.lang.Object socketOrChannel) |
boolean |
writable(java.nio.channels.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 static final int READABLE
public static final int WRITABLE
private final java.nio.channels.Selector selector
private final ZPoller.ItemCreator creator
private final java.util.Map<java.lang.Object,ZPoller.CompositePollItem> items
private final java.util.concurrent.atomic.AtomicReference<ZPoller.EventsHandler> globalHandler
private final java.lang.ThreadLocal<ZPoller.EventsHandler> currentGlobalHandler
public ZPoller(ZPoller poller)
poller - the main poller.public ZPoller(java.nio.channels.Selector selector)
selector - the selector to use for polling.public ZPoller(ZContext context)
context - the context that will provide 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, ZContext context)
creator - the items creatorcontext - the context that will provide the selector to use for polling.private ZPoller(ZPoller.ItemCreator creator, java.nio.channels.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(java.nio.channels.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, BiFunction<ZMQ.Socket,java.lang.Integer,java.lang.Boolean> 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, 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)
socket - the registering socket.handler - the events handler for this socketpublic final boolean register(ZMQ.Socket socket, int events)
ZPoller.EventsHandler.socket - the registering socket.events - the events to listen to, as a mask composed by ORing POLLIN, POLLOUT and POLLERR.public final boolean register(java.nio.channels.SelectableChannel channel,
BiFunction<java.nio.channels.SelectableChannel,java.lang.Integer,java.lang.Boolean> handler,
int events)
channel - the registering channel.handler - the events handler for this channelevents - the events to listen to, as a mask composed by ORing POLLIN, POLLOUT and POLLERR.public final boolean register(java.nio.channels.SelectableChannel channel,
ZPoller.EventsHandler handler,
int events)
channel - the registering channel.handler - the events handler for this channelevents - the events to listen to, as a mask composed by ORing POLLIN, POLLOUT and POLLERR.public final boolean register(java.nio.channels.SelectableChannel channel,
ZPoller.EventsHandler handler)
channel - the registering channel.handler - the events handler for this channelpublic final boolean register(java.nio.channels.SelectableChannel channel,
int events)
channel - the registering channel.events - the events to listen to, as a mask composed by ORing POLLIN, POLLOUT and POLLERR.public int registered()
public final boolean register(ZPoller.ItemHolder item)
item - the registering item.public final boolean unregister(java.lang.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.
If timeout is -1 and there is no object registered, it will return immediately
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)
If timeout is -1 and there is no object registered, it will return immediately
timeout - the timeout, as per zmq_poll ();dispatchEvents - true to dispatch events using items handler and the global one.protected int poll(java.nio.channels.Selector selector,
long tout,
java.util.Collection<PollItem> items)
private boolean dispatch(java.util.Set<ZPoller.CompositePollItem> allPolled)
@Deprecated protected boolean dispatch(java.util.Collection<? extends ZPoller.ItemHolder> all, int size)
dispatch(Collection) instead, as size is unusedall - the items used for dispatchingsize - Unusedprotected boolean dispatch(java.util.Collection<? extends ZPoller.ItemHolder> allDispatched)
allDispatched - the items used for dispatchingpublic boolean dispatch()
private boolean subDispatch(java.util.Collection<? extends ZPoller.ItemHolder> allDispatched)
public boolean isReadable(java.nio.channels.SelectableChannel channel)
channel - the channel to ask for.public boolean readable(java.nio.channels.SelectableChannel channel)
public boolean isReadable(ZMQ.Socket socket)
socket - the socket to ask for.public boolean readable(ZMQ.Socket socket)
public boolean readable(java.lang.Object socketOrChannel)
public boolean pollin(ZMQ.Socket socket)
public boolean pollin(java.nio.channels.SelectableChannel channel)
public boolean isWritable(java.nio.channels.SelectableChannel channel)
channel - the channel to ask for.public boolean writable(java.nio.channels.SelectableChannel channel)
public boolean isWritable(ZMQ.Socket socket)
socket - the socket to ask for.public boolean writable(ZMQ.Socket socket)
public boolean writable(java.lang.Object socketOrChannel)
public boolean pollout(ZMQ.Socket socket)
public boolean pollout(java.nio.channels.SelectableChannel channel)
public boolean isError(java.nio.channels.SelectableChannel channel)
channel - the channel to ask for.public boolean error(java.nio.channels.SelectableChannel channel)
public boolean isError(ZMQ.Socket socket)
socket - the socket to ask for.public boolean error(ZMQ.Socket socket)
public boolean error(java.lang.Object socketOrChannel)
public boolean pollerr(ZMQ.Socket socket)
public boolean pollerr(java.nio.channels.SelectableChannel channel)
public void close()
close in interface java.io.Closeableclose in interface java.lang.AutoCloseablepublic void destroy()
protected boolean add(java.lang.Object socketOrChannel,
ZPoller.ItemHolder holder)
private java.lang.Object computeSocketOrChannel(ZPoller.ItemHolder holder)
@Deprecated protected java.util.Set<ZPoller.ItemHolder> createContainer(int size)
size - the initial capacity of the hash tableprotected java.util.Collection<ZPoller.ItemHolder> items()
protected java.lang.Iterable<ZPoller.ItemHolder> items(java.lang.Object socketOrChannel)
protected PollItem filter(java.lang.Object socketOrChannel, int events)