The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java 8 Recursive WatchService

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Fabrizio Fortino

Posts: 10
Nickname: fabriziof
Registered: Aug, 2014

Fabrizio Fortino is an experienced programmer and open source enthusiast.
Java 8 Recursive WatchService Posted: Jun 7, 2016 2:44 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Fabrizio Fortino.
Original Post: Java 8 Recursive WatchService
Feed Title: Keep it HTTPS
Feed URL: http://fabriziofortino.github.io/feed.xml
Feed Description: No fluff just stuff weblog
Latest Java Buzz Posts
Latest Java Buzz Posts by Fabrizio Fortino
Latest Posts From Keep it HTTPS

Advertisement

WatchService is a class included in the standard Java nio package since version 7. WatchService is extremely useful when we need to trigger an action after an event happens to an object in a specific folder or set of folders.

In the following code I have implemented a watch service registered to a root folder and all its subfolders.

Most of the examples around are based on Java 7. Since I am using Java 8 I have decided to re-implement it using streams and lambdas:

  • The code responsible to recursively register the folders has been implemented as a Consumer Functional Interface (lines 67-84) and gets called to register the root folder (line 86) and the folders created at successive stages (line 109).
  • A lambda with no arguments (or thunk) is used at line 88 to create and start a separate thread to execute the WatchService logic.
  • The iteration of the pending events has been implemented in a functional style using Java 8 streams (lines 103-114).

The code makes use of Spring but it’s extremely easy to remove it and adapt to other needs.

Java 8 Recursive WatchService was originally published by Fabrizio Fortino at keep it https on June 05, 2016.

Read: Java 8 Recursive WatchService

Topic: Hibernate JSON UserType: simple handling of JSON objects with Jackson Previous Topic   Next Topic Topic: Apache Hadoop Hue Tutorial

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use