-
Notifications
You must be signed in to change notification settings - Fork 643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add SNS connector with publish sink #204 #205
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4280680
add SNS connector with publish sink #204
87da633
add SnsPublishFlow, update documentation and PR improvements
44cc23b
emit SNS PublishResult in SnsPublishFlow
38b6b12
update SnsPublishFlow documentation
3b81cff
provide SnsPublisher with sink and flow factory methods
65c4edf
remove SnsPublishSink and track inFlight messages for proper stage cl…
02ba3ed
update documentation
d11f49c
update code formatting
2cfdb15
make SnsPublishFlowStage internal API
2b36d4c
update SnsPublisher documentation
f744d00
Merge branch 'master' into aws_sns_sink
588d731
update push/pull logic and file headers
8af3ebf
Merge branch 'master' into aws_sns_sink
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# AWS SNS Connector | ||
|
||
The AWS SNS connector provides an Akka Stream Flow and Sink for push notifications through AWS SNS. | ||
|
||
For more information about AWS SNS please visit the [official documentation](https://aws.amazon.com/documentation/sns/). | ||
|
||
## Artifacts | ||
|
||
sbt | ||
: @@@vars | ||
```scala | ||
libraryDependencies += "com.lightbend.akka" %% "akka-stream-alpakka-sns" % "$version$" | ||
``` | ||
@@@ | ||
|
||
Maven | ||
: @@@vars | ||
```xml | ||
<dependency> | ||
<groupId>com.lightbend.akka</groupId> | ||
<artifactId>akka-stream-alpakka-sns_$scala.binaryVersion$</artifactId> | ||
<version>$version$</version> | ||
</dependency> | ||
``` | ||
@@@ | ||
|
||
Gradle | ||
: @@@vars | ||
```gradle | ||
dependencies { | ||
compile group: "com.lightbend.akka", name: "akka-stream-alpakka-sns_$scala.binaryVersion$", version: "$version$" | ||
} | ||
``` | ||
@@@ | ||
|
||
## Usage | ||
|
||
Sources provided by this connector need a prepared `AmazonSNSAsyncClient` to publish messages to a topic. | ||
|
||
Scala | ||
: @@snip (../../../../sns/src/test/scala/akka/stream/alpakka/sns/scaladsl/Examples.scala) { #init-client } | ||
|
||
Java | ||
: @@snip (../../../../sns/src/test/java/akka/stream/alpakka/sns/javadsl/Examples.java) { #init-client } | ||
|
||
We will also need an @scaladoc[ActorSystem](akka.actor.ActorSystem) and an @scaladoc[ActorMaterializer](akka.stream.ActorMaterializer). | ||
|
||
Scala | ||
: @@snip (../../../../sns/src/test/scala/akka/stream/alpakka/sns/scaladsl/Examples.scala) { #init-system } | ||
|
||
Java | ||
: @@snip (../../../../sns/src/test/java/akka/stream/alpakka/sns/javadsl/Examples.java) { #init-system } | ||
|
||
This is all preparation that we are going to need. | ||
|
||
### Publish messages to a SNS topic | ||
|
||
Now we can publish a String message to any SNS topic where we have access to by providing the topic ARN to the | ||
@scaladoc[SnsPublisher](akka.stream.alpakka.sns.scaladsl.SnsPublisher$) Flow or Sink factory method. | ||
|
||
### Using a Flow | ||
|
||
Scala | ||
: @@snip (../../../../sns/src/test/scala/akka/stream/alpakka/sns/scaladsl/Examples.scala) { #use-flow } | ||
|
||
Java | ||
: @@snip (../../../../sns/src/test/java/akka/stream/alpakka/sns/javadsl/Examples.java) { #use-flow } | ||
|
||
As you can see, this would publish the messages from the source to the specified AWS SNS topic. | ||
After a message has been successfully published, a | ||
[PublishResult](http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/sns/model/PublishResult.html) | ||
will be pushed downstream. | ||
|
||
### Using a Sink | ||
|
||
Scala | ||
: @@snip (../../../../sns/src/test/scala/akka/stream/alpakka/sns/scaladsl/Examples.scala) { #use-sink } | ||
|
||
Java | ||
: @@snip (../../../../sns/src/test/java/akka/stream/alpakka/sns/javadsl/Examples.java) { #use-sink } | ||
|
||
As you can see, this would publish the messages from the source to the specified AWS SNS topic. | ||
|
||
### Running the example code | ||
|
||
The code in this guide is part of runnable tests of this project. You are welcome to edit the code and run it in sbt. | ||
|
||
Scala | ||
: ``` | ||
sbt | ||
> sns/test | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
sns/src/main/scala/akka/stream/alpakka/sns/SnsPublishFlowStage.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (C) 2016-2017 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
package akka.stream.alpakka.sns | ||
|
||
import akka.stream._ | ||
import akka.stream.stage._ | ||
import com.amazonaws.handlers.AsyncHandler | ||
import com.amazonaws.services.sns.AmazonSNSAsync | ||
import com.amazonaws.services.sns.model.{PublishRequest, PublishResult} | ||
|
||
private[akka] final class SnsPublishFlowStage(topicArn: String, snsClient: AmazonSNSAsync) | ||
extends GraphStage[FlowShape[String, PublishResult]] { | ||
|
||
private val in = Inlet[String]("SnsPublishFlow.in") | ||
private val out = Outlet[PublishResult]("SnsPublishFlow.out") | ||
|
||
override def shape: FlowShape[String, PublishResult] = FlowShape.of(in, out) | ||
|
||
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = | ||
new GraphStageLogic(shape) with InHandler with OutHandler with StageLogging { | ||
|
||
private var isMessageInFlight = false | ||
private val failureCallback = getAsyncCallback[Throwable](handleFailure) | ||
private val successCallback = getAsyncCallback[PublishResult](handleSuccess) | ||
|
||
private def handleFailure(ex: Throwable): Unit = | ||
failStage(ex) | ||
|
||
private def handleSuccess(result: PublishResult): Unit = { | ||
log.debug("Published SNS message: {}", result.getMessageId) | ||
isMessageInFlight = false | ||
if (!isClosed(out)) push(out, result) | ||
} | ||
|
||
private val asyncHandler = new AsyncHandler[PublishRequest, PublishResult] { | ||
override def onError(exception: Exception): Unit = | ||
failureCallback.invoke(exception) | ||
override def onSuccess(request: PublishRequest, result: PublishResult): Unit = | ||
successCallback.invoke(result) | ||
} | ||
|
||
override def onPush(): Unit = { | ||
isMessageInFlight = true | ||
val request = new PublishRequest().withTopicArn(topicArn).withMessage(grab(in)) | ||
snsClient.publishAsync(request, asyncHandler) | ||
} | ||
|
||
override def onPull(): Unit = { | ||
if (isClosed(in) && !isMessageInFlight) completeStage() | ||
if (!hasBeenPulled(in)) tryPull(in) | ||
} | ||
|
||
override def onUpstreamFinish(): Unit = | ||
if (!isMessageInFlight) completeStage() | ||
|
||
setHandlers(in, out, this) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
sns/src/main/scala/akka/stream/alpakka/sns/javadsl/SnsPublisher.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (C) 2016-2017 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
package akka.stream.alpakka.sns.javadsl | ||
|
||
import java.util.concurrent.CompletionStage | ||
|
||
import akka.stream.alpakka.sns.SnsPublishFlowStage | ||
import akka.stream.javadsl.{Flow, Keep, Sink} | ||
import akka.{Done, NotUsed} | ||
import com.amazonaws.services.sns.AmazonSNSAsync | ||
import com.amazonaws.services.sns.model.PublishResult | ||
|
||
object SnsPublisher { | ||
|
||
/** | ||
* Java API: creates a [[Flow]] to publish messages to a SNS topic using an [[AmazonSNSAsync]] | ||
*/ | ||
def createFlow(topicArn: String, snsClient: AmazonSNSAsync): Flow[String, PublishResult, NotUsed] = | ||
Flow.fromGraph(new SnsPublishFlowStage(topicArn, snsClient)) | ||
|
||
/** | ||
* Java API: creates a [[Sink]] to publish messages to a SNS topic using an [[AmazonSNSAsync]] | ||
*/ | ||
def createSink(topicArn: String, snsClient: AmazonSNSAsync): Sink[String, CompletionStage[Done]] = | ||
createFlow(topicArn, snsClient).toMat(Sink.ignore(), Keep.right[NotUsed, CompletionStage[Done]]) | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
sns/src/main/scala/akka/stream/alpakka/sns/scaladsl/SnsPublisher.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (C) 2016-2017 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
package akka.stream.alpakka.sns.scaladsl | ||
|
||
import akka.stream.alpakka.sns.SnsPublishFlowStage | ||
import akka.stream.scaladsl.{Flow, Keep, Sink} | ||
import akka.{Done, NotUsed} | ||
import com.amazonaws.services.sns.AmazonSNSAsync | ||
import com.amazonaws.services.sns.model.PublishResult | ||
|
||
import scala.concurrent.Future | ||
|
||
object SnsPublisher { | ||
|
||
/** | ||
* Scala API: creates a [[Sink]] to publish messages to a SNS topic using an [[AmazonSNSAsync]] | ||
*/ | ||
def flow(topicArn: String)(implicit snsClient: AmazonSNSAsync): Flow[String, PublishResult, NotUsed] = | ||
Flow.fromGraph(new SnsPublishFlowStage(topicArn, snsClient)) | ||
|
||
/** | ||
* Scala API: creates a [[Sink]] to publish messages to a SNS topic using an [[AmazonSNSAsync]] | ||
*/ | ||
def sink(topicArn: String)(implicit snsClient: AmazonSNSAsync): Sink[String, Future[Done]] = | ||
flow(topicArn).toMat(Sink.ignore)(Keep.right) | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
sns/src/test/java/akka/stream/alpakka/sns/javadsl/Examples.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (C) 2016-2017 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
package akka.stream.alpakka.sns.javadsl; | ||
|
||
import akka.Done; | ||
import akka.actor.ActorSystem; | ||
import akka.stream.ActorMaterializer; | ||
import akka.stream.javadsl.Sink; | ||
import akka.stream.javadsl.Source; | ||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.sns.AmazonSNSAsync; | ||
import com.amazonaws.services.sns.AmazonSNSAsyncClientBuilder; | ||
|
||
import java.util.concurrent.CompletionStage; | ||
|
||
public class Examples { | ||
|
||
//#init-client | ||
BasicAWSCredentials credentials = new BasicAWSCredentials("x", "x"); | ||
AmazonSNSAsync snsClient = AmazonSNSAsyncClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials)).build(); | ||
//#init-client | ||
|
||
//#init-system | ||
ActorSystem system = ActorSystem.create(); | ||
ActorMaterializer materializer = ActorMaterializer.create(system); | ||
//#init-system | ||
|
||
//#use-sink | ||
CompletionStage<Done> sink = Source.single("message").runWith(SnsPublisher.createSink("topic-arn", snsClient), materializer); | ||
//#use-sink | ||
|
||
//#use-flow | ||
CompletionStage<Done> flow = Source.single("message").via(SnsPublisher.createFlow("topic-arn", snsClient)).runWith(Sink.ignore(), materializer); | ||
//#use-flow | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
sns/src/test/scala/akka/stream/alpakka/sns/DefaultTestContext.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (C) 2016-2017 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
package akka.stream.alpakka.sns | ||
|
||
import akka.actor.ActorSystem | ||
import akka.stream.{ActorMaterializer, Materializer} | ||
import com.amazonaws.services.sns.AmazonSNSAsyncClient | ||
import org.mockito.Mockito.reset | ||
import org.scalatest.mockito.MockitoSugar | ||
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, Suite} | ||
|
||
import scala.concurrent.Await | ||
import scala.concurrent.duration._ | ||
|
||
trait DefaultTestContext extends BeforeAndAfterAll with BeforeAndAfterEach with MockitoSugar { this: Suite => | ||
|
||
implicit protected val system: ActorSystem = ActorSystem() | ||
implicit protected val mat: Materializer = ActorMaterializer() | ||
implicit protected val snsClient: AmazonSNSAsyncClient = mock[AmazonSNSAsyncClient] | ||
|
||
override protected def beforeEach(): Unit = | ||
reset(snsClient) | ||
|
||
override protected def afterAll(): Unit = | ||
Await.ready(system.terminate(), 5.seconds) | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the client have a lifecycle, is is thread safe, should this be a client factory instead (
() => AmazonSNSAsync
) and have its lifecycle managed by the stage (`client.close() when the stage stops)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NVM, I already asked that previously but github hid all comments and I had forgot.