Jnotify dynamically listens to folders, mail system folder listening example, jnotify Mail System
JNotify is a rack package that supports dynamic monitoring files and folders (supports cascading monitoring. In linux, the underlying inotify service of linux is called, but the sub-Folder cascading monitoring function is added. In windows, you need to add the attached dll file because this service is not available in windows by default.
I. Monitoring of local folders by the mail system
1. To use jnotify, You need to import jnotify. jar
2. Add the jnotify local method to jdk/bin: jnotify. dll jpolicy_64bit.dll.
Step 2
1. jpolicy listening directory
Public class MailWatcher {
/** Log */
Private static final Logger LOG = LoggerFactory
. GetLogger (MailWatcher. class );
// Receiving path
Private String path;
Public MailWatcher (){
// Recipient email path
Path = Configure. getString ("path ");
}
// Listen to the main thread
Public void start (){
New Thread (new Runnable (){
@ Override
Public void run (){
Try {
Int mask = JNotify. FILE_CREATED | JNotify. FILE_DELETED
| JNotify. FILE_MODIFIED | JNotify. FILE_RENAMED;
// Listen to receive emails
Int watchId = JNotify. addWatch (path, mask, true, new MailListener ());
// Whether to listen to sub-Directories
Boolean watchSubtree = true;
// Log
LOG.info ("watcher start listening: {}{}", path, watchId );
} Catch (Exception e ){
LOG. error ("listener failed", e );
}
}
}). Start ();
// Prevent the main thread from being suspended
While (true ){
Try {
Thread. sleep (200 );
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
}
2. listener,
// Create a dynamic listener for a file
Public class MailListener implements jpolicylistener {
/** Log */
Private static final Logger LOG = LoggerFactory. getLogger (MailListener. class );
// When renaming a file
Public void fileRenamed (int wd, String rootPath, String oldName, String newName ){
LOG. debug ("{} renamed to {}", oldName, newName );
}
// When the file is modified
Public void fileModified (int wd, String rootPath, String name ){
LOG. debug ("{} modified", name );
}
// When a file is deleted
Public void fileDeleted (int wd, String rootPath, String name ){
LOG. debug ("{} deleted", name );
}
// When a file is created
Public void fileCreated (final int wd, final String rootPath, final String name ){
If (name. endsWith (". eml ")){
LOG.info ("{} created", name );
New Thread (new Runnable (){
@ Override
Public void run (){
LOG.info ("{}, start reading", name );
MimeMessage msg = null;
File file = new File (rootPath, name );
Try {
Thread. sleep (Configure. getLong ("wait_first_time "));
} Catch (Exception e ){
LOG. warn ("{}, initial wait failed", name );
}
// Wait 50 times for recycling, and wait 10 seconds each time, totaling 500 seconds
// It takes time to write the email, so the cycle is
For (int I = 0; I <Configure. getInt ("wait_times"); I ++ ){
Try {
// The encapsulation written here, file ---> mimemessage
Msg = EmlFileHelper. getMimeFile (file );
Break;
} Catch (Exception e ){
LOG. debug ("failed to read file {} Times", I + 1, name, e );
}
Try {
Thread. sleep (Configure. getLong ("wait_time_interval") * 1000l );
} Catch (InterruptedException e ){
LOG. debug ("Wait For failure", e );
}
}
If (msg = null ){
LOG. error ("{}, failed to read after retry", name );
Return;
}
If (! Configure. getBoolean ("not_intercept_send_email ")){
// Determine whether the email is sent. If yes, no records are recorded.
Try {
String explorer = name. substring (0, name. indexOf (File. separator ));
// Here is the email help class I wrote, ignore
String from = MailHelper. getFrom (msg );
If (from. startsWith (receiver + "@")){
LOG.info ("{} is the sent email, not recorded", name );
Return;
}
} Catch (Exception e ){
LOG. error ("failed to identify whether the email was sent", e );
}
}
Try {
SaveMsg (msg, name );
LOG.info ("{}, saved successfully", name );
} Catch (Exception e ){
LOG. error ("{}, failed to save", name, e );
}
}
}). Start ();
}
}
}
3. Enable listening,
Public class Main {
Public static void main (String [] args) throws Exception {
New MailWatcher (). start ();
}
}
4. Description: Enable the main thread in main, and file modification triggers the jnotify listening method,