Java 在Play框架上调用Amazon通知服务
我想使用Play框架从Amazon简单通知服务(SNS)发送一条消息,我读到我必须使用WS-for REST API。然而,我对如何做到这一点感到困惑。有人能帮我解决这个问题吗?首先,您需要通过在build.sbt中添加以下行将SNS SDK添加到您的项目中:Java 在Play框架上调用Amazon通知服务,java,playframework,Java,Playframework,我想使用Play框架从Amazon简单通知服务(SNS)发送一条消息,我读到我必须使用WS-for REST API。然而,我对如何做到这一点感到困惑。有人能帮我解决这个问题吗?首先,您需要通过在build.sbt中添加以下行将SNS SDK添加到您的项目中: libraryDependencies += "com.amazonaws" % "aws-java-sdk-sns" % "1.11.271" 然后通过将服务注入控制器来使用服务,您的服务应该类似于: @Singleton publi
libraryDependencies += "com.amazonaws" % "aws-java-sdk-sns" % "1.11.271"
然后通过将服务注入控制器来使用服务,您的服务应该类似于:
@Singleton
public final class AmazonSNSService {
// logging always a good thing to do
private final Logger.ALogger logger = Logger.of(this.getClass());
// this is what you have to use
private final com.amazonaws.services.sns.AmazonSNS snsClient;
private final String AMAZON_ARN;
@Inject
public AmazonSNSService(Configuration configuration) { // or Config if play 2.6 and later
// I set the aws config in application.conf and I read them here
final String AWS_ACCESS_KEY = configuration.getString("aws_access_key");
final String AWS_SECRET_KEY = configuration.getString("aws_secrect_key");
snsClient = AmazonSNSClient.builder()
.withRegion(Regions.US_EAST_1)
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY)))
.build();
AMAZON_ARN = configuration.getString("amazon_arn_config_key");
}
}
然后,您可以使用snsClient
和您想要的方法来创建主题:
public void createTopic(String topicName) {
String topicARN = AMAZON_ARN + ":" + topicName;
if (doesTopicExists(topicARN)) {
logger.debug("TopicArn - already Exists" + topicARN);
} else {
//create a new SNS topic
CreateTopicRequest createTopicRequest = new CreateTopicRequest(topicName);
CreateTopicResult createTopicResult = snsClient.createTopic(createTopicRequest);
//get request id for CreateTopicRequest from SNS metadata
ResponseMetadata topicResponse = snsClient.getCachedResponseMetadata(createTopicRequest);
logger.debug("CreateTopicArn - " + createTopicResult.getTopicArn());
}
}
还有另一个订阅主题的示例:
public String subscribeToTopic(String topicName, String deviceEndpointARN) {
String topicARN = AMAZON_ARN + ":" + topicName;
//if topic does not exists create topic then subscribe
if (!doesTopicExists(topicARN)) {
createTopic(topicName);
}
return subscribeToTopic(topicARN, deviceEndpointARN);
}
想进入主题吗
public void publishToTopic(String message, String topicName) {
String topicARN = AMAZON_ARN + ":" + topicName;
//if topic does not exists create topic then publish to topic
// or throw an exception, maybe it does not make sense to push to the topic that have no subscribers at all.
if (!doesTopicExists(topicARN)) {
createTopic(topicName);
}
//publish to an SNS topic
PublishRequest publishRequest = new PublishRequest(topicARN, message);
publishRequest.setMessageStructure("json");
PublishResult publishResult = snsClient.publish(publishRequest);
//print MessageId of message published to SNS topic
logger.debug("Push Notification sent to TOPIC [" + topicARN + "] MessageId - [" + publishResult.getMessageId() + "] Message Body: " + message);
}
这就是我检查主题是否存在的方式:
private boolean doesTopicExists(String topicARN) {
String nextToken = null;
do {
ListTopicsRequest request = new ListTopicsRequest();
request.setNextToken(nextToken);
ListTopicsResult listTopicsResult = snsClient.listTopics();
List<Topic> topics = listTopicsResult.getTopics();
for (Topic topic : topics) {
if (topic.getTopicArn().equals(topicARN)) {
return true;
}
}
nextToken = request.getNextToken();
} while (nextToken != null);
return false;
}
private boolean doesTopicExists(字符串主题){
字符串nextToken=null;
做{
ListTopicsRequest=新建ListTopicsRequest();
请求。setNextToken(nextToken);
ListTopicsResult ListTopicsResult=snsClient.listTopics();
List topics=listTopicsResult.getTopics();
用于(主题:主题){
if(topic.gettopicar().equals(topicARN)){
返回true;
}
}
nextToken=request.getNextToken();
}while(nextToken!=null);
返回false;
}
更多信息,请查看它们的java文档并搜索示例,
玩得开心 首先,您需要通过在build.sbt中添加以下行将SNS SDK添加到您的项目中:
libraryDependencies += "com.amazonaws" % "aws-java-sdk-sns" % "1.11.271"
然后通过将服务注入控制器来使用服务,您的服务应该类似于:
@Singleton
public final class AmazonSNSService {
// logging always a good thing to do
private final Logger.ALogger logger = Logger.of(this.getClass());
// this is what you have to use
private final com.amazonaws.services.sns.AmazonSNS snsClient;
private final String AMAZON_ARN;
@Inject
public AmazonSNSService(Configuration configuration) { // or Config if play 2.6 and later
// I set the aws config in application.conf and I read them here
final String AWS_ACCESS_KEY = configuration.getString("aws_access_key");
final String AWS_SECRET_KEY = configuration.getString("aws_secrect_key");
snsClient = AmazonSNSClient.builder()
.withRegion(Regions.US_EAST_1)
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY)))
.build();
AMAZON_ARN = configuration.getString("amazon_arn_config_key");
}
}
然后,您可以使用snsClient
和您想要的方法来创建主题:
public void createTopic(String topicName) {
String topicARN = AMAZON_ARN + ":" + topicName;
if (doesTopicExists(topicARN)) {
logger.debug("TopicArn - already Exists" + topicARN);
} else {
//create a new SNS topic
CreateTopicRequest createTopicRequest = new CreateTopicRequest(topicName);
CreateTopicResult createTopicResult = snsClient.createTopic(createTopicRequest);
//get request id for CreateTopicRequest from SNS metadata
ResponseMetadata topicResponse = snsClient.getCachedResponseMetadata(createTopicRequest);
logger.debug("CreateTopicArn - " + createTopicResult.getTopicArn());
}
}
还有另一个订阅主题的示例:
public String subscribeToTopic(String topicName, String deviceEndpointARN) {
String topicARN = AMAZON_ARN + ":" + topicName;
//if topic does not exists create topic then subscribe
if (!doesTopicExists(topicARN)) {
createTopic(topicName);
}
return subscribeToTopic(topicARN, deviceEndpointARN);
}
想进入主题吗
public void publishToTopic(String message, String topicName) {
String topicARN = AMAZON_ARN + ":" + topicName;
//if topic does not exists create topic then publish to topic
// or throw an exception, maybe it does not make sense to push to the topic that have no subscribers at all.
if (!doesTopicExists(topicARN)) {
createTopic(topicName);
}
//publish to an SNS topic
PublishRequest publishRequest = new PublishRequest(topicARN, message);
publishRequest.setMessageStructure("json");
PublishResult publishResult = snsClient.publish(publishRequest);
//print MessageId of message published to SNS topic
logger.debug("Push Notification sent to TOPIC [" + topicARN + "] MessageId - [" + publishResult.getMessageId() + "] Message Body: " + message);
}
这就是我检查主题是否存在的方式:
private boolean doesTopicExists(String topicARN) {
String nextToken = null;
do {
ListTopicsRequest request = new ListTopicsRequest();
request.setNextToken(nextToken);
ListTopicsResult listTopicsResult = snsClient.listTopics();
List<Topic> topics = listTopicsResult.getTopics();
for (Topic topic : topics) {
if (topic.getTopicArn().equals(topicARN)) {
return true;
}
}
nextToken = request.getNextToken();
} while (nextToken != null);
return false;
}
private boolean doesTopicExists(字符串主题){
字符串nextToken=null;
做{
ListTopicsRequest=新建ListTopicsRequest();
请求。setNextToken(nextToken);
ListTopicsResult ListTopicsResult=snsClient.listTopics();
List topics=listTopicsResult.getTopics();
用于(主题:主题){
if(topic.gettopicar().equals(topicARN)){
返回true;
}
}
nextToken=request.getNextToken();
}while(nextToken!=null);
返回false;
}
更多信息,请查看它们的java文档并搜索示例,
玩得开心 对!!它已经在prod环境中工作了,有什么问题吗?不管怎么说,它是java如果你的意思是它将是适用的,但处理事情会更麻烦,SDK会为你节省很多时间。当然,所以这个SDK,我必须下载才能玩?如果我要使用WS,你知道你会怎么做吗?对于SDK,只需将这一行添加到build.sbt文件中,它就会为你下载!使用
WS
确实太广泛了,它是关于构建服务和添加大量行来处理响应、错误、如何发送、收集数据和嵌套调用等等,为什么要这样做?无论如何,如果play文档没有帮到你,我也真的帮不了你,我认为你需要从简单的代码开始,并使用文档中提供的示例开始。此外,没有叫做play的语言,它是一个基于Java/Scala的框架,为什么我觉得你们认为它们都是不同的东西,比如Java不能与play一起工作!对它已经在prod环境中工作了,有什么问题吗?不管怎么说,它是java如果你的意思是它将是适用的,但处理事情会更麻烦,SDK会为你节省很多时间。当然,所以这个SDK,我必须下载才能玩?如果我要使用WS,你知道你会怎么做吗?对于SDK,只需将这一行添加到build.sbt文件中,它就会为你下载!使用WS
确实太广泛了,它是关于构建服务和添加大量行来处理响应、错误、如何发送、收集数据和嵌套调用等等,为什么要这样做?无论如何,如果play文档没有帮到你,我也真的帮不了你,我认为你需要从简单的代码开始,并使用文档中提供的示例开始。此外,没有叫做play的语言,它是一个基于Java/Scala的框架,为什么我觉得你们认为它们都是不同的东西,比如Java不能与play一起工作!