1. 程式人生 > >AWS Resource APIs for SNS and SQS

AWS Resource APIs for SNS and SQS

Last week we released version 0.0.3 of the AWS Resource APIs for Java, adding support for the Amazon Simple Queue Service (SQS) and Amazon Simple Notification Service (SNS). SNS and SQS are similar services that together provide a fully-managed cloud messaging platform. These services expose two powerful primitives — Topics and Queues — which let you decouple message producers from message consumers. The Resource APIs for SNS and SQS make it easier than ever to use these two services. Enough chit-chat, let’s see some code!

Amazon SNS — Topics

SNS is used for multicast messaging. Consumers subscribe to a "Topic," and messages published to the Topic are pushed to all current subscribers. The resource API for SNS exposes a resource object representing a Topic, giving you convenient methods for managing subscriptions to the topic and publishing messages to the topic. It also exposes resource objects for PlatformApplications

and PlatformEndpoints, which are used to integrate with various mobile push services. This example demonstrates creating a new Topic, adding a couple of subscribers, and publishing a message to the topic.

SNS sns = ServiceBuilder.forService(SNS.class).build();

// Create a new topic.
Topic topic = sns.createTopic("MyTestTopic");
try {

    // Subscribe an email address.
    topic.subscribe("
[email protected]
", "email"); // Subscribe an HTTPS endpoint. topic.subscribe("https://api.example.com/notify?user=david", "https"); // Subscribe all of the endpoints from a previously-created // mobile platform application. PlatformApplication myMobileApp = sns.getPlatformApplication("arn:aws:..."); for (PlatformEndpoint endpoint : myMobileApp.getEndpoints()) { topic.subscribe(endpoint.getArn(), "application"); } // Publish a message to all of the subscribers. topic.publish("Hello from Amazon SNS!"); } finally { // Clean up after ourselves. topic.delete(); }

Amazon SQS — Queues

SQS is used for reliable anycast messaging. Producers write messages to a "Queue," and consumers pull messages from the queue; each message is delivered to a single consumer[1]. The resource API for SQS exposes a Queue resource object, giving you convenient methods for sending messages to a queue and receiving messages from the queue. This example demonstrates creating a queue, sending a couple of messages to it, and then reading those messages back out.

SQS sqs = ServiceBuilder.forService(SQS.class).build();

// Create a new queue.
Queue queue = sqs.createQueue("MyTestQueue");
try {

    // Configure the queue for more efficient long-polling.
    queue.setAttributes(Collections.singletonMap(
            "ReceiveMessageWaitTimeSeconds",
            "20"));

    // Send it a couple messages.
    for (int i = 0; i < 10; ++i) {
        queue.sendMessage("Hello from Amazon SQS: " + i);
    }

    while (true) {
        // Pull a batch of messages from the queue for processing.
        List<Message> messages = queue.receiveMessages();
        for (Message message : messages) {
            System.out.println(message.getBody());

            // Delete the message from the queue to acknowledge that
            // we've successfully processed it.
            message.delete();
        }
    }

} finally {
    // Clean up after ourselves.
    queue.delete();
}

Conclusion

Using SNS or SQS, or interested in getting started with them? Give these new resource APIs a try and let us know what you think, either here or via GitHub issues!

[1] To be precise, it’s delivered to at least one consumer; if the first consumer who reads it does not delete the message from the queue in time (whether due to failure or just being slow), it’ll eventually be delivered to another consumer.

相關推薦

AWS Resource APIs for SNS and SQS

Last week we released version 0.0.3 of the AWS Resource APIs for Java, adding support for the Amazon Simple Queue Service (SQS) and Amazon Simple

AWS Marketplace support for clusters and AWS resources

With the HPE Vertica Analytics Platform for AWS you get enterprise-class Big Data analytics for all your data. It consists of a mass

在Python中使用awssnssqs

github scribe bsp 情況下 分布式 int 方法 test devel 首先,sns = Simple Notification Service,sqs = Simple Queue Service sns與sqs有什麽不同? (ref:https://st

AWS Marketplace: TIBCO Jaspersoft Reporting and Analytics for AWS (BYOL)

TIBCO Jaspersoft for AWS is a commercial open source reporting and analytics server built for AWS that can run standalone or be embedded in your a

AWS Marketplace: TIBCO Jaspersoft Reporting and Analytics for AWS (Hourly)

Product Overview TIBCO Jaspersoft for AWS is a commercial open source reportin

AWS Marketplace: TIBCO Jaspersoft Reporting and Analytics for AWS

AWS Marketplace is hiring! Amazon Web Services (AWS) is a dynamic, growing business unit within Am

AWS Programs for Research and Education

Amazon Web Services is Hiring. Amazon Web Services (AWS) is a dynamic, growing business unit within Amazon.com. We are currently hiring So

AWS Marketplace: Sumo Logic Machine Data Analytics for Logs and Metrics (Monthly)

AWS Marketplace is hiring! Amazon Web Services (AWS) is a dynamic, growing business unit within Am

Get Docker for CentOS and Installing Docker

man inux can all pac nload require sys ont 1、OS requirements To install Docker, you need the 64-bit version of CentOS 7. 2、Uninstall old

11gR2(11.2) RAC TAF Configuration for Admin and Policy Managed Databases (文檔 ID 1312749.1)

style manual ogre version sessions adf from concept ould In this Document Purpose Scope Details Concepts

[Nuxt] Load Data from APIs with Nuxt and Vuex

his pro -- http template map https etc not run In a server-rendered application, if you attempt to load data before the page renders and

[Nuxt] Use Vuex Actions to Delete Data from APIs in Nuxt and Vue.js

export begin async delet tin remove todo ras alt You‘ll begin to notice as you build out your actions in Vuex, many of them will look qui

解決 struts2 報"There is no Action mapped for namespace / and action name"的問題

https def col demo 解決 ack ges 裏的 dex 在配置struts2 入門demo時碰到"There is no Action mapped for namespace / and action name"問題,查了一些資料,但沒有完全解決問題,

There is no Action mapped for namespace [/] and action name [Login] associated

pat rop back 圖片 src 兩個 ont person 大小 使用struts2時出現了: There is no Action mapped for namespace [/] and action name [Login] associated 兩個可能的

AWS學習筆記(七)--集成SQS和Lambda

n) eip mha 調整 scope 根據 直接 ast fun 本文介紹了集成SQS和Lambda的方法,代碼基於JAVA SDK。 POM配置 <dependencies> <dependency> <ar

【bug】使用spring+struts2註解開發,提示” There is no Action mapped for namespace [/] and action name [xxxxx.action] associated with context path []”

there 提示 就是 技術分享 文件夾路徑 iat class bubuko ring 使用註解開發,通過spring管理struts2容器,配置文件沒有問題,前臺路徑無誤 錯誤提示: There is no Action mapped for namespace [/]

Best Practices for QML and Qt Quick

ins proto IT fault qmake scala simple text view Despite all of the benefits that QML and Qt Quick offer, they can be challenging in certa

Indy 10.5.8 for Delphi and Lazarus 修改版(2011)

troy lock 選擇 單用戶 tcp客戶端 現象 域名 在線 enter Indy 10.5.8 for Delphi and Lazarus 修改版(2011) Internet Direct(Indy)是一組開放源代碼的Internet組件,涵蓋了幾乎所有流行

Memory Layout for Multiple and Virtual Inheritance

virt git nbsp multiple rdp csp www. amp sch https://analyticspace.wordpress.com/2016/07/page/3/ https://zhougy0717.github.io/2018/03/06/