public class ZProxy
extends java.lang.Object
ZActor
.
Basic features:
Hot Restart
: User-defined behavior with custom messages
Configure
: User-defined behavior with custom messages
command(String, boolean)
...: Custom commands of your own
ZProxy.ZmqPump
(zmq.ZMQ): useful for performances
ZProxy.ZPump
(ZeroMQ): useful for message transformation
, lower performances
1-method interface
You can have all the above non-customizable features in about these lines of code:
final ZProxy.Proxy provider = new ZProxy.SimpleProxy()
{
public Socket create(ZContext ctx, ZProxy.Plug place, Object ... args)
{
assert ("TEST".equals(args[0]);
Socket socket = null;
if (place == ZProxy.Plug.FRONT) {
socket = ctx.createSocket(ZMQ.ROUTER);
}
if (place == ZProxy.Plug.BACK) {
socket = ctx.createSocket(ZMQ.DEALER);
}
return socket;
}
public void configure(Socket socket, ZProxy.Plug place, Object ... args)
{
assert ("TEST".equals(args[0]);
int port = -1;
if (place == ZProxy.Plug.FRONT) {
port = socket.bind("tcp://127.0.0.1:6660");
}
if (place == ZProxy.Plug.BACK) {
port = socket.bind("tcp://127.0.0.1:6661");
}
if (place == ZProxy.Plug.CAPTURE && socket != null) {
socket.bind("tcp://127.0.0.1:4263");
}
}
};
ZProxy proxy = ZProxy.newProxy("ProxyOne", provider, "ABRACADABRA", Arrays.asList("TEST"));
Once created, the proxy is not started. You have to perform first a start command on it.
This choice was made because it is easier for a user to start it with one line of code than for the code to internally handle
different possible starting states (after all, someone may want the proxy started but paused at first or configured in a specific way?)
and because the a/sync stuff was funnier. Life is unfair ...
Or maybe an idea is floating in the air?
final boolean async = false, sync = true;
String status = null;
status = proxy.status();
status = proxy.pause(sync);
status = proxy.start(async);
status = proxy.restart(new ZMsg());
status = proxy.status(async);
status = proxy.stop(sync);
boolean here = proxy.sign();
ZMsg cfg = new ZMsg();
msg.add("CONFIG-1");
ZMsg rcvd = proxy.configure(cfg);
proxy.exit();
status = proxy.status(sync);
assert (!proxy.started());
A programmatic interface
with enums is also available.Modifier and Type | Class and Description |
---|---|
static class |
ZProxy.Command |
static class |
ZProxy.Plug
Possible places for sockets in the proxy.
|
static interface |
ZProxy.Proxy |
private static class |
ZProxy.ProxyActor |
static interface |
ZProxy.Pump |
static class |
ZProxy.State |
private static class |
ZProxy.ZmqPump
A specialized transport for better transmission purposes
that will send each packets individually instead of the whole message.
|
static class |
ZProxy.ZPump
A pump that reads a message as a whole before transmitting it.
|
Modifier and Type | Field and Description |
---|---|
private ZAgent |
agent |
static java.lang.String |
ALIVE |
private static java.lang.String |
CONFIG |
private static java.util.concurrent.atomic.AtomicInteger |
counter |
private ZStar.Exit |
exit |
private static java.lang.String |
EXIT |
static java.lang.String |
EXITED |
private static java.lang.String |
PAUSE |
static java.lang.String |
PAUSED |
private static java.lang.String |
RESTART |
private static java.lang.String |
START |
static java.lang.String |
STARTED |
private static java.lang.String |
STATUS |
private static java.lang.String |
STOP |
static java.lang.String |
STOPPED |
Constructor and Description |
---|
ZProxy(java.lang.String name,
ZAgent.SelectorCreator selector,
ZProxy.Proxy creator,
java.lang.String motdelafin,
java.lang.Object... args)
Deprecated.
use
ZProxy(String, Proxy, String, Object...) instead. |
ZProxy(java.lang.String name,
ZProxy.Proxy creator,
java.lang.String motdelafin,
java.lang.Object... args)
Creates a new named proxy.
|
ZProxy(java.lang.String name,
ZProxy.Proxy sockets,
ZProxy.Pump pump,
java.lang.String motdelafin,
java.lang.Object... args)
Creates a new named proxy.
|
ZProxy(ZAgent.SelectorCreator selector,
ZProxy.Proxy creator,
java.lang.String motdelafin,
java.lang.Object... args)
Deprecated.
use
ZProxy(Proxy, String, Object...) instead. |
ZProxy(ZContext ctx,
java.lang.String name,
ZAgent.SelectorCreator selector,
ZProxy.Proxy sockets,
ZProxy.Pump pump,
java.lang.String motdelafin,
java.lang.Object... args)
Deprecated.
|
ZProxy(ZContext ctx,
java.lang.String name,
ZProxy.Proxy sockets,
ZProxy.Pump pump,
java.lang.String motdelafin,
java.lang.Object... args)
Creates a new named proxy.
|
ZProxy(ZProxy.Proxy creator,
java.lang.String motdelafin,
java.lang.Object... args)
Creates a new unnamed proxy.
|
Modifier and Type | Method and Description |
---|---|
java.lang.String |
command(java.lang.String command,
boolean sync)
Sends a command message to the proxy actor.
|
ZProxy.State |
command(ZProxy.Command command,
boolean sync)
Sends a command message to the proxy actor.
|
ZMsg |
command(ZProxy.Command command,
ZMsg msg,
boolean sync)
Sends a command message to the proxy actor.
|
ZMsg |
configure(ZMsg msg)
Configures the proxy.
|
java.lang.String |
exit()
Stops the proxy and exits.
|
java.lang.String |
exit(boolean sync)
Deprecated.
The call is synchronous: the sync parameter is ignored,
as it leads to many mistakes in case of a provided ZContext.
|
boolean |
isStarted()
Binary inquiry for the status of the proxy.
|
static ZProxy |
newProxy(ZContext ctx,
java.lang.String name,
ZAgent.SelectorCreator selector,
ZProxy.Proxy sockets,
java.lang.String motdelafin,
java.lang.Object... args)
Deprecated.
use
newProxy(ZContext, String, Proxy, String, Object...) instead. |
static ZProxy |
newProxy(ZContext ctx,
java.lang.String name,
ZProxy.Proxy sockets,
java.lang.String motdelafin,
java.lang.Object... args)
Creates a new low-level proxy for better performances.
|
static ZProxy |
newZProxy(ZContext ctx,
java.lang.String name,
ZAgent.SelectorCreator selector,
ZProxy.Proxy sockets,
java.lang.String motdelafin,
java.lang.Object... args)
Deprecated.
|
static ZProxy |
newZProxy(ZContext ctx,
java.lang.String name,
ZProxy.Proxy sockets,
java.lang.String motdelafin,
java.lang.Object... args)
Creates a new proxy in a ZeroMQ way.
|
java.lang.String |
pause(boolean sync)
Pauses the proxy.
|
private java.lang.String |
recvStatus() |
java.lang.String |
restart(ZMsg hot)
Restarts the proxy.
|
java.lang.String |
start(boolean sync)
Starts the proxy.
|
boolean |
started()
Binary inquiry for the status of the proxy.
|
java.lang.String |
status()
Inquires for the status of the proxy.
|
java.lang.String |
status(boolean sync)
Inquires for the status of the proxy.
|
java.lang.String |
stop(boolean sync)
Stops the proxy.
|
private static final java.lang.String START
private static final java.lang.String PAUSE
private static final java.lang.String STOP
private static final java.lang.String RESTART
private static final java.lang.String EXIT
private static final java.lang.String STATUS
private static final java.lang.String CONFIG
public static final java.lang.String STARTED
public static final java.lang.String PAUSED
public static final java.lang.String STOPPED
public static final java.lang.String EXITED
public static final java.lang.String ALIVE
private static final java.util.concurrent.atomic.AtomicInteger counter
private final ZAgent agent
private final ZStar.Exit exit
@Deprecated public ZProxy(ZAgent.SelectorCreator selector, ZProxy.Proxy creator, java.lang.String motdelafin, java.lang.Object... args)
ZProxy(Proxy, String, Object...)
instead.selector
- the creator of the selector used for the proxy.creator
- the creator of the sockets of the proxy.motdelafin
- the final word used to mark the end of the proxy. Null to disable this mechanism.args
- an optional array of arguments that will be passed at the creation.public ZProxy(ZProxy.Proxy creator, java.lang.String motdelafin, java.lang.Object... args)
creator
- the creator of the sockets of the proxy.motdelafin
- the final word used to mark the end of the proxy. Null to disable this mechanism.args
- an optional array of arguments that will be passed at the creation.@Deprecated public ZProxy(java.lang.String name, ZAgent.SelectorCreator selector, ZProxy.Proxy creator, java.lang.String motdelafin, java.lang.Object... args)
ZProxy(String, Proxy, String, Object...)
instead.name
- the name of the proxy (used in threads naming).selector
- the creator of the selector used for the proxy.creator
- the creator of the sockets of the proxy.motdelafin
- the final word used to mark the end of the proxy. Null to disable this mechanism.args
- an optional array of arguments that will be passed at the creation.public ZProxy(java.lang.String name, ZProxy.Proxy creator, java.lang.String motdelafin, java.lang.Object... args)
name
- the name of the proxy (used in threads naming).creator
- the creator of the sockets of the proxy.motdelafin
- the final word used to mark the end of the proxy. Null to disable this mechanism.args
- an optional array of arguments that will be passed at the creation.@Deprecated public ZProxy(ZContext ctx, java.lang.String name, ZAgent.SelectorCreator selector, ZProxy.Proxy sockets, ZProxy.Pump pump, java.lang.String motdelafin, java.lang.Object... args)
ZProxy(ZContext, String, Proxy, Pump, String, Object...)
instead.ctx
- the main context used.
If null, a new context will be created and closed at the stop of the operation.
If not null, it is the responsibility of the call to close it.name
- the name of the proxy (used in threads naming).selector
- the creator of the selector used for the proxy.sockets
- the creator of the sockets of the proxy.pump
- the pump used for the proxymotdelafin
- the final word used to mark the end of the proxy. Null to disable this mechanism.args
- an optional array of arguments that will be passed at the creation.public ZProxy(java.lang.String name, ZProxy.Proxy sockets, ZProxy.Pump pump, java.lang.String motdelafin, java.lang.Object... args)
name
- the name of the proxy (used in threads naming).sockets
- the creator of the sockets of the proxy.pump
- the pump used for the proxymotdelafin
- the final word used to mark the end of the proxy. Null to disable this mechanism.args
- an optional array of arguments that will be passed at the creation.public ZProxy(ZContext ctx, java.lang.String name, ZProxy.Proxy sockets, ZProxy.Pump pump, java.lang.String motdelafin, java.lang.Object... args)
ctx
- the main context used.
If null, a new context will be created and closed at the stop of the operation.
If not null, it is the responsibility of the call to close it.name
- the name of the proxy (used in threads naming).sockets
- the creator of the sockets of the proxy.pump
- the pump used for the proxymotdelafin
- the final word used to mark the end of the proxy. Null to disable this mechanism.args
- an optional array of arguments that will be passed at the creation.@Deprecated public static ZProxy newZProxy(ZContext ctx, java.lang.String name, ZAgent.SelectorCreator selector, ZProxy.Proxy sockets, java.lang.String motdelafin, java.lang.Object... args)
newZProxy(ZContext, String, Proxy, String, Object...)
instead.low-level one
.ctx
- the context used for the proxy.
Possibly null, in this case a new context will be created and automatically destroyed afterwards.name
- the name of the proxy. Possibly null.selector
- the creator of the selector used for the internal polling. Not null.sockets
- the sockets creator of the proxy. Not null.motdelafin
- the final word used to mark the end of the proxy. Null to disable this mechanism.args
- an optional array of arguments that will be passed at the creation.public static ZProxy newZProxy(ZContext ctx, java.lang.String name, ZProxy.Proxy sockets, java.lang.String motdelafin, java.lang.Object... args)
low-level one
.ctx
- the context used for the proxy.
Possibly null, in this case a new context will be created and automatically destroyed afterwards.name
- the name of the proxy. Possibly null.sockets
- the sockets creator of the proxy. Not null.motdelafin
- the final word used to mark the end of the proxy. Null to disable this mechanism.args
- an optional array of arguments that will be passed at the creation.@Deprecated public static ZProxy newProxy(ZContext ctx, java.lang.String name, ZAgent.SelectorCreator selector, ZProxy.Proxy sockets, java.lang.String motdelafin, java.lang.Object... args)
newProxy(ZContext, String, Proxy, String, Object...)
instead.ctx
- the context used for the proxy.
Possibly null, in this case a new context will be created and automatically destroyed afterwards.name
- the name of the proxy. Possibly null.selector
- the creator of the selector used for the internal polling. Not null.sockets
- the sockets creator of the proxy. Not null.motdelafin
- the final word used to mark the end of the proxy. Null to disable this mechanism.args
- an optional array of arguments that will be passed at the creation.public static ZProxy newProxy(ZContext ctx, java.lang.String name, ZProxy.Proxy sockets, java.lang.String motdelafin, java.lang.Object... args)
ctx
- the context used for the proxy.
Possibly null, in this case a new context will be created and automatically destroyed afterwards.name
- the name of the proxy. Possibly null.sockets
- the sockets creator of the proxy. Not null.motdelafin
- the final word used to mark the end of the proxy. Null to disable this mechanism.args
- an optional array of arguments that will be passed at the creation.public java.lang.String start(boolean sync)
sync
- true to read the status in synchronous way, false for asynchronous modepublic java.lang.String pause(boolean sync)
sync
- true to read the status in synchronous way, false for asynchronous modepublic java.lang.String stop(boolean sync)
sync
- true to read the status in synchronous way, false for asynchronous modepublic java.lang.String command(java.lang.String command, boolean sync)
CONFIG
and RESTART
.command
- the command to execute. Not null.sync
- true to read the status in synchronous way, false for asynchronous modepublic ZProxy.State command(ZProxy.Command command, boolean sync)
CONFIG
and RESTART
.command
- the command to execute.sync
- true to read the status in synchronous way, false for asynchronous modepublic ZMsg command(ZProxy.Command command, ZMsg msg, boolean sync)
CONFIG
and RESTART
.command
- the command to execute.msg
- the custom message to transmit.sync
- true to read the status in synchronous way, false for asynchronous modepublic ZMsg configure(ZMsg msg)
msg
- the custom message sent as configuration tippublic java.lang.String restart(ZMsg hot)
hot
- null to make a cold restart (closing then re-creation of the sockets)
or a configuration message to perform a configurable hot restart,@Deprecated public java.lang.String exit(boolean sync)
sync
- forced to true to read the status in synchronous way.public java.lang.String exit()
public java.lang.String status()
public java.lang.String status(boolean sync)
sync
- true to read the status in synchronous way, false for asynchronous mode.
If false, you get the last cached status of the proxyprivate java.lang.String recvStatus()
public boolean isStarted()
public boolean started()