Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send and receive a Java Object from Amazon SQS

Tags:

amazon-sqs

How do I send and receive a Java Object from SQS? For instance, I have a java object Log. I send the object to the message queue as

this.getSqs().sendMessage(new SendMessageRequest(myQueueUrl, log.toString());

However, at the time of retrieving the message from queue, I want to be able to retrieve it as List<Log> and use it as a java Log object inside my application. Any pointers on how to do that?

like image 452
user2890683 Avatar asked Oct 21 '22 18:10

user2890683


1 Answers

Id use Gson to serialize and deserialize pojo's to strings

you would send the message above as

sendMessage(new SendMessageRequest(myQueueUrl,log.toString());

then when you get a List<Messages> messages = sns.read();
for(Message m:messages){
    String json= m.getBody();
    Gson g = new Gson();
    Log l = g.fromJson(json,Log.class);
}
like image 66
Sahan Jayasumana Avatar answered Jan 02 '23 20:01

Sahan Jayasumana