App42 Cloud API

J2ME(S40) Cloud SDK

Message

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 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 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 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.

public Queue receiveMessage(String queueName, long receiveTimeOut) throws App42Exception

Pull all the message from the queue.

Parameters:

queueName - The name of the queue from which messages have to be pulled
receiveTimeOut - Receive time out(in milli seconds)

Returns:

Queue object containing messages which have been pulled

Response: Queue Object

Exception:

This method throws following exception.
/*
2400 - NOT FOUND - Queue with the name '<queueName>' not found.
2401 - NOT FOUND - Queue with the name '<queueName>' does not have any messages.  
*/ 		          
For more details click Exception
String queueName ="MyQueue";
long receiveTimeOut = 10000;

Queue queue = queueService.receiveMessage(queueName, receiveTimeOut);  /* returns the Queue object. */ 
System.out.println ("queueName is "+ queue.getQueueName () );
Vector messageList = queue.getMessageList(); 
for(int i=0;i< messageList.size();i++)
{
   	Queue.Message message = (Queue.Message) messageList.elementAt(i);
   	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"
                        }
                    }
                }
            }
        }
    }
}
public Queue receiveMessageByCorrelationId(String queueName, long receiveTimeOut, String correlationId) throws App42Exception

Pull message based on the correlation id.

Parameters:

queueName - The name of the queue from which the message has to be pulled
receiveTimeOut - Receive time out(in milli seconds)
correlationId - Correlation Id for which message has to be pulled

Returns:

Queue containing message which has pulled based on the correlation id

Response: Queue Object

Exception:

This method throws following 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. 
*/ 		          
For more details click Exception
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 () );
Vector messageList = queue.getMessageList(); 
for(int i=0;i< messageList.size();i++)
{
   	Queue.Message message = (Queue.Message) messageList.elementAt(i);
   	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"
                        }
                    }
                }
            }
        }
    }
}
public App42Response removeMessage(String queueName, String messageId) throws App42Exception

Remove message from the queue based on the message id.
Note: Once the message is removed it cannot be pulled.

Parameters:

queueName - The name of the queue from which the message has to be removed
messageId - The message id of the message which has to be removed

Returns:

App42Response if removed successfully

Response: App42Response Object

Exception:

This method throws following exception.
/*
2400 - NOT FOUND - Queue with the name '<queueName>' not found.

*/ 		         
For more details click Exception
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"
                        }
                    }
                }
            }
        }
    }
}
public Queue sendMessage(String queueName, String msg, long exp) throws App42Exception

Send message on the queue with an expiry. The message will expire if it is not pulled/dequeued before the expiry.

Parameters:

queueName - The name of the queue to which he message has to be sent
msg - Message that has to be sent
exp - Message expiry time

Returns:

Queue object containing message which has been sent with its message id and correlation id

Response: Queue Object

Exception:

This method throws following exception.
/*
2400 - NOT FOUND - Queue with the name '<queueName>' not found.
*/ 		  
For more details click Exception
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 ());  
Vector messageList = queue.getMessageList(); 
for(int i=0;i< messageList.size();i++)
{
   	Queue.Message message = (Queue.Message) messageList.elementAt(i);
   	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"
                        }
                    }
                }
            }
        }
    }
}
public Queue createPullQueue(String queueName, String queueDescription) throws App42Exception

Creates a type Pull Queue.

Parameters:

queueName - The name of the queue which has to be created
queueDescription - The description of the queue

Returns:

Queue object containing queue name which has been created

Response: Queue Object

Exception:

This method throws following exception.
/*
2403 - BAD REQUEST - Queue by the name '<queueName>' already exists.
*/ 		        
For more details click Exception
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"
                }
            }
        }
    }
}
public App42Response deletePullQueue(String queueName) throws App42Exception

Deletes the Pull type Queue.

Parameters:

queueName - The name of the queue which has to be deleted

Returns:

App42Response if deleted successfully

Response: App42Response Object

Exception:

This method throws following exception.
/*
2400 - NOT FOUND - Queue with the name '<queueName>' not found.
*/ 		 
For more details click Exception
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"
                }
            }
        }
    }
}
public Queue getMessages(String queueName, long receiveTimeOut) throws App42Exception

Messages are retrieved and dequeued from the Queue.

Parameters:

queueName - The name of the queue which have to be retrieved
receiveTimeOut - Receive timeout(in milli seconds)

Returns:

Queue object containing messages in the Queue

Response: Queue Object

Exception:

This method throws following exception.
/*
2400 - NOT FOUND - Queue with the name '<queueName>' not found.*/ 		        
For more details click Exception
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());
Vector messageList = queue.getMessageList(); 
for(int i=0;i< messageList.size();i++)
{
   	Queue.Message message = (Queue.Message) messageList.elementAt(i);
   	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"
                        }
                    }
                }
            }
        }
    }
}
public Queue pendingMessages(String queueName) throws App42Exception

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:

queueName - The name of the queue from which pending messages have to be fetched

Returns:

Queue object containing pending messages in the Queue

Response: Queue Object

String queueName = "MyQueue";
		  
Queue queue = queueService.pendingMessages(queueName);  /* returns the Queue object. */ 
System.out.println ("queueName is "+ queue.getQueueName ()); 
System.out.println ("queueDescription is "+ queue.getQueueDescription ()); 
Vector messageList = queue.getMessageList(); 
for(int i=0;i< messageList.size();i++)
{
   	Queue.Message message = (Queue.Message) messageList.elementAt(i);
   	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"
                        }
                    }
                }
            }
        }
    }
}
public Queue purgePullQueue(String queueName) throws App42Exception

Purges message on the Queue.
Note: once the Queue is purged the messages are removed from the Queue and wont be available for dequeueing.

Parameters:

queueName - The name of the queue which has to be purged

Returns:

Queue object containing queue name which has been purged

Response: Queue Object

String queueName = "MyQueue";
		  
Queue queue = queueService.purgePullQueue(queueName);  /* returns the Queue object. */ 
System.out.println ("queueName is "+ queue.getQueueName ()); 
System.out.println ("queueDescription is "+ queue.getQueueDescription ()); 
String jsonResponse = queue.toString(); /* returns the response in JSON format. (as shown below)*/                            
{
    "app42": {
        "response": {
            "success": true,
            "queues": {
                "queue": {
                    "queueName": "MyQueue",
                    "queueType": null,
                    "description": "Queue Description"
                }
            }
        }
    }
}