@@ -29,64 +29,7 @@ import com.weightwatchers.reactive.kinesis.utils.{FutureUtils, TypesafeConfigExt
29
29
30
30
import scala .concurrent .{ExecutionContextExecutor , Future }
31
31
32
- trait KinesisProducer {
33
-
34
- /**
35
- * Adds a message to the next batch to be sent to the configured stream.
36
- *
37
- * @return On success: Future{UserRecordResult}
38
- * On failure: Future.failed(...): Any Throwable related to put.
39
- * @see Callee `com.amazonaws.services.kinesis.producer.KinesisProducer.addUserRecord`
40
- * @see UserRecordResult
41
- * @see KinesisProducerConfiguration#setRecordTtl(long)
42
- * @see UserRecordFailedException
43
- */
44
- def addUserRecord (event : ProducerEvent )(
45
- implicit ec : ExecutionContextExecutor
46
- ): Future [UserRecordResult ]
47
-
48
- /**
49
- * Get the number of unfinished records currently being processed. The
50
- * records could either be waiting to be sent to the child process, or have
51
- * reached the child process and are being worked on.
52
- *
53
- * <p>
54
- * This is equal to the number of futures returned from [[addUserRecord ]]
55
- * that have not finished.
56
- *
57
- * This is useful for applying backpressure and throttling the number of concurrent Futures.
58
- *
59
- * @return The number of unfinished records currently being processed.
60
- */
61
- def outstandingRecordsCount (): Int
62
-
63
- /**
64
- * Firstly, blocks whilst all all records are complete (either succeeding or failing).
65
- *
66
- * <p>
67
- *
68
- * The includes whilst any retries are performed. Depending on
69
- * your configuration of record TTL and request timeout, this can
70
- * potentially take a long time if the library is having trouble delivering
71
- * records to the backend, for example due to network problems.
72
- *
73
- * <p>
74
- *
75
- * Finally the [[KinesisProducer ]] is destroyed, preventing further use.
76
- *
77
- * @throws com.amazonaws.services.kinesis.producer.DaemonException if the child process is dead //TODO - handle this better?
78
- * @see [[AWSKinesisProducer ]]
79
- */
80
- def stop (): Unit
81
-
82
- /**
83
- * @return true if the [[KinesisProducer ]] has been stopped & destroyed.
84
- */
85
- def destroyed (): Boolean
86
-
87
- }
88
-
89
- object KinesisProducerKPL extends LazyLogging {
32
+ object KinesisProducer extends LazyLogging {
90
33
91
34
/**
92
35
* The config passed is expected to contain the AWS KPL properties at the top level.
@@ -100,9 +43,11 @@ object KinesisProducerKPL extends LazyLogging {
100
43
* @param credentialsProvider A specific CredentialsProvider. The KCL defaults to DefaultAWSCredentialsProviderChain.
101
44
* @return an instantiated [[KinesisProducer ]]
102
45
*/
46
+ @ deprecated(" Use KinesisProducer(producerConf: ProducerConf) instead" , " v0.5.7" )
103
47
def apply (kplConfig : Config ,
104
48
streamName : String ,
105
49
credentialsProvider : Option [AWSCredentialsProvider ] = None ): KinesisProducer = {
50
+
106
51
import TypesafeConfigExtensions ._
107
52
108
53
// We directly load our properties into the KPL as a Java `Properties` object
@@ -117,7 +62,32 @@ object KinesisProducerKPL extends LazyLogging {
117
62
KinesisProducerConfiguration .fromProperties(kplProps)
118
63
credentialsProvider.foreach(kplLibConfiguration.setCredentialsProvider)
119
64
120
- new KinesisProducerKPL (new AWSKinesisProducer (kplLibConfiguration), streamName)
65
+ new KinesisProducer (new AWSKinesisProducer (kplLibConfiguration), streamName)
66
+ }
67
+
68
+ /**
69
+ * The config passed is expected to contain the AWS KPL properties at the top level.
70
+ *
71
+ * @param producerConf An instance of [[ProducerConf ]] which contains all required configuration for the KPL.
72
+ * @return an instantiated [[KinesisProducer ]]
73
+ */
74
+ def apply (producerConf : ProducerConf ): KinesisProducer = {
75
+ apply(producerConf.kplLibConfiguration, producerConf.streamName)
76
+ }
77
+
78
+ /**
79
+ * The [[KinesisProducerConfiguration ]] argument is passed directly to the KPL library.
80
+ * This constructor makes no use of the Typesafe config.
81
+ *
82
+ * @see `src/it/resources/reference.conf` for a more detailed example.
83
+ * @param kplConfig An instance of the underlying [[KinesisProducerConfiguration ]] to be passed
84
+ * directly to the library.
85
+ * @param streamName Th name of the Kinesis stream, which must exist.
86
+ * @return an instantiated [[KinesisProducer ]]
87
+ */
88
+ def apply (kplConfig : KinesisProducerConfiguration , streamName : String ): KinesisProducer = {
89
+ // TODO add logging
90
+ new KinesisProducer (new AWSKinesisProducer (kplConfig), streamName)
121
91
}
122
92
}
123
93
@@ -126,9 +96,7 @@ object KinesisProducerKPL extends LazyLogging {
126
96
*
127
97
* To create an instance of this class, we recommend using the apply method to instantiate from config.
128
98
*/
129
- class KinesisProducerKPL (kinesis : AWSKinesisProducer , streamName : String )
130
- extends LazyLogging
131
- with KinesisProducer {
99
+ class KinesisProducer (kinesis : AWSKinesisProducer , streamName : String ) extends LazyLogging {
132
100
133
101
val underlying = kinesis
134
102
private var _destroyed = false
@@ -137,9 +105,16 @@ class KinesisProducerKPL(kinesis: AWSKinesisProducer, streamName: String)
137
105
// TODO seems difficult to get access to stream specific operations from producer
138
106
139
107
/**
140
- * @see [[KinesisProducer ]].addUserRecord
108
+ * Adds a message to the next batch to be sent to the configured stream.
109
+ *
110
+ * @return On success: Future{UserRecordResult}
111
+ * On failure: Future.failed(...): Any Throwable related to put.
112
+ * @see Callee `com.amazonaws.services.kinesis.producer.KinesisProducer.addUserRecord`
113
+ * @see UserRecordResult
114
+ * @see KinesisProducerConfiguration#setRecordTtl(long)
115
+ * @see UserRecordFailedException
141
116
*/
142
- override def addUserRecord (
117
+ def addUserRecord (
143
118
event : ProducerEvent
144
119
)(implicit ec : ExecutionContextExecutor ): Future [UserRecordResult ] = {
145
120
assert(! _destroyed, " Kinesis has been destroyed, no longer accepting messages" ) // TODO specific exception?
@@ -148,24 +123,47 @@ class KinesisProducerKPL(kinesis: AWSKinesisProducer, streamName: String)
148
123
}
149
124
150
125
/**
151
- * @see [[KinesisProducer ]].outstandingRecordsCount()
126
+ * Get the number of unfinished records currently being processed. The
127
+ * records could either be waiting to be sent to the child process, or have
128
+ * reached the child process and are being worked on.
129
+ *
130
+ * <p>
131
+ * This is equal to the number of futures returned from [[addUserRecord ]]
132
+ * that have not finished.
133
+ *
134
+ * This is useful for applying backpressure and throttling the number of concurrent Futures.
135
+ *
136
+ * @return The number of unfinished records currently being processed.
152
137
*/
153
- override def outstandingRecordsCount (): Int = {
138
+ def outstandingRecordsCount (): Int = {
154
139
kinesis.getOutstandingRecordsCount
155
140
}
156
141
157
142
/**
158
- * @see [[KinesisProducer ]].stop()sbt publish
143
+ * Firstly, blocks whilst all all records are complete (either succeeding or failing).
144
+ *
145
+ * <p>
159
146
*
147
+ * The includes whilst any retries are performed. Depending on
148
+ * your configuration of record TTL and request timeout, this can
149
+ * potentially take a long time if the library is having trouble delivering
150
+ * records to the backend, for example due to network problems.
151
+ *
152
+ * <p>
153
+ *
154
+ * Finally the [[KinesisProducer ]] is destroyed, preventing further use.
155
+ *
156
+ * @throws com.amazonaws.services.kinesis.producer.DaemonException if the child process is dead //TODO - handle this better?
157
+ * @see [[AWSKinesisProducer ]]
160
158
*/
161
- override def stop (): Unit = {
159
+ def stop (): Unit = {
162
160
kinesis.flushSync() // This blocks until all records are flushed
163
161
kinesis.destroy()
164
162
_destroyed = true
165
163
}
166
164
167
165
/**
168
- * @see [[KinesisProducer ]]destroyed()
166
+ * @return true if the [[KinesisProducer ]] has been stopped & destroyed.
169
167
*/
170
- override def destroyed (): Boolean = _destroyed
168
+ def destroyed (): Boolean = _destroyed
171
169
}
0 commit comments