Java (Springboot)
Last updated
Was this helpful?
Last updated
Was this helpful?
The KubeMQ SDK for Spring Boot enables Spring Boot developers to seamlessly communicate with the KubeMQ server, implementing various communication patterns such as Events, EventStore, Commands, Queries, and Queues.
Java Development Kit (JDK) 17 or higher
Spring Boot 3.x
Maven
KubeMQ server running locally or accessible over the network
The recommended way to use the SDK for Java in your project is to add it as a dependency in Maven:
To build with Gradle, add the dependency to your build.gradle
file:
The examples are standalone projects that showcase the usage of the SDK with Spring Boot. To run the examples, ensure you have a running instance of KubeMQ. Import the project into any IDE of your choice (e.g., IntelliJ, Eclipse, NetBeans). The example project contains three packages demonstrating different implementations:
io.kubemq.example.cq
: Examples related to Commands and Queries
io.kubemq.example.pubsub
: Examples related to Events and EventStore
io.kubemq.example.queues
: Examples related to Queues
Once you check out the code from GitHub, you can build it using Maven:
This command will run the tests and install the JAR file to your local Maven repository. To skip the tests, use the following command:
The SDK implements all communication patterns available through the KubeMQ server:
PubSub
Events
EventStore
Commands & Queries (CQ)
Commands
Queries
Queues
All KubeMQ clients (PubSubClient, QueuesClient, and CQClient) share the same configuration parameters. You can use Spring Boot @Autowired
To inject any client instance. In Spring Boot application project you have to specify the property in configuration files. Minimum two mandatory parameters: kubemq.client.address
(KubeMQ server address) and kubemq.client.clientId
you need to specify.
Configuration Example: -
application.properties
application.yml
The table below describes all available configuration parameters:
address
String
The address of the KubeMQ server.
None
Yes
clientId
String
The client ID used for authentication.
None
Yes
authToken
String
The authorization token for secure communication.
None
No
tls
boolean
Indicates if TLS (Transport Layer Security) is enabled.
false
No
tlsCertFile
String
The path to the TLS certificate file.
None
No
tlsKeyFile
String
The path to the TLS key file.
None
No
caCertFile
String
The path to the CA certificate file.
None
No
maxReceiveSize
int
The maximum size of the messages to receive (in bytes).
104857600 (100MB)
No
reconnectIntervalSeconds
int
The interval in seconds between reconnection attempts.
1
No
keepAlive
boolean
Indicates if the connection should be kept alive.
false
No
pingIntervalInSeconds
int
The interval in seconds between ping messages.
60
No
pingTimeoutInSeconds
int
The timeout in seconds for ping messages.
30
No
logLevel
Level
The logging level to use.
Level.INFO
No
Enable component scan for package io.kubmq
this is base package for SDK, so spring can scan for beans and make it available for Auto-wiring.
Here's an example of how to Autowire client instance:
The parameters specified in configuration file remain the same for all client types.
For secure connections, set tls
to true
and provide the paths to your TLS certificate and key files.
Adjust maxReceiveSize
based on your expected message sizes to optimize performance.
Fine-tune reconnectIntervalSeconds
, keepAlive
, pingIntervalInSeconds
, and pingTimeoutInSeconds
based on your network conditions and requirements.
Choose an appropriate logLevel
for your development or production environment.
Remember to handle any exceptions that might be thrown during client creation, such as connection errors or invalid configuration parameters.
All KubeMQ clients (PubSubClient, QueuesClient, and CQClient) provide an optional ping()
method to verify connectivity with the KubeMQ server. This method is not required for normal operations and should be used sparingly.
ServerInfo
The ping()
method returns a ServerInfo
object with the following attributes:
host
String
The host address of the KubeMQ server
version
String
The version of the KubeMQ server
serverStartTime
long
The start time of the server (in seconds since epoch)
serverUpTimeSeconds
long
The uptime of the server in seconds
The ping operation is optional and should be used judiciously. Here are some appropriate scenarios for using ping:
Initial Connection Verification: You may use ping once after creating the client to verify the initial connection.
Troubleshooting: If you suspect connectivity issues, ping can help diagnose if the problem is with the connection to the KubeMQ server.
Long Periods of Inactivity: In applications with long periods of inactivity, you might use ping to check if the connection is still alive before performing an operation.
Not Required for Regular Operations: The ping operation is not needed for regular message sending or receiving operations. The client handles connection management internally.
Performance Consideration: Excessive use of ping can introduce unnecessary network traffic and potential performance overhead.
Not a Guarantee: A successful ping doesn't guarantee that all server functionalities are working correctly. It only verifies basic connectivity.
Error Handling: Always handle potential IOException when using ping, as network issues can occur.
Remember, the KubeMQ client is designed to handle connection management efficiently. In most cases, you can rely on the client to maintain the connection without explicit ping operations.
Creates a new Events channel.
channelName
String
Name of the channel you want to create
None
Yes
isChannelCreated
boolean
Indicates if channel was created
Deletes an existing Events channel.
channelName
String
Name of the channel you want to delete
None
Yes
isChannelDeleted
boolean
Indicates if channel was deleted
Retrieves a list of Events channels.
searchQuery
String
Search query to filter channels (optional)
None
No
Returns a List<PubSubChannel>
where each PubSubChannel
has the following attributes:
name
String
The name of the Pub/Sub channel.
type
String
The type of the Pub/Sub channel.
lastActivity
long
The timestamp of the last activity on the channel, represented in milliseconds since epoch.
isActive
boolean
Indicates whether the channel is active or not.
incoming
PubSubStats
The statistics related to incoming messages for this channel.
outgoing
PubSubStats
The statistics related to outgoing messages for this channel.
Sends a message to an Events channel.
EventMessage
Class Attributesid
String
Unique identifier for the event message.
None
No
channel
String
The channel to which the event message is sent.
None
Yes
metadata
String
Metadata associated with the event message.
None
No
body
byte[]
Body of the event message in bytes.
Empty byte array
No
tags
Map<String, String>
Tags associated with the event message as key-value pairs.
Empty Map
No
This method doesn't return a value. Successful execution implies the message was sent.
Subscribes to receive messages from an Events channel.
EventsSubscription
Class Attributeschannel
String
The channel to subscribe to.
None
Yes
group
String
The group to subscribe with.
None
No
onReceiveEventCallback
Consumer
Callback function to be called when an event message is received.
None
Yes
onErrorCallback
Consumer
Callback function to be called when an error occurs.
None
No
This method doesn't return a value. It sets up a subscription that will invoke the provided callbacks.
EventMessageReceived
Class Detailid
String
The unique identifier of the message.
fromClientId
String
The ID of the client that sent the message.
timestamp
long
The timestamp when the message was received, in seconds.
channel
String
The channel to which the message belongs.
metadata
String
The metadata associated with the message.
body
byte[]
The body of the message.
sequence
long
The sequence number of the message.
tags
Map<String, String>
The tags associated with the message.
Note: Remember to handle the subscription lifecycle appropriately in your application. You may want to store the subscription object to cancel it when it's no longer needed.
Creates a new EventsStore channel.
channelName
String
Name of the channel you want to create
None
Yes
isChannelCreated
boolean
Indicates if channel was created
Deletes an existing EventsStore channel.
channelName
String
Name of the channel you want to delete
None
Yes
isChannelDeleted
boolean
Indicates if channel was deleted
Retrieves a list of EventsStore channels.
searchQuery
String
Search query to filter channels (optional)
None
No
Returns a List<PubSubChannel>
where each PubSubChannel
has the following attributes:
name
String
The name of the Pub/Sub channel.
type
String
The type of the Pub/Sub channel.
lastActivity
long
The timestamp of the last activity on the channel, represented in milliseconds since epoch.
isActive
boolean
Indicates whether the channel is active or not.
incoming
PubSubStats
The statistics related to incoming messages for this channel.
outgoing
PubSubStats
The statistics related to outgoing messages for this channel.
Sends a message to an EventsStore channel.
EventStoreMessage
Class Attributesid
String
Unique identifier for the event message.
None
No
channel
String
The channel to which the event message is sent.
None
Yes
metadata
String
Metadata associated with the event message.
None
No
body
byte[]
Body of the event message in bytes.
Empty byte array
No
tags
Map<String, String>
Tags associated with the event message as key-value pairs.
Empty Map
No
Note: At least one of metadata
, body
, or tags
is required.
Returns an EventSendResult
object (details not provided in the original content).
Subscribes to receive messages from an EventsStore channel.
EventsStoreSubscription
Class Attributeschannel
String
The channel to subscribe to.
None
Yes
group
String
The group to subscribe with.
None
No
onReceiveEventCallback
Consumer
Callback function to be called when an event message is received.
None
Yes
onErrorCallback
Consumer
Callback function to be called when an error occurs.
None
No
eventsStoreType
EventsStoreType
Type of EventsStore subscription (e.g., StartAtTime, StartAtSequence)
None
Yes
eventsStoreStartTime
Instant
Start time for EventsStore subscription (if applicable)
None
Conditional
Undefined
0
Default value, should be explicitly set to a valid type before use
StartNewOnly
1
Start storing events from the point when the subscription is made
StartFromFirst
2
Start storing events from the first event available
StartFromLast
3
Start storing events from the last event available
StartAtSequence
4
Start storing events from a specific sequence number
StartAtTime
5
Start storing events from a specific point in time
StartAtTimeDelta
6
Start storing events from a specific time delta in seconds
This method doesn't return a value. It sets up a subscription that will invoke the provided callbacks.
EventStoreMessageReceived
Class Detailid
String
The unique identifier of the message.
fromClientId
String
The ID of the client that sent the message.
timestamp
long
The timestamp when the message was received, in seconds.
channel
String
The channel to which the message belongs.
metadata
String
The metadata associated with the message.
body
byte[]
The body of the message.
sequence
long
The sequence number of the message.
tags
Map<String, String>
The tags associated with the message.
Note: Remember to handle the subscription lifecycle appropriately in your application. You may want to store the subscription object to cancel it when it's no longer needed.
Creates a new Command channel.
channelName
String
Name of the channel you want to create
None
Yes
isChannelCreated
boolean
Indicates if channel was created
Deletes an existing Command channel.
channelName
String
Name of the channel you want to delete
None
Yes
isChannelDeleted
boolean
Indicates if channel was deleted
Retrieves a list of Command channels.
searchString
String
Search query to filter channels (optional)
None
No
Returns a List<CQChannel>
where each CQChannel
has the following attributes:
name
String
The name of the channel.
type
String
The type of the channel.
lastActivity
long
The timestamp of the last activity on the channel.
isActive
boolean
Indicates whether the channel is currently active.
incoming
CQStats
Statistics about incoming messages to the channel.
outgoing
CQStats
Statistics about outgoing messages from the channel.
Sends a command request to a Command channel.
CommandMessage
Class Attributesid
String
The ID of the command message.
None
Yes
channel
String
The channel through which the command message will be sent.
None
Yes
metadata
String
Additional metadata associated with the command message.
None
No
body
byte[]
The body of the command message as bytes.
Empty byte array
No
tags
Map<String, String>
A dictionary of key-value pairs representing tags associated with the command message.
Empty Map
No
timeoutInSeconds
int
The maximum time in seconds for waiting to response.
None
Yes
CommandResponseMessage
Class AttributescommandReceived
CommandMessageReceived
The command message received in the response.
clientId
String
The client ID associated with the command response.
requestId
String
The unique request ID of the command response.
isExecuted
boolean
Indicates if the command has been executed.
timestamp
LocalDateTime
The timestamp when the command response was created.
error
String
The error message if there was an error.
Subscribes to receive command messages from a Command channel.
CommandsSubscription
Class Attributeschannel
String
The channel for the subscription.
None
Yes
group
String
The group associated with the subscription.
None
No
onReceiveCommandCallback
Consumer
Callback function for receiving commands.
None
Yes
onErrorCallback
Consumer
Callback function for error handling.
None
No
This method doesn't return a value. It sets up a subscription that will invoke the provided callbacks.
CommandMessageReceived
Class Attributesid
String
The unique identifier of the command message.
fromClientId
String
The ID of the client who sent the command message.
timestamp
Instant
The timestamp when the command message was received.
channel
String
The channel through which the command message was sent.
metadata
String
Additional metadata associated with the command message.
body
byte[]
The body of the command message as bytes.
replyChannel
String
The channel to which the reply should be sent.
tags
Map<String, String>
A dictionary of key-value pairs representing tags associated with the command message.
CommandResponseMessage
Class AttributesWhen responding to a received command, you should construct a CommandResponseMessage
with the following attributes:
commandReceived
CommandMessageReceived
The command message received in the response.
clientId
String
The client ID associated with the command response.
requestId
String
The unique request ID of the command response.
isExecuted
boolean
Indicates if the command has been executed.
timestamp
LocalDateTime
The timestamp when the command response was created.
error
String
The error message if there was an error.
Note: Remember to handle the subscription lifecycle appropriately in your application. You may want to store the subscription object to cancel it when it's no longer needed. Also, ensure that you properly construct and send a CommandResponseMessage
for each received command to complete the request-response cycle.
Creates a new Query channel.
channelName
String
Name of the channel you want to create
None
Yes
isChannelCreated
boolean
Indicates if channel was created
Deletes an existing Query channel.
channelName
String
Name of the channel you want to delete
None
Yes
isChannelDeleted
boolean
Indicates if channel was deleted
Retrieves a list of Query channels.
searchString
String
Search query to filter channels (optional)
None
No
Returns a List<CQChannel>
where each CQChannel
has the following attributes:
name
String
The name of the channel.
type
String
The type of the channel.
lastActivity
long
The timestamp of the last activity on the channel.
isActive
boolean
Indicates whether the channel is currently active.
incoming
CQStats
Statistics about incoming messages to the channel.
outgoing
CQStats
Statistics about outgoing messages from the channel.
Sends a query request to a Query channel.
QueryMessage
Class Attributesid
String
The ID of the query message.
None
Yes
channel
String
The channel through which the query message will be sent.
None
Yes
metadata
String
Additional metadata associated with the query message.
None
No
body
byte[]
The body of the query message as bytes.
Empty byte array
No
tags
Map<String, String>
A dictionary of key-value pairs representing tags associated with the query message.
Empty Map
No
timeoutInSeconds
int
The maximum time in seconds for waiting response.
None
Yes
QueryResponseMessage
Class AttributesqueryReceived
QueryMessageReceived
The query message received in the response.
clientId
String
The client ID associated with the query response.
requestId
String
The unique request ID of the query response.
executed
boolean
Indicates if the query has been executed.
timestamp
LocalDateTime
The timestamp when the query response was created.
metadata
String
Additional metadata associated with the response.
body
byte[]
The body of the query response as bytes.
error
String
The error message if there was an error.
Subscribes to receive query messages from a Query channel.
QueriesSubscription
Class Attributeschannel
String
The channel for the subscription.
None
Yes
group
String
The group associated with the subscription.
None
No
onReceiveQueryCallback
Consumer
Callback function for receiving queries.
None
Yes
onErrorCallback
Consumer
Callback function for error handling.
None
No
This method doesn't return a value. It sets up a subscription that will invoke the provided callbacks.
QueryMessageReceived
Class Attributesid
String
The unique identifier of the query message.
fromClientId
String
The ID of the client who sent the query message.
timestamp
Instant
The timestamp when the query message was received.
channel
String
The channel through which the query message was sent.
metadata
String
Additional metadata associated with the query message.
body
byte[]
The body of the query message as bytes.
replyChannel
String
The channel to which the reply should be sent.
tags
Map<String, String>
A dictionary of key-value pairs representing tags associated with the query message.
QueryResponseMessage
Class AttributesWhen responding to a received query, you should construct a QueryResponseMessage
with the following attributes:
queryReceived
QueryMessageReceived
The query message received in the response.
clientId
String
The client ID associated with the query response.
requestId
String
The unique request ID of the query response.
executed
boolean
Indicates if the query has been executed.
timestamp
LocalDateTime
The timestamp when the query response was created.
metadata
String
Additional metadata associated with the response.
body
byte[]
The body of the query response as bytes.
error
String
The error message if there was an error.