Showing posts with label tech. Show all posts
Showing posts with label tech. Show all posts

Thursday, March 24, 2011

AWS: How do you subscribe an SQS queue to an SNS topic?

Image representing Amazon Web Services as depi...Image via CrunchBase
If there are any AWS (Amazon Web Services) gurus out there, I'm stumped on trying to use a Simple Queue Service queue to receive notifications sent out on a Simple Notification Service topic. Any pointers?

In the code below, I'm using the AWS SDK for Java (available here). The program does the following:


  1. Create an SNS topic.
  2. Set a policy on the topic to allow subscriptions.
  3. Create an SQS queue.
  4. Set a policy on the queue to allow message posting.
  5. Subscribe the queue to the topic.
  6. Send an SNS notification message.
  7. Listen for the message on the queue.
Steps 1-6 seem to work just fine, but on step 7, it listens in vain for messages. All ten iterations go by without picking up any messages on the queue. As an added note, if I subscribe an email address to the topic, messages are delivered. Only the queue appears to be bypassed when SNS sends its notifications.


public class SnsWithSqsDemo {
private static final String TOPIC_NAME = "TestTopic";
private static final String QUEUE_NAME = "TestQueue";

/**
* This program demonstrates how to hook up an AWS SQS queue to an AWS SNS
* topic to receive messages.
*
* 1. Create an SNS topic.
* 2. Set a policy on the topic to allow subscriptions.
* 3. Create an SQS queue.
* 4. Set a policy on the queue to allow message posting.
* 5. Subscribe the queue to the topic.
* 6. Send an SNS notification message.
* 7. Listen for the message on the queue.
*
* @param args not used
*/
public static void main(String[] args) {
AWSCredentials awsCredentials = null;
AmazonSNS sns = null;
AmazonSQS sqs = null;
String topicArn = null;
String queueUrl = null;
String queueArn = null;
String subscriptionArn = null;
try {
awsCredentials =
new PropertiesCredentials(
SnsWithSqsDemo.class.getResourceAsStream("AwsCredentials.properties"));
sns = new AmazonSNSClient(awsCredentials);
// 1. Create a topic
CreateTopicResult ctResult =
sns.createTopic(new CreateTopicRequest(TOPIC_NAME));
topicArn = ctResult.getTopicArn();
System.out.println(String.format("Created topic %s with ARN %s",
TOPIC_NAME, topicArn));
// 2. Set policy on topic to allow open subscriptions
Policy snsPolicy =
new Policy().withStatements(
new Statement(Effect.Allow)
.withPrincipals(Principal.AllUsers)
.withActions(SNSActions.Subscribe));
// ??? Is there no Java SDK constant for "Policy"?
sns.setTopicAttributes(new SetTopicAttributesRequest(
topicArn, "Policy", snsPolicy.toJson()));
// 3. Create a queue
sqs = new AmazonSQSClient(awsCredentials);
CreateQueueResult cqResult =
sqs.createQueue(new CreateQueueRequest(QUEUE_NAME));
queueUrl = cqResult.getQueueUrl();
System.out.println(String.format("Created queue %s with URL %s",
QUEUE_NAME, queueUrl));
// 4. Set the queue policy to allow SNS to publish messages
Policy sqsPolicy =
new Policy().withStatements(
new Statement(Effect.Allow)
.withPrincipals(Principal.AllUsers)
.withActions(SQSActions.SendMessage)
.withConditions(ConditionFactory.newSourceArnCondition(topicArn)));
Map queueAttributes = new HashMap();
queueAttributes.put("Policy", sqsPolicy.toJson());
sqs.setQueueAttributes(new SetQueueAttributesRequest(queueUrl, queueAttributes));
// ??? Why isn't the queue ARN in the queue attributes?
AmazonIdentityManagement idMgr = new AmazonIdentityManagementClient(awsCredentials);
String accountId = idMgr.getUser().getUser().getUserId();
queueArn = String.format("arn:aws:sqs:us-east-1:%s:%s", accountId, QUEUE_NAME);
// 5. Subscribe the queue to the topic
SubscribeResult sResult =
sns.subscribe(new SubscribeRequest(topicArn, "sqs", queueArn));
subscriptionArn = sResult.getSubscriptionArn();
System.out.println("Subscription ARN: " + subscriptionArn);
// 5.5. Wait a bit for AWS to get all synched-up
Thread.sleep(60000L);
// 6. Send a notification
PublishResult pResult =
sns.publish(new PublishRequest(topicArn,
"Mr Watson -- Come here -- I want to see you."));
System.out.println("Sent message ID = " + pResult.getMessageId());
// 7. Wait for message receipt in queue
for (int i = 0; i < 10; i++) { Thread.sleep(2000L); ReceiveMessageResult rmResult = sqs.receiveMessage(new ReceiveMessageRequest(queueUrl)); if (rmResult.getMessages().size() > 0) {
// A message has been received
for (Message message : rmResult.getMessages()) {
System.out.println(message.getBody());
sqs.deleteMessage(new DeleteMessageRequest(queueUrl,
message.getReceiptHandle()));
}
break;
} else {
System.out.println("No messages available, attempt " + (i+1));
}
}
} catch (Exception e) {
e.printStackTrace(System.err);
} finally {
// Unsubscribe the queue from the topic
if (sns != null && subscriptionArn != null) {
sns.unsubscribe(new UnsubscribeRequest(subscriptionArn));
System.out.println("Unsubscribed queue from topic.");
}
// Destroy queue
if (sqs != null && queueUrl != null) {
sqs.deleteQueue(new DeleteQueueRequest(queueUrl));
System.out.println("Deleted the queue.");
sqs.shutdown();
}
// Destroy topic
if (sns != null && topicArn != null) {
sns.deleteTopic(new DeleteTopicRequest(topicArn));
System.out.println("Deleted the topic.");
sns.shutdown();
}
}
}

}


Enhanced by Zemanta

Saturday, February 19, 2011

2011: My Android Year

Android robot logo.Image via Wikipedia
Gadget journalism on the Web seems to be held to the same standard as entertainment journalism, meaning rumors, guesswork and hype make up most of my Google News alerts and Twitter feeds. But (like entertainment journalism, to those who follow it), the hype is strangely addictive. And the hype isn't just about a new device hitting the shelves, but upcoming announcements about devices that won't see a shelf for months, or even announcements about upcoming announcements. It starts with leaked device code-names or Photoshopped concept images, then there are unconfirmed spec sheets and rumored release dates, followed by official teasers and then trade show releases (with the hands-on videos and device comparison sheets). Finally, YouTube starts getting the gadget version of amateur porn: the unboxing videos. It's an information strip-tease, showing just enough to catch the attention and drive the imagination wild with what isn't revealed.

I'm not an early adopter of technology, and when something starts to get exciting, I research it for months before I make the jump. I've been carrying around a Windows Mobile phone (the HTC Touch Pro 2), top-class technology that integrated well with work when I bought it, but it seems that Microsoft as a mobile platform is moribund, while Android is the new market share powerhouse. In early 2010, I started following rumors of a beast of an Android phone called the HTC "Scorpion" and for the next year, I submerged myself in Android hype. By January's Consumer Electronics Show, I was sold on two or three Android devices scheduled to release in the first half of the year.

I'd followed a rumored Motorola Android tablet code-named "Stingray," which was introduced at CES as the "Xoom." The Xoom is the hardware reference for Google's new release of Android, version 3.0 dubbed "Honeycomb," and it boasts a dual-core processor, rear- and front-facing cameras for video chats and HDMI output among its features. When the iPad came out, I couldn't understand where it fit in the gadget ecosystem: was it a bulky smartphone (sans calls), an underpowered laptop or a high-eyestrain eBook reader? Then, as I moved many of my computer activities into the cloud, live-blogged from geek conventions and trips, and grew weary of taking my laptop out of my carry-on at airport security, I began to realize that the tablet was the ideal travel laptop. The more I researched the applications and accessories (like a Bluetooth keyboard) that would be available, the more the tablet's niche solidified:

  • Live-blogging
  • Travel laptop that stays in the carry-on, through security
  • Garmin navigator replacement
  • Stream media (Netflix, Hulu, etc.) to any TV
  • SSH into my work computers and Amazon EC2 instances
  • Portable movies
  • Document editing (with Google Docs)

As of this writing, the Xoom is supposed to release on February 24, 2011. But that information comes from the gadget rags: Motorola and Verizon are silent on the topic.

As for the Android phone, up until CES 2011, I was looking forward to the HTC Thunderbolt, billed as "Not your dream phone. The one after that." It's sleek like the Nexus One, thin but with a huge 4.3" screen. It's the first "4G" phone on Verizon's LTE network. But Motorola came to CES with a surprise: the heretofore un-hyped (and unheard of) DROID Bionic, with a dual-core processor (the Thunderbolt's is single-core), more on-board storage and an HDMI-out port. The Bionic's case is not as pretty as the Thunderbolt's, but it seems to be more of a powerhouse. Even if Android 2.2 doesn't utilize dual core processors (and that's a big IF -- I haven't seen it categorically demonstrated that this is the case), Android 2.4 will, and software always evolves to push the hardware. The Bionic will have staying power. My wife surprised me by turning down an iPhone to replace her dying BlackBerry Storm, so I'm going to get her the Thunderbolt and stick with my Windows Mobile phone until the Bionic comes out. I'll be able to do some real comparisons then.

Now, I wait. Neither Verizon, nor HTC nor Motorola have made an official statement about release dates, but the current rumor-mill has the Motorola Xoom releasing on February 24 2011, the HTC Thunderbolt releasing on February 28 2011, and the Motorola DROID Bionic releasing in June 2011. After that, I'll have to wean myself off of Android hype news, and enjoy what I have instead of lusting after the next cool thing.
Enhanced by Zemanta

Friday, August 20, 2010

Location, Location, Location

I used to be a regular at a bar near my apartment, "Garret Ripley's".  I'd go there almost every day.  Even when alone, I'd go to read or write over a couple of pints of Boddington's.  The owners and staff knew me by name, and I knew them; someone would start pouring my Boddy's the minute I walked in the door.  Cute servers would chat with me between rounds like we were old friends.  It felt special.  And that feeling of being special, as much as the Boddington's, kept me coming back.

This week, Facebook launched its "Places" feature, in line with social networking technologies like FourSquare and Gowalla.  You, with your GPS-enabled mobile phone, can "check in" to the places you vsit, broadcasting your city-crawling lifestyle to your friends.  You're also rewarded for "accomplishments" both wide (you've visited the most places in your neighborhood) and deep (you're the most regular of the regulars at your favorite pub).  Now, feeling special is easier than ever: these services will express admiration of your worldliness, and show appreciation of your repeat business -- all while providing businesses with valuable customer intelligence as outlined in the fine print of their privacy policies.

Commentators on the State Of Society have the same critique of these technologies as they had for blogging, Facebook and Twitter: they feed our narcissism, they substitute stunted forms of interaction for real human interaction, they publicize things better kept private, they create a barrier to "authentic" experience...  There's truth in all those notions, but that doesn't interest me much.  These aren't technologies that are aimed at solving particular problems, they're technologies that are looking for a use.  And while the initial applications are sometimes bizarre, it's interesting to see the uses people do find for them, and how that causes the technology to evolve.

FourSquare and Gowalla users can leave their friends messages, retrievable only when their friends check into the location it was left.  The messages can be useful, like "try the corn bisque -- it's dynamite", or just fun Kilroy-was-here markers, like finding a friend's initials carved into your school desk.  As the things you can leave for your friends to find become more sophisticated and multi-media, entire parallel dimensions of reality will be created, accessible through your mobile devices, where the public, physical world is only one part of what you can discover.  All the old haunts are new again.  It's easy to imagine "Dungeons & Dragons" types of games, played in a virtual overlay on the physical world.  "London Below", the hidden world existing sideways to the real London from Neil Gaiman's book Neverwhere, seems prophetic.

Of course, if you're technically savvy, there's no reason to even leave your house to explore this virtual world. I have a friend who writes applications against the FourSquare API such that he has generated phantom digital proxies of himself who spend all day wandering plausible routes (plausible, to prevent FourSquare from noticing it isn't a real person, and blocking it) and checking into places around town.  He describes it as the "game underneath the game" of FourSquare.

Digital realities and phantoms who wander a world similar to our own, but invisible without the proper gear and permission.  The craziest science fiction is coming true in our lifetime.

Do you check into places using social media?  If so, what do you enjoy about it?  How would you like to see this develop?  If not, what's your biggest turn-off about it?  What changes would get you to use a location check-in system?
Enhanced by Zemanta