AMAZON SNS(1)Set up SNS to SQS and JAVA API

本文详细介绍了如何通过Amazon SNS直接创建主题并将其与SQS队列集成,以及如何使用Java API进行消息发送与接收。包括创建主题、队列、订阅消息、使用SNS与SQS之间的消息传递流程,并提供了关键代码片段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AMAZON SNS(1)Set up SNS to SQS and JAVA API

1. Create a Topic on SNS
Directly go to the console and follow the buttons and labels.
https://console.aws.amazon.com/sns/v2/home?region=us-east-1#/home

2. Create the SQS Queue
Directly go to the console and follow the buttons and labels
https://console.aws.amazon.com/sqs/home?region=us-east-1

3. Subscribe the Message from SNS for SQS
Following this document
http://docs.aws.amazon.com/zh_cn/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqssubscribe.html
On the SQS side, we can multiple select 3 SQS I need and subscribe the SNS.

On the SNS side, I can click on the name of the SNS to confirm/delete the subscription.

4. Java API
http://docs.aws.amazon.com/zh_cn/sns/latest/dg/using-awssdkjava.html

Here is the SNSMessage.scala Codes
package com.sillycat.jobsconsumer.messagequeue

import com.amazonaws.auth.{BasicAWSCredentials}
import com.amazonaws.services.sns.{AmazonSNSAsyncClient, AmazonSNSClient}
import com.sillycat.jobsconsumer.utilities.{IncludeConfig, IncludeLogger}
import com.amazonaws.regions.Region
import com.amazonaws.regions.Regions

object SNSMessage extends IncludeLogger with IncludeConfig{

private def getCredential = {
new BasicAWSCredentials(
config.getString(envStr("sns.keyId")),
config.getString(envStr("sns.accessKey")))
}

def getClient = {
val snsClient:AmazonSNSClient = new AmazonSNSClient(getCredential)
snsClient.setRegion(Region.getRegion(Regions.fromName(config.getString(envStr("sns.region")))))
snsClient
}

def getAsyncClient = {
val snsClient:AmazonSNSClient = new AmazonSNSAsyncClient(getCredential)
snsClient.setRegion(Region.getRegion(Regions.fromName(config.getString(envStr("sns.region")))))
snsClient
}

}

The Trait Class, IncludeSNSProducer.scala
package com.sillycat.jobsconsumer.messagequeue.producer

import com.amazonaws.services.sns.model.{PublishResult, PublishRequest}
import com.sillycat.jobsconsumer.messagequeue.{SNSMessage}
import com.sillycat.jobsconsumer.utilities.{IncludeConfig, IncludeLogger}

trait IncludeSNSProducer extends IncludeLogger with IncludeConfig {

protected def topicArn = "default"

def sendMessage(sourceMsg: String): PublishResult = {
val publishRequest:PublishRequest = new PublishRequest(topicArn, prepareMessage(sourceMsg))
val publishResult:PublishResult = SNSMessage.getClient.publish(publishRequest)
publishResult
}

protected def prepareMessage(sourceMsg:Any):String

}

The test class, SNSMessageSpec.scala
package com.sillycat.jobsconsumer.messagequeue

import com.amazonaws.services.sns.model.PublishRequest
import com.amazonaws.services.sns.{AmazonSNSClient}
import com.sillycat.jobsconsumer.utilities.{IncludeLogger, IncludeConfig}
import org.scalatest.{BeforeAndAfterAll, Matchers, FunSpec}

class SNSMessageSpec extends FunSpec with Matchers with BeforeAndAfterAll with IncludeConfig with IncludeLogger{

var client: AmazonSNSClient= _

var topicArn: String = config.getString(envStr("sns.topicArn.classifiedjobs"))

override def beforeAll(): Unit = {
if(config.getString("build.env").equals("test")){
logger.info("No embedded SNS, ignore the test")
}else{
client = SNSMessage.getClient
}
}

override def afterAll() {
}

describe("IncludeSNSProducer") {
describe("sendMessage"){
it("Directly Send String") {
if(config.getString("build.env").equals("test")){
logger.info("No embedded SNS, ignore the test")
}else{
val expect = "134343143"
val publishRequest:PublishRequest = new PublishRequest(topicArn, "134343143")
val result = client.publish(publishRequest)
result.getMessageId should not be (null)
}
}
}
}

}

References:
http://docs.aws.amazon.com/zh_cn/sns/latest/dg/SNSMobilePushBaiduAPI.html
https://aws.amazon.com/cn/sns/getting-started/
http://docs.aws.amazon.com/sns/latest/dg/SendMessageToSQS.html
http://docs.aws.amazon.com/zh_cn/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqssubscribe.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值