Class ZPoller
java.lang.Object
org.zeromq.ZPoller
- All Implemented Interfaces:
Closeable, AutoCloseable
Rewritten poller for ØMQ.
Polls selectable channels and sockets for specified events.
This poller can be used in two ways:
- 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 bare poller used
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. - 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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classprivate static classstatic interfacestatic interfacestatic interfacestatic classstatic class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Set<ZPoller.CompositePollItem> private final ZPoller.ItemCreatorstatic final intprivate ZPoller.EventsHandlerstatic final intprivate final Map<Object, ZPoller.CompositePollItem> static final intstatic final intstatic final intstatic final intstatic final intprivate final Selectorstatic final int -
Constructor Summary
ConstructorsModifierConstructorDescriptionCreates a new poller with a given selector for operational polling.Creates a new poller attached to a given context that will provide selector for operational polling.Creates a new poller based on the current one.ZPoller(ZPoller.ItemCreator creator, 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.privateZPoller(ZPoller.ItemCreator creator, ZContext context, Selector selector) Creates a new poller.ZPoller(ZPoller.ItemCreator creator, ZPoller poller) Creates a new poller based on the current one. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanadd(Object socketOrChannel, ZPoller.ItemHolder holder) voidclose()Destroys the poller.protected ZPoller.ItemHoldercreate(SelectableChannel channel, ZPoller.EventsHandler handler, int events) protected ZPoller.ItemHoldercreate(ZMQ.Socket socket, ZPoller.EventsHandler handler, int events) protected Set<ZPoller.ItemHolder> createContainer(int size) Deprecated.voiddestroy()Destroys the poller without exception.booleandispatch()protected booleandispatch(Collection<? extends ZPoller.ItemHolder> all, int size) Dispatches the polled events.private booleandispatch(Set<ZPoller.CompositePollItem> all, int size) booleanbooleanerror(SelectableChannel channel) booleanerror(ZMQ.Socket socket) protected PollItemReturns the global events handler for all registered sockets.booleanisError(SelectableChannel channel) Tells if a channel is in error from this poller.booleanisError(ZMQ.Socket socket) Tells if a socket is in error from this poller.booleanisReadable(SelectableChannel channel) Tells if a channel is readable from this poller.booleanisReadable(ZMQ.Socket socket) Tells if a socket is readable from this poller.booleanisWritable(SelectableChannel channel) Tells if a channel is writable from this poller.booleanisWritable(ZMQ.Socket socket) Tells if a socket is writable from this poller.protected Collection<? extends ZPoller.ItemHolder> items()protected Iterable<ZPoller.ItemHolder> intpoll(long timeout) Issue a poll call, using the specified timeout value.protected intpoll(long timeout, boolean dispatchEvents) Issue a poll call, using the specified timeout value.protected intpoll(Selector selector, long tout, Collection<PollItem> items) booleanpollerr(SelectableChannel channel) booleanpollerr(ZMQ.Socket socket) booleanpollin(SelectableChannel channel) booleanpollin(ZMQ.Socket socket) booleanpollout(SelectableChannel channel) booleanpollout(ZMQ.Socket socket) booleanbooleanreadable(SelectableChannel channel) booleanreadable(ZMQ.Socket socket) final booleanregister(SelectableChannel channel, int events) Registers a SelectableChannel for polling on specified events.final booleanregister(SelectableChannel channel, ZPoller.EventsHandler handler) Registers a SelectableChannel for polling on all events.final booleanregister(SelectableChannel channel, ZPoller.EventsHandler handler, int events) Registers a SelectableChannel for polling on specified events.final booleanregister(SelectableChannel channel, BiFunction<SelectableChannel, Integer, Boolean> handler, int events) Registers a SelectableChannel for polling on specified events.final booleanregister(ZMQ.Socket socket, int events) final booleanregister(ZMQ.Socket socket, ZPoller.EventsHandler handler) final booleanregister(ZMQ.Socket socket, ZPoller.EventsHandler handler, int events) Register a Socket for polling on specified events.final booleanregister(ZMQ.Socket socket, BiFunction<ZMQ.Socket, Integer, Boolean> handler, int events) Register a Socket for polling on specified events.final booleanregister(ZPoller.ItemHolder item) Register an ItemHolder for polling on specified events.voidsetGlobalHandler(ZPoller.EventsHandler globalHandler) Sets the global events handler for all registered sockets.final booleanunregister(Object socketOrChannel) Unregister a Socket or SelectableChannel for polling on the specified events.booleanbooleanwritable(SelectableChannel channel) booleanwritable(ZMQ.Socket socket)
-
Field Details
-
POLLIN
public static final int POLLIN- See Also:
-
POLLOUT
public static final int POLLOUT- See Also:
-
POLLERR
public static final int POLLERR- See Also:
-
IN
public static final int IN- See Also:
-
OUT
public static final int OUT- See Also:
-
ERR
public static final int ERR- See Also:
-
READABLE
public static final int READABLE- See Also:
-
WRITABLE
public static final int WRITABLE- See Also:
-
selector
-
creator
-
items
-
all
-
globalHandler
-
-
Constructor Details
-
ZPoller
Creates a new poller based on the current one. This will be a shadow poller, sharing the same selector and items creator. The global events handler will not be shared.- Parameters:
poller- the main poller.
-
ZPoller
Creates a new poller with a given selector for operational polling.- Parameters:
selector- the selector to use for polling.
-
ZPoller
Creates a new poller attached to a given context that will provide selector for operational polling.- Parameters:
context- the context that will provide the selector to use for polling.
-
ZPoller
Creates a new poller based on the current one. This will be a shadow poller, sharing the same selector. The global events handler will not be shared.- Parameters:
creator- the items creatorpoller- the main poller.
-
ZPoller
Creates a new poller attached to a given context that will provide selector for operational polling.- Parameters:
creator- the items creatorcontext- the context that will provide the selector to use for polling.
-
ZPoller
Creates a new poller.- Parameters:
creator- the items creatorselector- the selector to use for polling.
-
ZPoller
Creates a new poller.- Parameters:
creator- the items creatorcontext- the optional context where the selector should come from. If non-null, the selector will be destroyed on close.selector- the selector to use for polling.
-
-
Method Details
-
create
-
create
protected ZPoller.ItemHolder create(SelectableChannel channel, ZPoller.EventsHandler handler, int events) -
setGlobalHandler
Sets the global events handler for all registered sockets.- Parameters:
globalHandler- the events handler to set
-
getGlobalHandler
Returns the global events handler for all registered sockets.- Returns:
- the global events handler for all registered sockets.
-
register
public final boolean register(ZMQ.Socket socket, BiFunction<ZMQ.Socket, Integer, Boolean> handler, int events) Register a Socket for polling on specified events.- Parameters:
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.- Returns:
- true if registered, otherwise false
-
register
Register a Socket for polling on specified events.- Parameters:
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.- Returns:
- true if registered, otherwise false
-
register
-
register
-
register
public final boolean register(SelectableChannel channel, BiFunction<SelectableChannel, Integer, Boolean> handler, int events) Registers a SelectableChannel for polling on specified events.- Parameters:
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.- Returns:
- true if registered, otherwise false
-
register
Registers a SelectableChannel for polling on specified events.- Parameters:
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.- Returns:
- true if registered, otherwise false
-
register
Registers a SelectableChannel for polling on all events.- Parameters:
channel- the registering channel.handler- the events handler for this channel- Returns:
- true if registered, otherwise false
-
register
Registers a SelectableChannel for polling on specified events.- Parameters:
channel- the registering channel.events- the events to listen to, as a mask composed by ORing POLLIN, POLLOUT and POLLERR.- Returns:
- true if registered, otherwise false
-
register
Register an ItemHolder for polling on specified events.- Parameters:
item- the registering item.- Returns:
- true if registered, otherwise false
-
unregister
Unregister a Socket or SelectableChannel for polling on the specified events.- Parameters:
socketOrChannel- the Socket or SelectableChannel to be unregistered- Returns:
- true if unregistered, otherwise false TODO would it be useful to unregister only for specific events ?
-
poll
public int poll(long timeout) Issue a poll call, using the specified timeout value.Since ZeroMQ 3.0, the timeout parameter is in milliseconds, but prior to this the unit was microseconds.
- Parameters:
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).- Returns:
- how many objects where signaled by poll ()
- See Also:
-
poll
protected int poll(long timeout, boolean dispatchEvents) Issue a poll call, using the specified timeout value.- Parameters:
timeout- the timeout, as per zmq_poll ();dispatchEvents- true to dispatch events using items handler and the global one.- Returns:
- how many objects where signaled by poll ()
- See Also:
-
dispatch
-
poll
-
dispatch
Dispatches the polled events.- Parameters:
all- the items used for dispatchingsize- the number of items to dispatch- Returns:
- true if correctly dispatched, false in case of error
-
dispatch
public boolean dispatch() -
isReadable
Tells if a channel is readable from this poller.- Parameters:
channel- the channel to ask for.- Returns:
- true if readable, otherwise false
-
readable
-
isReadable
Tells if a socket is readable from this poller.- Parameters:
socket- the socket to ask for.- Returns:
- true if readable, otherwise false
-
readable
-
readable
-
pollin
-
pollin
-
isWritable
Tells if a channel is writable from this poller.- Parameters:
channel- the channel to ask for.- Returns:
- true if writable, otherwise false
-
writable
-
isWritable
Tells if a socket is writable from this poller.- Parameters:
socket- the socket to ask for.- Returns:
- true if writable, otherwise false
-
writable
-
writable
-
pollout
-
pollout
-
isError
Tells if a channel is in error from this poller.- Parameters:
channel- the channel to ask for.- Returns:
- true if in error, otherwise false
-
error
-
isError
Tells if a socket is in error from this poller.- Parameters:
socket- the socket to ask for.- Returns:
- true if in error, otherwise false
-
error
-
error
-
pollerr
-
pollerr
-
close
Destroys the poller. Does actually nothing.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
destroy
public void destroy()Destroys the poller without exception. -
add
-
createContainer
Deprecated. -
items
-
items
-
filter
-