Message API consists of Message and Queue.
Message - Service for sending, pulling and removing Message from a particular queue.
Asynchronous messages can be sent to a queue which can be pulled using various receive messages.
Queue - Manages Asynchronous queues. Allows to create, delete, purge messages, view pending messages and
get all messages.
In order to use the various functions available under a particular API, a developer will have to create an instance of ServiceAPI by passing the apiKey and the secretKey which has been received while creating an app.
ServiceAPI api = new ServiceAPI("<API_KEY>","<SECRET_KEY>");
After initialization, developer needs to call the buildXXXService method on ServiceAPI instance to get the instance of the particular API that you wish to build. For example, To build an instance of QueueService, buildQueueService() method needs to be called.
QueueService queueService = api.buildQueueService()
Once the API instance has been retrieved, You are ready to use the functions available for that particular API.
The methods available under a particular API will return the domain object (like Queue in this case) as a response which will have the accessor / mutator to access the available properties for that object. You can get the response in the form of JSON as well. We have provided the JSON response with every function detail which can be retrieved by calling the toString() on the returned object.
String queueName = "MyQueue";
String queueDescription = "Hi its testing Time";
Queue queue = queueService.createPullQueue(queueName, queueDescription); /* returns the Queue object. */
System.out.println ("queueName is "+ queue.getQueueName () );
System.out.println ("queueType is "+ queue.getQueueType () );
System.out.println ("queueDescription is "+ queue.getDescription() );
String jsonResponse = queue.toString(); /* returns the response in JSON format. */
The functions available under Message/Queue API can throw some exceptions in abnormal conditions. Example of the same has been given below.
E.g. If App developer is sending a message on the queue which has not been created, the function will throw the App42Exception (as shown below) with message as "Not Found" and the appErrorCode as "2400" and the details as "Queue with the name '<queueName>' not found".
String queueName = "MyQueue";
String msg = "Its a Real Message";
long exp = 100000;
try
{
Queue queue = queueService.sendMessage(queueName, msg, exp);
}
catch(App42Exception ex)
{
int appErrorCode = ex.getAppErrorCode();
int httpErrorCode = ex.getHttpErrorCode();
if(appErrorCode == 2400)
{
// Handle here for Not Found (Queue with the name '<queueName> not found.)
}
else if(appErrorCode == 1401)
{
// handle here for Client is not authorized
}
else if(appErrorCode == 1500)
{
// handle here for Internal Server Error
}
String jsonText = ex.getMessage(); /* returns the Exception text in JSON format. (as shown below)*/
}
{
"app42Fault": {
"httpErrorCode": 404,
"appErrorCode": 2400,
"message": "Not Found",
"details": "Queue with the name 'MyQueue' not found"
}
}
Below are the HTTP Error Codes and their description, the function under the Message API can throw.
/* 1400 - BAD REQUEST - The Request parameters are invalid 1401 - UNAUTHORIZED - Client is not authorized 1500 - INTERNAL SERVER ERROR - Internal Server Error. Please try again */
Below are the Application Error Codes and their description, the function under the Message API can throw.
/* 2400 - NOT FOUND - Queue with the name '<queueName>' not found. 2401 - NOT FOUND - Queue with the name '<queueName>' does not have any messages. 2402 - NOT FOUND - Queue with the name '<queueName>' and correlationId '<correlationId>' does not have any messages. 2403 - BAD REQUEST - Queue by the name '<queueName>' already exists. 2404 - BAD REQUEST - The request parameters are invalid. This action is only applicable for PULL type queue. 2405 - NOT FOUND - Queue with the name '<queueName>' does not have any pending messages. */
Various functions available under Message/Queue API has been explained below.
Pull all the message from the queue.
Parameters:
Returns:
Response: Queue Object
Exception:
/* 2400 - NOT FOUND - Queue with the name '<queueName>' not found. 2401 - NOT FOUND - Queue with the name '<queueName>' does not have any messages. */
String queueName ="MyQueue";
long receiveTimeOut = 10000;
Queue queue = queueService.receiveMessage(queueName, receiveTimeOut); /* returns the Queue object. */
System.out.println("queueName is " + queue.getQueueName());
System.out.println("queueType is " + queue.getQueueType());
ArrayList<Queue.Message> messageList = queue.getMessageList();
for(Queue.Message message : messageList)
{
System.out.println("correlationId is " + message.getCorrelationId());
System.out.println("messageId is " + message.getMessageId());
System.out.println("payLoad is " + message.getPayLoad());
}
String jsonResponse = queue.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"queues": {
"queue": {
"queueName": "MyQueue",
"queueType": "PULL",
"messages": {
"message": {
"correlationId": "App42_d929f477-3517-4131-a46d-c2826b701f8d",
"payLoad": "Message Description",
"messageId": "ID:SHEPHERTZ-59712-1336725186689-0:30:24:1:1"
}
}
}
}
}
}
}
Pull message based on the correlation id.
Parameters:
Returns:
Response: Queue Object
Exception:
/* 2400 - NOT FOUND - Queue with the name '<queueName>' not found. 2402 - NOT FOUND - Queue with the name '<queueName>' and correlationId '<correlationId>' does not have any messages. */
String queueName = "MyQueue";
long receiveTimeOut = 10000;
String correlationId = "App42_d929f477-3517-4131-a46d-c2826b701f8d";
Queue queue = queueService.receiveMessageByCorrelationId(queueName, receiveTimeOut, correlationId); /* returns the Queue object. */
System.out.println("queueName is " + queue.getQueueName());
System.out.println("queueType is " + queue.getQueueType());
ArrayList<Queue.Message> messageList = queue.getMessageList();
for(Queue.Message message : messageList)
{
System.out.println("correlationId is " + message.getCorrelationId());
System.out.println("messageId is " + message.getMessageId());
System.out.println("payLoad is " + message.getPayLoad());
}
String jsonResponse = queue.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"queues": {
"queue": {
"queueName": "MyQueue",
"queueType": "PULL",
"messages": {
"message": {
"correlationId": "App42_d929f477-3517-4131-a46d-c2826b701f8d",
"payLoad": "Description of Messages",
"messageId": "ID:SHEPHERTZ-59712-1336725186689-0:30:24:1:1"
}
}
}
}
}
}
}
Remove message from the queue based on the message id.
Note: Once the message is removed it cannot be pulled.
Parameters:
Returns:
Response: App42Response Object
Exception:
/* 2400 - NOT FOUND - Queue with the name '<queueName>' not found. */
String queueName = "MyQueue"; String messageId = "ID:SHEPHERTZ-59712-1336725186689-0:30:24:1:1"; App42Response response = queueService.removeMessage(queueName, messageId); /* returns the App42Response object. */ boolean success = response.isResponseSuccess(); String jsonResponse = response.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"queues": {
"queue": {
"messages": {
"message": {
"queueName": "MyQueue",
"messageId": "ID:SHEPHERTZ-59712-1336725186689-0:30:24:1:1"
}
}
}
}
}
}
}
Send message on the queue with an expiry. The message will expire if it is not pulled/dequeued before the expiry.
Parameters:
Returns:
Response: Queue Object
Exception:
/* 2400 - NOT FOUND - Queue with the name '<queueName>' not found. */
String queueName = "MyQueue";
String msg = "Its a Real Message";
long exp = 100000;
Queue queue = queueService.sendMessage(queueName, msg, exp); /* returns the Queue object. */
System.out.println("queueName is " + queue.getQueueName());
System.out.println("queueType is " + queue.getQueueType());
ArrayList<Queue.Message> messageList = queue.getMessageList();
for(Queue.Message message : messageList)
{
System.out.println("correlationId is " + message.getCorrelationId());
System.out.println("messageId is " + message.getMessageId());
System.out.println("payLoad is " + message.getPayLoad());
}
String jsonResponse = queue.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"queues": {
"queue": {
"queueName": "MyQueue",
"queueType": "PULL",
"messages": {
"message": {
"correlationId": "App42_d929f477-3517-4131-a46d-c2826b701f8d",
"payLoad": "Its a Real Message"
}
}
}
}
}
}
}
Creates a type Pull Queue.
Parameters:
Returns:
Response: Queue Object
Exception:
/* 2403 - BAD REQUEST - Queue by the name '<queueName>' already exists. */
String queueName = "MyQueue";
String queueDescription = "Hi its testing Time";
Queue queue = queueService.createPullQueue(queueName, queueDescription); /* returns the Queue object. */
System.out.println ("queueName is "+ queue.getQueueName () );
System.out.println ("queueType is "+ queue.getQueueType () );
System.out.println ("queueDescription is "+ queue.getDescription() );
String jsonResponse = queue.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"queues": {
"queue": {
"queueName": "MyQueue",
"queueType": "pull",
"description": "Hi its testing Time"
}
}
}
}
}
Deletes the Pull type Queue.
Parameters:
Returns:
Response: App42Response Object
Exception:
/* 2400 - NOT FOUND - Queue with the name '<queueName>' not found. */
String queueName = "MyQueue"; App42Response response = queueService.deletePullQueue(queueName); /* returns the App42Response object. */ boolean success = response.isResponseSuccess(); String jsonResponse = response.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"queues": {
"queue": {
"queueName": "MyQueue",
"queueType": "pull",
"description": "Queue Description"
}
}
}
}
}
Messages are retrieved and dequeued from the Queue.
Parameters:
Returns:
Response: Queue Object
Exception:
/* 2400 - NOT FOUND - Queue with the name '<queueName>' not found. */
String queueName = "MyQueue";
long receiveTimeOut = 10000;
Queue queue = queueService.getMessages(queueName, receiveTimeOut); /* returns the Queue object. */
System.out.println("queueName is " + queue.getQueueName());
System.out.println("queueType is " + queue.getQueueType());
ArrayList<Queue.Message> messageList = queue.getMessageList();
for(Queue.Message message : messageList)
{
System.out.println("correlationId is " + message.getCorrelationId());
System.out.println("messageId is " + message.getMessageId());
System.out.println("payLoad is " + message.getPayLoad());
}
String jsonResponse = queue.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"queues": {
"queue": {
"queueName": "MyQueue",
"queueType": "PULL",
"messages": {
"message": {
"correlationId": "App42_d929f477-3517-4131-a46d-c2826b701f8d",
"payLoad": "Hi its Queue Description",
"messageId": "ID:SHEPHERTZ-59712-1336725186689-0:30:24:1:1"
}
}
}
}
}
}
}
Messages which are pending to be dequeue.
Note: Calling this method does not
dequeue the messages in the Queue. The messages stay in the Queue till they are dequeued
Parameters:
Returns:
Response: Queue Object
Exception:
/* 2400 - NOT FOUND - Queue with the name '<queueName>' not found. 2405 - NOT FOUND - Queue with the name '<queueName>' does not have any pending messages. */
String queueName = "MyQueue";
Queue queue = queueService.pendingMessages(queueName); /* returns the Queue object. */
System.out.println("queueName is " + queue.getQueueName());
System.out.println("queueType is " + queue.getQueueType());
ArrayList<Queue.Message> messageList = queue.getMessageList();
for(Queue.Message message : messageList)
{
System.out.println("correlationId is " + message.getCorrelationId());
System.out.println("messageId is " + message.getMessageId());
System.out.println("payLoad is " + message.getPayLoad());
}
String jsonResponse = queue.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"queues": {
"queue": {
"queueName": "MyQueue",
"queueType": "PULL",
"messages": {
"message": {
"correlationId": "App42_d929f477-3517-4131-a46d-c2826b701f8d",
"payLoad": "DESCRIPTION",
"messageId": "ID:SHEPHERTZ-59712-1336725186689-0:30:24:1:1"
}
}
}
}
}
}
}
Purges message on the Queue.
Note: once the Queue is purged the messages
are removed from the Queue and wont be available for dequeuing.
Parameters:
Returns:
Response: App42Response Object
String queueName = "MyQueue"; App42Response response = queueService.purgePullQueue(queueName); /* returns the Queue object. */ boolean success = response.isResponseSuccess(); String jsonResponse = response.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"queues": {
"queue": {
"queueName": "MyQueue",
"queueType": null,
"description": "Queue Description"
}
}
}
}
}