You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To create an injectable |c.t.app.App|_, first depend on the `inject-app` library. Then use the
7
16
`inject framework <../getting-started/framework.html#inject>`__ to create an injectable App. Finatra
8
17
provides an injectable version of the |c.t.app.App|_ trait: |c.t.inject.app.App|_.
@@ -16,24 +25,49 @@ in a |c.t.app.App|_ with support for `modules <../getting-started/modules.html>`
16
25
Scala Example
17
26
-------------
18
27
28
+
Given a `module <../getting-started/modules.html>`__ definition:
29
+
30
+
.. code:: scala
31
+
32
+
import com.twitter.inject.TwitterModule
33
+
34
+
object MyModule1 extends TwitterModule {
35
+
36
+
@Singleton
37
+
@Provides
38
+
def providesFooService: FooService = ???
39
+
}
40
+
41
+
You could define an |c.t.inject.app.App|_:
42
+
19
43
.. code:: scala
20
44
21
45
import com.google.inject.Module
22
-
import com.twitter.inject.app.App
23
46
import com.twitter.inject.Logging
47
+
import com.twitter.inject.app.App
48
+
import com.twitter.inject.modules.LoggingModule
24
49
25
50
object MyAppMain extends MyApp
26
51
27
52
class MyApp extends App with Logging {
28
53
29
54
override val modules: Seq[Module] = Seq(
55
+
LoggingModule,
30
56
MyModule1)
31
57
32
58
override protected def run(): Unit = {
33
59
// Core app logic goes here.
60
+
val fooService = injector.instance[FooService]
61
+
???
34
62
}
35
63
}
36
64
65
+
.. tip::
66
+
67
+
We include the `c.t.inject.modules.LoggingModule <https://github.com/twitter/finatra/blob/develop/inject/inject-modules/src/main/scala/com/twitter/inject/modules/LoggerModule.scala>`__ to attempt installation of the
68
+
`SLF4JBridgeHandler <https://www.slf4j.org/api/org/slf4j/bridge/SLF4JBridgeHandler.html>`__ here as an example of
69
+
how to `bridge legacy APIs <https://www.slf4j.org/legacy.html>`__.
70
+
37
71
Then to test:
38
72
39
73
.. code:: scala
@@ -61,21 +95,38 @@ Java Example
61
95
importcom.google.inject.Module;
62
96
63
97
importcom.twitter.inject.app.AbstractApp;
98
+
importcom.twitter.inject.modules.LoggerModule$;
64
99
65
100
publicclassMyAppextendsAbstractApp {
66
101
67
102
@Override
68
103
publicCollection<Module>javaModules() {
69
104
returnCollections.<Module>singletonList(
105
+
LoggerModule$.MODULE$,
70
106
MyModule1$.MODULE$);
71
107
}
72
108
73
109
@Override
74
110
publicvoidrun() {
75
111
// Core app logic goes here.
112
+
FooService fooService =
113
+
injector().instance(FooService.class);
76
114
}
77
115
}
78
116
117
+
Then create a "main" class:
118
+
119
+
.. code:: java
120
+
121
+
finalclassMyAppMain {
122
+
privateMyAppMain() {
123
+
}
124
+
125
+
publicstaticvoidmain(String[] args) {
126
+
newMyApp().main(args);
127
+
}
128
+
}
129
+
79
130
See the Finatra `examples <https://github.com/twitter/finatra/tree/develop/examples>`__ for detailed examples with tests.
80
131
81
132
`App#run`
@@ -87,6 +138,61 @@ The Finatra `Startup Lifecycle <../getting-started/lifecycle.html#startup>`__ en
87
138
will be properly configured before access and provides the ``#run`` method as the function for implementing
88
139
the app.
89
140
141
+
Using Injection
142
+
---------------
143
+
144
+
The |c.t.inject.app.App|_ exposes access to the configured `c.t.inject.Injector <https://github.com/twitter/finatra/blob/develop/inject/inject-core/src/main/scala/com/twitter/inject/Injector.scala>`__ which can be used to obtain instances from
145
+
the object graph.
146
+
147
+
.. note::
148
+
149
+
You should take care to only access the injector in the `App#run` function as this is the correct place in the
150
+
`lifecycle <../getting-started/lifecycle.html#c-t-app-app-lifecycle>`__ where you are ensured to have a fully
151
+
configured `c.t.inject.Injector`. Accessing the `c.t.inject.Injector` too early in the lifecycle (before it is
152
+
configured) will result in an Exception being thrown.
153
+
154
+
An Example of Handling Signals
155
+
------------------------------
156
+
157
+
There may be cases where you want your application to handle an
158
+
`IPC signal <https://en.wikipedia.org/wiki/Signal_(IPC)>`__ instead of closing normally
159
+
once the code execution is done, e.g., handling an `INT` (Ctrl-C) or `TSTP` (Ctrl-Z) signal.
160
+
161
+
You can use the
162
+
`com.twitter.util.HandleSignal <https://github.com/twitter/util/blob/aaa3d6e2bf37a3bd565a8c51187fd9b6db8a0b25/util-core/src/main/scala/com/twitter/util/Signal.scala#L75>`__ utility to apply a callback to run on receiving
163
+
the signal.
164
+
165
+
.. note::
166
+
167
+
Please consult the scaladocs for `com.twitter.util.HandleSignal <https://github.com/twitter/util/blob/aaa3d6e2bf37a3bd565a8c51187fd9b6db8a0b25/util-core/src/main/scala/com/twitter/util/Signal.scala#L75>`__
168
+
to make sure you are aware of the limitations of the code in handling signals.
169
+
170
+
For example, to exit the application upon receiving an `INT` signal:
171
+
172
+
.. code:: scala
173
+
174
+
import com.google.inject.Module
175
+
import com.twitter.inject.app.App
176
+
import com.twitter.inject.Logging
177
+
178
+
object MyAppMain extends MyApp
179
+
180
+
class MyApp extends App with Logging {
181
+
182
+
override val modules: Seq[Module] = Seq(
183
+
MyModule1)
184
+
185
+
HandleSignal("INT") { signal =>
186
+
exitOnError(s"Process is being terminated with signal $signal.")
Copy file name to clipboardExpand all lines: doc/src/sphinx/user-guide/kafka-streams/examples.rst
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@
3
3
Examples
4
4
========
5
5
6
-
The `integration tests <https://github.com/twitter/finatra/blob/develop/kafka-streams/kafka-streams/src/test/scala/com/twitter/unittests/integration>`__ serve as a good collection of example Finatra Kafka Streams servers.
6
+
The `integration tests <https://github.com/twitter/finatra/tree/develop/kafka-streams/kafka-streams/src/test/scala/com/twitter/finatra/kafkastreams/integration>`__ serve as a good collection of example Finatra Kafka Streams servers.
7
7
8
8
Word Count Server
9
9
-----------------
@@ -52,4 +52,4 @@ We can then expose a Thrift endpoint enabling clients to directly query the stat
52
52
}
53
53
}
54
54
55
-
In this example, ``WordCountQueryService`` is an underlying Thrift service.
55
+
In this example, ``WordCountQueryService`` is an underlying Thrift service.
Copy file name to clipboardExpand all lines: doc/src/sphinx/user-guide/kafka-streams/index.rst
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ Finatra has native integration with `Kafka Streams <https://kafka.apache.org/doc
8
8
Features
9
9
--------
10
10
11
-
- Intuitive `DSL <https://github.com/twitter/finatra/blob/develop/kafka-streams/kafka-streams/src/main/scala/com/twitter/finatra/kafkastreams/internal/utils/FinatraDslV2Implicits.scala>`__ for topology creation, compatible with the `Kafka Streams DSL <https://kafka.apache.org/21/documentation/streams/developer-guide/dsl-api.html>`__
11
+
- Intuitive `DSL <https://github.com/twitter/finatra/tree/develop/kafka-streams/kafka-streams/src/main/scala/com/twitter/finatra/kafkastreams/dsl>`__ for topology creation, compatible with the `Kafka Streams DSL <https://kafka.apache.org/21/documentation/streams/developer-guide/dsl-api.html>`__
12
12
- Full Kafka Streams metric integration, exposed as `TwitterServer Metrics <https://twitter.github.io/twitter-server/Features.html#metrics>`__
13
13
- `RocksDB integration <#rocksdb>`__
14
14
- `Queryable State <#queryable-state>`__
@@ -23,15 +23,15 @@ a fully functional service can be written by simply configuring the Kafka Stream
23
23
Transformers
24
24
~~~~~~~~~~~~
25
25
26
-
Implement custom `transformers <https://kafka.apache.org/21/javadoc/org/apache/kafka/streams/kstream/Transformer.html>`__ using `FinatraTransformer <https://github.com/twitter/finatra/blob/develop/kafka-streams/kafka-streams/src/main/scala/com/twitter/finatra/streams/transformer/FinatraTransformer.scala>`__.
26
+
Implement custom `transformers <https://kafka.apache.org/21/javadoc/org/apache/kafka/streams/kstream/Transformer.html>`__ using `FinatraTransformer <https://github.com/twitter/finatra/blob/develop/kafka-streams/kafka-streams/src/main/scala/com/twitter/finatra/kafkastreams/transformer/FinatraTransformer.scala>`__.
27
27
28
28
Aggregations
29
29
^^^^^^^^^^^^
30
30
31
31
There are several included aggregating transformers, which may be used when configuring a ``StreamsBuilder``
32
+
+ ``aggregate``
32
33
+ ``sample``
33
-
+ ``sum``
34
-
+ ``compositeSum``
34
+
+ ``sum``
35
35
36
36
Stores
37
37
------
@@ -44,13 +44,13 @@ In addition to using `state stores <https://kafka.apache.org/21/javadoc/org/apac
44
44
Queryable State
45
45
~~~~~~~~~~~~~~~
46
46
47
-
Finatra Kafka Streams supports directly querying state from a store. This can be useful for creating a service that serves data aggregated within a local Topology. You can use `static partitioning <https://github.com/twitter/finatra/blob/develop/kafka-streams/kafka-streams-static-partitioning/src/main/scala/com/twitter/finatra/streams/partitioning/StaticPartitioning.scala>`__ to query an instance deterministically known to hold a key.
47
+
Finatra Kafka Streams supports directly querying state from a store. This can be useful for creating a service that serves data aggregated within a local Topology. You can use `static partitioning <https://github.com/twitter/finatra/blob/develop/kafka-streams/kafka-streams-static-partitioning/src/main/scala/com/twitter/finatra/kafkastreams/partitioning/StaticPartitioning.scala>`__ to query an instance deterministically known to hold a key.
48
48
49
49
See how queryable state is used in the following `example <examples.html#queryable-state>`__.
Copy file name to clipboardExpand all lines: doc/src/sphinx/user-guide/kafka-streams/testing.rst
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,4 +3,4 @@
3
3
Testing
4
4
=======
5
5
6
-
Finatra Kafka Streams includes tooling that simplifies the process of writing highly testable services. See `TopologyFeatureTest <https://github.com/twitter/finatra/blob/develop/kafka-streams/kafka-streams/src/test/scala/com/twitter/finatra/streams/tests/TopologyFeatureTest.scala>`__, which includes a `FinatraTopologyTester <https://github.com/twitter/finatra/blob/develop/kafka-streams/kafka-streams/src/test/scala/com/twitter/finatra/streams/tests/FinatraTopologyTester.scala>`__ that integrates Kafka Streams' `TopologyTestDriver <https://kafka.apache.org/21/javadoc/org/apache/kafka/streams/TopologyTestDriver.html>`__ with a `KafkaStreamsTwitterServer <https://github.com/twitter/finatra/blob/develop/kafka-streams/kafka-streams/src/main/scala/com/twitter/finatra/kafkastreams/KafkaStreamsTwitterServer.scala>`__.
6
+
Finatra Kafka Streams includes tooling that simplifies the process of writing highly testable services. See `TopologyFeatureTest <https://github.com/twitter/finatra/blob/develop/kafka-streams/kafka-streams/src/test/scala/com/twitter/finatra/kafkastreams/test/TopologyFeatureTest.scala>`__, which includes a `FinatraTopologyTester <https://github.com/twitter/finatra/blob/develop/kafka-streams/kafka-streams/src/test/scala/com/twitter/finatra/kafkastreams/test/FinatraTopologyTester.scala>`__ that integrates Kafka Streams' `TopologyTestDriver <https://kafka.apache.org/21/javadoc/org/apache/kafka/streams/TopologyTestDriver.html>`__ with a `KafkaStreamsTwitterServer <https://github.com/twitter/finatra/blob/develop/kafka-streams/kafka-streams/src/main/scala/com/twitter/finatra/kafkastreams/KafkaStreamsTwitterServer.scala>`__.
0 commit comments