Using PubSub+ Cache With the JCSMP API
When a session has been established with a PubSub+ event broker, a client application using the PubSub+ Message API for JCSMP can create a cache session and then make requests for cached messages (that is, cache requests).
The basic steps to make a cache request in a session are:
- Create a cache session. Refer to Creating Cache Session Objects.
- Send a cache request. Refer to Sending Cache Requests.
- Receive requested cached messages. Refer to Receiving Cached Messages.
Creating Cache Session Objects
Once JCSMPSession
is acquired, a client can call the createCacheSession(CacheSessionProperties)
method and pass in the cache session properties listed below to create a cache session object.
A cache session object allows multiple outstanding cache requests and synchronizes those requests. A single session can have many cache session objects.
Parameter | Description |
---|---|
cacheName |
The identifying name of the Distributed Cache, Cache Cluster, or PubSub+ Cache Instance to send the cache requests to. Specifying a PubSub+ Cache Instance bypasses redundancy and load balancing of cache requests. Specifying the Distributed Cache is the preferred method. |
maxMsgAge |
The maximum age (in seconds) of the messages to retrieve. Messages that have been cached for more than this amount of time are not retrieved. A value of 0 returns all possible messages for the topic, as defined by |
maxMsgsPerTopic |
The maximum number of messages to retrieve for any one topic. If more messages are cached on a topic than the maximum value, the newest messages are returned. A value of 0 retrieves all messages newer than |
timeout |
The amount of time (in milliseconds) to wait for a response to the cache request from the cache. This is a per-request timeout where each request sent by the application could result in multiple underlying requests. |
Sending Cache Requests
To send a cache request, the client application must call getMessageProducer(JCSMPStreamingPublishEventHandler)
to get an instance of XMLMessageProducer
and getMessageConsumer(XMLMessageListener
) to get an instance of XMLMessageConsumer
from the session. XMLMessageProducer
and XMLMessageConsumer
can be obtained before or after creating a cache session, but they must be obtained before sending a cache request.
Within a cache session, the client application can call sendCacheRequest(Long, Topic, boolean, CacheLiveDataAction)
to send a blocking cache request or call sendCacheRequest(Long, Topic, boolean, CacheLiveDataAction, CacheRequestListener)
to send a non‑blocking cache request message in the cache session.
If there is an error in the passed parameters, JCSMPException
is thrown.
It is up to the client to deal with request timeouts as it is possible that Cache Clusters, event brokers, or individual PubSub+ Cache Instances may fail or disconnect in the middle of cache request processing. The cache response indicates whether the PubSub+ Cache Instance is in a lost message state. Refer to Lost Message State for details.
Parameter | Description |
---|---|
requestId |
A long value for a request provided by the application that is used to identify the originating request in the |
topic |
The topic for which data is requested from the cache. Use of Wildcard cache requests are only supported for |
subscribe |
If this parameter is set to By sending a subscription with the cache request, the client will receive relevant live data messages after it has received all of the cached messages. |
liveDataAction |
Enumerated actions to perform on live data messages received during an outstanding cache request:
To perform wildcard cache requests, |
listener |
The |
After a non‑blocking cache request is made, the API can invoke the callback methods listed below when the request completes or returns an exception.
Method | Description |
---|---|
onComplete(...) |
Indicates that the non‑blocking cache request completed successfully. The corresponding request ID, subscription topic, and cache request result are provided. The possible cache request results include:
|
onException(...) |
An error interrupted the non‑blocking cache request. The corresponding request ID, subscription topic, and exception that occurred during the cache request are provided. If a failure occurs, the API does not automatically retry any outstanding cache requests. In addition, if there is a loss of connection with the event broker, any outstanding cache requests are canceled. |
Receiving Cached Messages
When using JCSMP, cached messages are received using the same methods as other messages, except that for non‑blocking cache messages a CacheRequestListener
is required. For more information on receiving Direct messages, refer to Receiving Direct Messages.
The messaging API does not attempt to reorder cached messages received across different Cache Clusters. In addition, the messaging API does not attempt to drop duplicate messages received from Cache Clusters or live publishers.
Destroying Cache Sessions
To cancel all outstanding requests and destroy the cache session object, call the close()
method. It is safe to call this method from any thread.
When close()
is called, the following occurs:
- All outstanding cache requests are implicitly cancelled as they are when the
cancel()
method is called. - Any subsequent cache requests throw a
ClosedFacilityException
.
Canceling Cache Requests
To cancel all outstanding requests, without destroying the cache session object, call the cancel()
method. It is safe to call this method from any thread, and it can be called repeatedly.
When cancel()
is called, the following occurs:
- all outstanding cache requests are canceled
- all live messages that have been queued are delivered
- all in-progress synchronous cache requests immediately return to the caller through a
JCSMPInterruptedException
- all in-progress asynchronous cache requests have their
CacheRequestListener onException(...)
method called immediately with aJCSMPInterruptedException