public class MigratoryDataClient extends Object
| Modifier and Type | Field and Description |
|---|---|
static String |
CONSTANT_WINDOW_BACKOFF
A constant used to define the reconnect policy.
|
static String |
NOTIFY_CONNECT_DENY
A constant which indicates that the client was denied to connect using the entitlement token defined on the client side.
|
static String |
NOTIFY_CONNECT_OK
A constant which indicates that the client was authorized to connect using the entitlement token defined on the client side.
|
static String |
NOTIFY_DATA_RESYNC
A constant which indicates that after a fail-over reconnection, the client successfully synchronized a subscribed
subject with the latest message available for that subject, but not with the potential messages published during
the fail-over, therefore behaving as a fresh client.
|
static String |
NOTIFY_DATA_SYNC
A constant which indicates that after a fail-over reconnection, the client successfully synchronized a subscribed
subject with the latest message available for that subject, as well as with all messages published during the
fail-over period for that subject.
|
static String |
NOTIFY_MESSAGE_SIZE_LIMIT_EXCEEDED
A constant which indicates that the client was unable to publish a message because the size of the message
exceeds the message size limit allowed by the server (see the server parameter \c MAX_MESSAGE_SIZE).
|
static String |
NOTIFY_PUBLISH_DENIED
A constant which indicates that the client was not authorized to publish a message.
|
static String |
NOTIFY_PUBLISH_FAILED
A constant which indicates that the client was unable to publish a message.
|
static String |
NOTIFY_PUBLISH_OK
A constant which indicates that the client successfully published a message.
|
static String |
NOTIFY_SERVER_DOWN
A constant which indicates that the client failed to connect to a MigratoryData server.
|
static String |
NOTIFY_SERVER_UP
A constant which indicates that the client successfully connected to a MigratoryData server.
|
static String |
NOTIFY_SUBSCRIBE_ALLOW
A constant which indicates that the client was authorized to subscribe to a subject.
|
static String |
NOTIFY_SUBSCRIBE_DENY
A constant which indicates that the client was not authorized to subscribe to a subject.
|
static String |
TRANSPORT_HTTP
A constant used to define the transport type.
|
static String |
TRANSPORT_WEBSOCKET
A constant used to define the transport type.
|
static String |
TRUNCATED_EXPONENTIAL_BACKOFF
A constant used to define the reconnect policy.
|
| Constructor and Description |
|---|
MigratoryDataClient()
Create a MigratoryDataClient object.
|
| Modifier and Type | Method and Description |
|---|---|
void |
advertiseInteractiveSubjects(List<String> wildcardSubjects)
Advertise interactive subjects via a list of wildcard subjects.
|
void |
connect()
Connect to the MigratoryData cluster.
|
void |
disconnect()
Disconnect from the connected MigratoryData server and dispose the resources used by the connection.
|
MigratoryDataListener |
getListener()
Get the listener for handling real-time messages and status notifications.
|
Collection<String> |
getSubjects()
\brief Return the list of subscribed subjects.
|
void |
notifyAfterFailedConnectionAttempts(int retries)
Define the number of failed attempts to connect to one or more MigratoryData servers before triggering a status
notification MigratoryDataClient.NOTIFY_SERVER_DOWN.
|
void |
pause() |
void |
publish(MigratoryDataMessage message)
Publish a message.
|
void |
resume() |
void |
setBenchmarkMode(boolean b) |
void |
setEncryption(boolean encrypted)
Configure whether or not to use an encrypted SSL/TLS connection to communicate with the server.
|
void |
setEntitlementToken(String token)
Assign an entitlement token to the client or update its entitlement token.
|
void |
setExternalToken(String externalToken,
MonotonicId monotonicId)
Assign an external token to a client.
|
void |
setHttpHeader(String header,
String value)
This method allows for the inclusion of custom HTTP headers.
|
void |
setListener(MigratoryDataListener listener)
Attach a listener for handling the received real-time messages as well as the status notifications.
|
void |
setLogListener(MigratoryDataLogListener logListener,
MigratoryDataLogLevel logLevel)
Attach a listener for handling log messages outputted by the library.
|
void |
setQuickReconnectInitialDelay(int seconds)
\brief Define the number of seconds to wait before attempting to reconnect to the cluster after a connection
failure is detected.
|
void |
setQuickReconnectMaxRetries(int retries)
Define the maximum number of retries for the Quick Reconnect fail-over phase.
|
void |
setReconnectMaxDelay(int seconds)
Define the maximum reconnect delay for the MigratoryDataClient.TRUNCATED_EXPONENTIAL_BACKOFF policy.
|
void |
setReconnectPolicy(String policy)
Define the reconnect policy to be used after the Quick Reconnect phase.
|
void |
setReconnectTimeInterval(int seconds)
Define the time interval used for the reconnect schedule after the Quick Reconnect phase.
|
void |
setServers(String[] servers)
Specify a cluster of one or more MigratoryData servers to which the client will connect to.
|
void |
setSocketTimeoutSeconds(int seconds) |
void |
setSslHandshakeTimeoutSeconds(int seconds) |
void |
setTimestamp(boolean b) |
void |
setTransport(String type)
Define the transport type used by the client to communicate with the MigratoryData cluster.
|
void |
setVerboseLogEventsHandler(VerboseLogEvents logEvents) |
void |
subscribe(List<String> subjects)
Subscribe to one or more subjects.
|
void |
subscribeFromOffset(String subject,
int epoch,
int seq) |
void |
subscribeWithHistory(List<String> subjects,
int numberOfHistoricalMessages)
Get the historical messages for one or more subjects, if any, then subscribe to those subjects.
|
void |
unsubscribe(List<String> subjects)
Unsubscribe from one or more subjects.
|
public static final String NOTIFY_SERVER_UP
public static final String NOTIFY_SERVER_DOWN
public static final String NOTIFY_DATA_SYNC
public static final String NOTIFY_DATA_RESYNC
public static final String NOTIFY_SUBSCRIBE_ALLOW
public static final String NOTIFY_SUBSCRIBE_DENY
public static final String NOTIFY_PUBLISH_OK
public static final String NOTIFY_PUBLISH_FAILED
public static final String NOTIFY_MESSAGE_SIZE_LIMIT_EXCEEDED
public static final String NOTIFY_PUBLISH_DENIED
public static final String NOTIFY_CONNECT_OK
Custom authorization extension built with the
MigratoryData Extensions API version 2 or later. For the entitlement methods None, Basic,
or Custom authorization extension built with the previous version of the MigratoryData Extensions API,
this notification is always sent no matter the entitlement token is valid or not, the verification of the entitlement
token being made only during subscribe and publish operations.public static final String NOTIFY_CONNECT_DENY
Custom authorization extension built with the
MigratoryData Extensions API version 2 or later. For the entitlement methods None, Basic,
or Custom authorization extension built with the previous version of the MigratoryData Extensions API,
this notification is never sent no matter the entitlement token is valid or not, the verification of the entitlement
token being made only during subscribe and publish operations.public static final String CONSTANT_WINDOW_BACKOFF
public static final String TRUNCATED_EXPONENTIAL_BACKOFF
public static String TRANSPORT_HTTP
public static String TRANSPORT_WEBSOCKET
public MigratoryDataClient()
public void setLogListener(MigratoryDataLogListener logListener, MigratoryDataLogLevel logLevel)
logListener - an implementation of the \link MigratoryDataLogListener \endlink interfacelogLevel - a particular \link MigratoryDataLogLevel \endlink configured as the logging thresholdpublic void setListener(MigratoryDataListener listener)
listener - an implementation of the \link MigratoryDataListener \endlink interfacepublic MigratoryDataListener getListener()
public void setServers(String[] servers)
client.setServers(new String[] {"p1.example.com:80", "p2.example.com:80"});
To achieve load-balancing, the library connects the client to a MigratoryData server chosen randomly from the
\c servers list. In this way, the load is balanced among all the members of the cluster.
<small>
Moreover, the library supports weighted load-balancing. This feature is especially useful if the MigratoryData
servers in the cluster are installed on machines with different capacities. You can assign to each member of the
cluster a \em weight ranging from \c 0 to \c 100. This weight assignment is a hint provided to the library to
select with a higher probability a MigratoryData server with a higher weight either initially when the client
connects to the cluster or later during a fail-over reconnection.
Supposing the address \c p1.example.com corresponds to a machine that is twice more powerful than the machine
having the address \c p2.example.com, then you can assign to \c p1.example.com a weight \c 100 and to
\c p2.example.com a weight \c 50 by prefixing each address with the assigned weight as follows:
client.setServers(new String[] {"100 p1.example.com:80", "50 p2.example.com:80"});
The library assigns a default weight \c 100 to the addresses not prefixed with a specific weight.
</small>
To achieve failover, if the connection between the client and a MigratoryData server is broken, then the library
will automatically detect the failure and will select another MigratoryData server from the \c servers list. If
the client fails to connect to the newly selected server, a status notification
MigratoryDataClient.NOTIFY_SERVER_DOWN will be triggered (unless you modify the number of failed connection
attempts with \link MigratoryDataClient.notifyAfterFailedConnectionAttempts() \endlink), and a new MigratoryData
server of the cluster will be selected again and again until the client will be able to connect to one of the
MigratoryData servers of the cluster. When successfully connected the library will trigger a status notification
MigratoryDataClient.NOTIFY_SERVER_UP.servers - an array of strings where each string represents the network address (IP address or DNS domain
name and its corresponding port) of a MigratoryData server, optionally prefixed by a weight
ranging from \c 0 to \c 100; if the weight prefix is not provided to an address, then the \c 100
weight will be automatically assigned to that addresspublic void connect()
public void subscribe(List<String> subjects)
List<String> subjects = new ArrayList<String>();
subjects.add("/stocks/NYSE/IBM");
subjects.add("/stocks/Nasdaq/MSFT");
client.subscribe(subjects);
The subjects are strings having a particular syntax. See the "Concepts" section of the Architecture Guide
to learn the syntax of the subjects.subjects - subjects to subscribepublic void subscribeWithHistory(List<String> subjects, int numberOfHistoricalMessages)
subjects - subjects to subscribenumberOfHistoricalMessages - the number of historical messages to be retrieved from the cache of the serverpublic void subscribeFromOffset(String subject, int epoch, int seq)
public void unsubscribe(List<String> subjects)
subjects - subjects to unsubscribepublic void setEncryption(boolean encrypted)
encrypted - indicate whether or not to use an encrypted SSL/TLS connection to communicate with the serverpublic void setEntitlementToken(String token)
token - an entitlement token.public Collection<String> getSubjects()
public void notifyAfterFailedConnectionAttempts(int retries)
retries - the number of the failed attempts to connect to one or more MigratoryData servers before
triggering a status notification MigratoryDataClient.NOTIFY_SERVER_DOWN; the default is \c 1public void disconnect()
public void publish(MigratoryDataMessage message)
message - a MigratoryDataMessage messagepublic void setQuickReconnectMaxRetries(int retries)
retries - the maximum number of quick reconnect retries; the default is \c 3public void setQuickReconnectInitialDelay(int seconds)
Quick Reconnect Phase (retries <= quickReconnectMaxRetries)
-----------------------------------------------------------
(retries starts with 1 and increment by 1 at each quick reconnect)
reconnectDelay = quickReconnectInitialDelay * retries - random(0, quickReconnectInitialDelay)
After Quick Reconnect Phase (retries > quickReconnectMaxRetries)
----------------------------------------------------------------
(reset retries to start with 1 and increment by 1 at each reconnect)
If reconnectPolicy is CONSTANT_WINDOW_BACKOFF, then
reconnectDelay = reconnectTimeInterval
else if reconnectPolicy is TRUNCATED_EXPONENTIAL_BACKOFF, then
reconnectDelay = min(reconnectTimeInterval * (2 ^ retries) - random(0, reconnectTimeInterval * retries), reconnectMaxDelay)
For a few users which are subject to temporary, atypical network conditions, if reconnectDelay computed
with the algorithm above is less than 10 seconds, then it is rounded to 10 seconds.seconds - the number of seconds to wait before attempting to reconnect to the cluster; the default is \c 5public void setReconnectPolicy(String policy)
policy - the reconnect policy to be used after the Quick Reconnect phase; the possible values are:
\link MigratoryDataClient.CONSTANT_WINDOW_BACKOFF \endlink and
\link MigratoryDataClient.TRUNCATED_EXPONENTIAL_BACKOFF \endlink; the default is the last onepublic void setReconnectTimeInterval(int seconds)
seconds - a time interval expressed in seconds used for reconnect schedule; the default is \c 20public void setReconnectMaxDelay(int seconds)
seconds - the maximum reconnect delay when using the MigratoryDataClient.TRUNCATED_EXPONENTIAL_BACKOFF
policy; the default value is \c 360 secondspublic void setExternalToken(String externalToken, MonotonicId monotonicId)
com.migratorydata.client.utils.SharedPreferencesMonotonicId
which is a class which provides a monotonic id implementation. The monotonic id is stored in the SharedPreferences
such that it can tolerate application failures or Android device restarts.externalToken - a tokenmonotonicId - an implementation of the MonotonicId interfacepublic void setTransport(String type)
type - the possible values are:
MigratoryDataClient.TRANSPORT_HTTP and
MigratoryDataClient.TRANSPORT_WEBSOCKET; the default value is the last onepublic void advertiseInteractiveSubjects(List<String> wildcardSubjects)
If the Interactive Publishing extension is enabled in the MigratoryData server, i.e. the parameter
Extension.InteractivePublishing of the MigratoryData server is set on true, then you
can use this API method to advertise the subjects to be used for interactive publishing as detailed below.
First, a wildcard subject is a MigratoryData subject where the last segment is *. For
example, by using the wildcard subject /stocks/ibm/* in the argument of this API method, it will
advertise the interactive subjects that start with /stocks/ibm/, e.g. /stocks/ibm/last/bid,
/stocks/ibm/level2/price etc. We say that an interactive subject, say /stocks/ibm/last/bid,
matches the wildcard subject /stocks/ibm/*.
Warning: Currently only wildcard subjects with two segments are supported. For example, the following
wildcard subject with two segments /stocks/* is supported. However, the following wildcard subject
/quotes/nyse/* is not currently supported as it has three segments. You need to use the broader
wildcard subject /quotes/* to advertise interactive subjects matching /quotes/nyse/*.
To learn more about the valid syntax of subjects, please check out the Concepts section of the product documentation.
An interactive subject is a subject for which this API triggers subscription events such as the first
subscribe event and the last unsubscribe event. These events are triggered via the callbacks
MigratoryDataInteractiveListener.onSubscribe(String) and
MigratoryDataInteractiveListener.onUnsubscribe(String) of the interface
MigratoryDataInteractiveListener.
The interactive subjects and theirs subscription events are useful to implement interactive publishers, i.e. publishers that can start publishing data messages on an interactive subject when a client subscribes for the first time to that interactive subject, and stop publishing data messages when there are no more clients subscribed to that interactive subject.
wildcardSubjects - A list of wildcard subjects.public void setHttpHeader(String header, String value)
header - header namevalue - header valuepublic void setTimestamp(boolean b)
public void setBenchmarkMode(boolean b)
public void pause()
public void resume()
public void setVerboseLogEventsHandler(VerboseLogEvents logEvents)
public void setSocketTimeoutSeconds(int seconds)
public void setSslHandshakeTimeoutSeconds(int seconds)
Copyright © 2023. All rights reserved.