concept session window in category kafka

This is an excerpt from Manning's book Kafka Streams in Action: Real-time apps and microservices with the Kafka Streams API.
Which type you choose depends on your business requirements. The tumbling and hopping windows are time bound, whereas session windows are more about user activity; the length of the session(s) is determined solely by how active the user is. A key point to keep in mind for all windows is that they’re based on the timestamps in the records and not wall-clock time.
Let’s use session windows to capture the stock transactions. The following code (found in src/main/java/bbejeck/chapter_5/CountingWindowingAndKTableJoinExample.java) shows how to implement the session windows.
Listing 5.5. Tracking stock transactions with session windows
Serde<String> stringSerde = Serdes.String(); Serde<StockTransaction> transactionSerde = StreamsSerdes.StockTransactionSerde(); Serde<TransactionSummary> transactionKeySerde = StreamsSerdes.TransactionSummarySerde(); long twentySeconds = 1000 * 20; long fifteenMinutes = 1000 * 60 * 15; KTable<Windowed<TransactionSummary>, Long> customerTransactionCounts = #1 builder.stream(STOCK_TRANSACTIONS_TOPIC, Consumed.with(stringSerde, transactionSerde) .withOffsetResetPolicy(LATEST)) #2 .groupBy((noKey, transaction) -> TransactionSummary.from(transaction), #3 Serialized.with(transactionKeySerde, transactionSerde)) .windowedBy(SessionWindows.with(twentySeconds). until(fifteenMinutes)).count(); #4 customerTransactionCounts.toStream() .print(Printed.<Windowed<TransactionSummary>, Long>toSysOut() .withLabel("Customer Transactions Counts")); #5
The code in listing 5.5 does a count over session windows. Figure 5.13 breaks it down.
With the call to windowedBy(SessionWindows.with(twentySeconds).until(fifteenMinutes)), you create a session window with an inactivity gap of 20 seconds and a retention period of 15 minutes. An inactivity time of 20 seconds means the application includes any record arriving within 20 seconds of the current session’s ending or start time within the current (active) session.
You then specify the aggregation operation to perform count, in this case, on the session window. If an incoming record falls outside the inactivity gap (on either side of the timestamp), the application creates a new session. The retention period maintains the session for the specified amount of time and allows for late-arriving data that’s outside the inactivity period of a session but can still be merged. Additionally, as sessions are combined, the newly created session uses the earliest timestamp and latest timestamp for the start and end of the new session, respectively.