public class HAClient extends Client
An HAClient object provides failover and resubscription functionality by
default. For reliable publish, set a PublishStore
for the HAClient
(and, in most cases, set a FailedWriteHandler
as well so the
application can detect any failed publishes). For managing transaction
log replays, set a BookmarkStore
.
To connect to AMPS, you must provide a ServerChooser
implementation to the HAClient. By default, the HAClient provides a
ExponentialDelayStrategy
with the default delay and backoff
behavior for that class.
By default, an HAClient object has the reconnect and resubscribe
functionality provided by the MemorySubscriptionManager
and the
HAClient.HADisconnectHandler
classes. It is typically not necessary to
replace the disconnect handler or subscription manager provided by
default. Notice that replacing either of these defaults will change
the failover, resubscription, and/or republish behavior of the
HAClient.
See the Developer Guide for more information on using the HAClient.
Example usage of the HAClient for publishing a JSON message:
public class ConsolePublisher{
// The location of the AMPS server.
private static final String uri_ = "tcp://127.0.0.1:9007/amps/json";
public static void main(String[] args) {
System.out.println("Starting the console publisher.");
try (HAClient client = new HAClient("ConsolePublisher");) {
// wait a bit to give a subscriber time to listen
Thread.sleep(2000L);
// Create a custom server chooser
DefaultServerChooser serverChooser = new DefaultServerChooser();
serverChooser.add(uri_);
// Set the server chooser for the HAClient
client.setServerChooser(serverChooser);
// connect to the AMPS server and logon
client.connectAndLogon();
System.out.println("Publisher Connected");
client.publish("messages", "{\"message\" : \"Hello, World!\"}");
}
catch (Exception e)
{
System.err.println(e.getLocalizedMessage());
e.printStackTrace(System.err);
}
}
}
Modifier and Type | Class and Description |
---|---|
static class |
HAClient.HADisconnectHandler
Class used to handle disconnects for highly-available AMPS Clients.
|
Client.Bookmarks, Client.ClientHandler, Client.GlobalCommandTypeHandlers, Client.Version
_lastFailedConnectionVersion, exceptionListener, lock, MIN_MULTI_BOOKMARK_VERSION, MIN_PERSISTED_BOOKMARK_VERSION
Constructor and Description |
---|
HAClient()
Default constructor that creates a client without a name.
|
HAClient(java.lang.String name)
Constructs a new HAClient.
|
Modifier and Type | Method and Description |
---|---|
void |
connect() |
void |
connect(java.lang.String uri)
Connects to the AMPS instance through the provided URI.
|
void |
connectAndLogon()
Connects to the next server chosen for us by our
ServerChooser . |
static HAClient |
createFileBacked(java.lang.String name,
java.lang.String publishLog,
int initialPublishCapacity,
java.lang.String subscribeLog)
Creates a new highly available client backed by disk.
|
static HAClient |
createFileBacked(java.lang.String name,
java.lang.String publishLog,
java.lang.String subscribeLog)
Creates a new highly available client backed by disk.
|
static HAClient |
createMemoryBacked(java.lang.String name)
Creates a new memory-backed highly-available client.
|
void |
disconnect()
Disconnect from the AMPS server.
|
ConnectionInfo |
getConnectionInfo()
Assembles a new ConnectionInfo with the state of this client and
associated classes.
|
java.lang.String |
getLogonOptions()
Returns the current logon options string.
|
ReconnectDelayStrategy |
getReconnectDelayStrategy()
Returns the
ReconnectDelayStrategy used to control sleeping
behavior before connecting to a server. |
ServerChooser |
getServerChooser()
Returns the current
ServerChooser for this client. |
int |
getTimeout()
Returns the timeout used for logging on and resubscribing, once a
connection is re-established.
|
CommandId |
logon()
Logs into AMPS with the parameters provided in the connect method.
|
CommandId |
logon(long timeout)
Logs into AMPS with the parameters provided in the connect method and the
logon correlation id, if any, that is set.
|
CommandId |
logon(long timeout,
Authenticator authenticator)
Logs into AMPS with the parameters provided in the connect method and the
logon correlation id, if any, that is set.
|
CommandId |
logon(long timeout,
Authenticator authenticator,
java.lang.String options)
Logs into AMPS with the parameters provided in the connect method and the
logon correlation id, if any, that is set.
|
CommandId |
logon(long timeout,
java.lang.String options)
Logs into AMPS with the parameters provided in the connect method and the
logon correlation id, if any, that is set.
|
CommandId |
logon(java.lang.String options)
Logs into AMPS with the parameters provided in the connect method and the
logon correlation id, if any, that is set.
|
void |
setLogonOptions(java.lang.String options_)
Sets a logon options string for self.
|
void |
setReconnectDelay(int reconnectInterval)
Sets the time delay between reconnect events.
|
HAClient |
setReconnectDelayStrategy(ReconnectDelayStrategy delayStrategy_)
Sets the
ReconnectDelayStrategy used to control sleeping
behavior before connecting to a server. |
void |
setServerChooser(ServerChooser serverChooser_)
Sets a
ServerChooser for self. |
void |
setTimeout(int timeout_)
Sets the timeout used for logging on and resubscribing, once a connection
is re-established.
|
_ack, _ack, _ack, ack, ack, ack, ack, ack, ack, addConnectionStateListener, addHttpPreflightHeader, addHttpPreflightHeader, addMessageHandler, allocateMessage, bookmarkSubscribe, broadcastConnectionStateChanged, clearHttpPreflightHeaders, close, deltaPublish, deltaPublish, deltaPublish, deltaPublish, deltaSubscribe, deltaSubscribe, deltaSubscribe, deltaSubscribe, execute, executeAsync, flush, flush, flushAcks, getAckBatchSize, getAckTimeout, getAutoAck, getBookmarkStore, getDefaultMaxDepth, getDisconnectHandler, getDuplicateMessageHandler, getExceptionListener, getFailedWriteHandler, getHeartbeatInterval, getHttpPreflightHeaders, getLogonCorrelationData, getName, getNameHash, getPublishStore, getReadTimeout, getRetryOnDisconnect, getServerVersion, getServerVersionInfo, getSubscriptionManager, getThreadCreatedHandler, getTransport, getTransportDisconnectHandler, getTransportFilter, getTransportMessageHandler, getURI, getVersion, getVersionAsInt, publish, publish, publish, publish, publishFlush, publishFlush, publishFlush, publishFlush, removeConnectionStateListener, removeMessageHandler, send, send, setAckBatchSize, setAckTimeout, setAutoAck, setBookmarkStore, setDefaultMaxDepth, setDisconnectHandler, setDuplicateMessageHandler, setExceptionListener, setFailedWriteHandler, setFailedWriteHandler, setGlobalCommandTypeMessageHandler, setGlobalCommandTypeMessageHandler, setGlobalCommandTypeMessageHandler, setHeartbeat, setHeartbeat, setHttpPreflightHeaders, setLastChanceMessageHandler, setLogonCorrelationData, setName, setPublishStore, setRetryOnDisconnect, setSubscriptionManager, setThreadCreatedHandler, setTransportFilter, setUnhandledMessageHandler, sow, sow, sow, sow, sow, sow, sow, sowAndDeltaSubscribe, sowAndDeltaSubscribe, sowAndDeltaSubscribe, sowAndDeltaSubscribe, sowAndDeltaSubscribe, sowAndDeltaSubscribe, sowAndSubscribe, sowAndSubscribe, sowAndSubscribe, sowAndSubscribe, sowAndSubscribe, sowAndSubscribe, sowAndSubscribe, sowAndSubscribe, sowDelete, sowDelete, sowDelete, sowDeleteByData, sowDeleteByKeys, startTimer, stopTimer, subscribe, subscribe, subscribe, subscribe, subscribe, subscribe, unsubscribe, unsubscribe
public HAClient()
Client.setName(String)
before connecting.HAClient(String)
,
Client.setName(String)
public HAClient(java.lang.String name)
name
- The client name, passed to the server to uniquely identify
this client across sessions. AMPS does not enforce
specific restrictions on the character set used, however some
protocols (for example, XML) may not allow specific characters.
60East recommends that the client name be meaningful, short,
human readable, and avoids using control characters, newline
characters, or square brackets.public void setReconnectDelay(int reconnectInterval)
FixedDelayStrategy
in this client.reconnectInterval
- The time waited between reconnect events, in
milliseconds.public void setTimeout(int timeout_)
timeout_
- The timeout value, in milliseconds, where 0 indicates no
timeout.public int getTimeout()
public ServerChooser getServerChooser()
ServerChooser
for this client.public void setServerChooser(ServerChooser serverChooser_)
ServerChooser
for self.serverChooser_
- The ServerChooser that will be used by self to
reconnect.public java.lang.String getLogonOptions()
public void setLogonOptions(java.lang.String options_)
options_
- The String that will be used by self
during logon.public static HAClient createMemoryBacked(java.lang.String name) throws StoreException
HAClient
and setting a MemoryBookmarkStore
and
MemoryPublishStore
for that client. The MemoryPublishStore
has a capacity of 10000 blocks. If your application will not be both
publishing to AMPS and using a bookmark replay, it is recommended that you
construct an HAClient
and then set the appropriate stores.name
- The client name, passed to the server to uniquely identify
this client across sessions. AMPS does not enforce specific
restrictions on the character set used, however some
protocols (for example, XML) may not allow specific characters.
60East recommends that the client name be meaningful, short,
human readable, and avoids using control characters, newline
characters or square brackets.StoreException
- Thrown when an operation fails with details of the
failure.public static HAClient createFileBacked(java.lang.String name, java.lang.String publishLog, int initialPublishCapacity, java.lang.String subscribeLog) throws java.io.IOException, StoreException
HAClient
and setting a LoggedBookmarkStore
and PublishStore
for
that client. If your application will not be both publishing to AMPS and
using a bookmark replay, it is recommended that you construct an
HAClient
and then set the appropriate stores.name
- The client name, passed to the server to uniquely identify
this client across sessions. AMPS does not enforce specific
restrictions on the character set used, however some protocols
(for example, XML) may not allow specific characters. 60East
recommends that the client name be meaningful, short, human
readable, and avoids using control characters, newline characters,
or square brackets.publishLog
- The path to a file used to store outgoing messages. This file
will be created if it does not exist.initialPublishCapacity
- The initial size (in 2k blocks) of the publish store. Choosing
an optimal value of initialPublishCapacity can prevent costly
resizes, when servers or networks slow down.subscribeLog
- The path to a file used to log incoming message bookmarks.
This file will be created if it does not exist.java.io.IOException
- Thrown if file creation fails.StoreException
- Thrown when an operation fails with details of the failure.public static HAClient createFileBacked(java.lang.String name, java.lang.String publishLog, java.lang.String subscribeLog) throws java.io.IOException, StoreException
HAClient
and setting a LoggedBookmarkStore
and PublishStore
that client. If your application will not be both
publishing to AMPS and using a bookmark replay, it is recommended that you
construct an HAClient
and then set the appropriate stores.name
- The client name, passed to the server to uniquely
identify this client across sessions. AMPS does not
enforce specific restrictions on the character set
used, however some protocols (for example, XML) may
not allow specific characters. 60East recommends that
the client name be meaningful, short, human readable,
and avoids using control characters, newline characters,
or square brackets.publishLog
- The path to a file used to store outgoing messages. This
file will be created if it does not exist.subscribeLog
- The path to a file used to log incoming message bookmarks. This file will be created if it does not exist.java.io.IOException
- Thrown when creating either the publish store or the
bookmark store results in an IOException.StoreException
- Thrown for any other failure. The exception will
contain details of the operation that failed.public void connectAndLogon() throws ConnectionException
ServerChooser
.
Will continue attempting to connect and logon to each successive
server returned by the ServerChooser until the connection succeeds
or the ServerChooser returns an empty URI.ConnectionException
- An exception indicating no AMPS instance
could be connected to.public void connect() throws ConnectionException
ConnectionException
public void connect(java.lang.String uri) throws ConnectionException
Client
The URI is a String
with the format:
transport://user:password@host:port/protocol
Notice that the protocol can be independent of the message type.
60East recommends using the amps
protocol, although some
installations use one of the legacy protocols such as fix
,
nvfix
or xml
. Contact your server
administrator for the correct URI for the instance.
Authentication is optional if the system is using the default authentication provided with AMPS. This default authentication allows all users to authenticate, regardless of the user or password provided. If your AMPS installation uses custom authentication, use the Authenticator appropriate for that authentication mechanism.
connect
in class Client
uri
- The URI string to connect toConnectionRefusedException
- The connection could not be establishedAlreadyConnectedException
- The connection is already connectedInvalidURIException
- The specified URI is invalidProtocolException
- The protocol is invalidTransportTypeException
- The transport type is invalidConnectionException
public CommandId logon(long timeout) throws ConnectionException
Client
logon
in class Client
timeout
- The number of milliseconds to wait for the client to receive
and consume a processed ack for this command, where 0
indicates to wait indefinitelyConnectionException
- A connection error occurred while logging on.public CommandId logon(java.lang.String options) throws ConnectionException
Client
logon
in class Client
options
- The options string to pass with the logon commandConnectionException
- A connection error occurred while logging on.public CommandId logon(long timeout, java.lang.String options) throws ConnectionException
Client
logon
in class Client
timeout
- The number of milliseconds to wait for the client to receive
and consume a processed ack for this command, where 0
indicates to wait indefinitelyoptions
- The options string to pass with the logon commandConnectionException
- A connection error occurred while logging on.public CommandId logon(long timeout, Authenticator authenticator) throws ConnectionException
Client
logon
in class Client
timeout
- The number of milliseconds to wait for the client to
receive and consume a processed ack for this command,
where 0 indicates to wait indefinitelyauthenticator
- The custom authenticator object to authenticate againstConnectionException
- A connection error occurred while logging on.public CommandId logon(long timeout, Authenticator authenticator, java.lang.String options) throws ConnectionException
Client
logon
in class Client
timeout
- The number of milliseconds to wait for the client to
receive and consume a processed ack for this command,
where 0 indicates to wait indefinitelyauthenticator
- The custom authenticator object to authenticate againstoptions
- The options string to pass with the logon commandConnectionException
- A connection error occurred while logging on.public CommandId logon() throws ConnectionException
Client
logon
in class Client
ConnectionException
- A connection error occurred while logging on.public void disconnect()
Client
disconnect
in class Client
public ConnectionInfo getConnectionInfo()
getConnectionInfo
in class Client
public final HAClient setReconnectDelayStrategy(ReconnectDelayStrategy delayStrategy_)
ReconnectDelayStrategy
used to control sleeping
behavior before connecting to a server.delayStrategy_
- The delay strategy to use for connecting.public ReconnectDelayStrategy getReconnectDelayStrategy()
ReconnectDelayStrategy
used to control sleeping
behavior before connecting to a server.