Ahoy there callbacks!

1 2 Page 2
Page 2 of 2
The send method is also asynchronous
<span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<code class='java'><span class='line'><span class="kd">private</span> <span class="kt">void</span> <span class="nf">send</span><span class="o">(</span><span class="kd">final</span> <span class="n">AmazonSQS</span> <span class="n">sqs</span><span class="o">,</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">queueURL</span><span class="o">,</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">message</span><span class="o">,</span> <span class="kd">final</span> <span class="n">MessageSentCallback</span> <span class="n">callback</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>  <span class="n">pool</span><span class="o">.</span><span class="na">execute</span><span class="o">(</span><span class="k">new</span> <span class="n">Runnable</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">run</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>      <span class="n">SendMessageResult</span> <span class="n">res</span> <span class="o">=</span> <span class="n">sqs</span><span class="o">.</span><span class="na">sendMessage</span><span class="o">(</span><span class="k">new</span> <span class="n">SendMessageRequest</span><span class="o">(</span><span class="n">queueURL</span><span class="o">,</span> <span class="n">message</span><span class="o">));</span>
</span><span class='line'>      <span class="k">if</span> <span class="o">(</span><span class="n">callback</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>        <span class="n">callback</span><span class="o">.</span><span class="na">onSend</span><span class="o">(</span><span class="n">res</span><span class="o">.</span><span class="na">getMessageId</span><span class="o">());</span>
</span><span class='line'>      <span class="o">}</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'>  <span class="o">});</span>
</span><span class='line'><span class="o">}</span>
</span></code>

Incidentally, the AWS Java SDK does provide an asynchronous client; however, this client’s implementation leverages Java’s Futures. While Futures are a neat concept, Ahoy!’s implementation is more convenient (at least for me and in the patterns of how I employ SQS) than Futures as there isn’t any polling involved once a message is sent or received.

While callbacks aren’t necessarily supported natively in Java, you can emulate them quite nicely and achieve the same level of code conciseness as what’s common in JavaScript. And if you need a handy way to interface with AWS SQS, then give Ahoy! a try! Can you dig it, man?

This story, "Ahoy there callbacks!" was originally published by JavaWorld.

Related:

Copyright © 2013 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2