import ballerina/file;
import ballerina/log;
endpoint file:Listener inFolder {
path:"/home/ballerina/fs-server-connector/observed-dir",
recursive:false
};service localObserver bind inFolder {
onCreate (file:FileEvent m) {
string msg = "Create: " + m.name;
log:printInfo(msg);
}
onDelete (file:FileEvent m) {
string msg = "Delete: " + m.name;
log:printInfo(msg);
}
onModify (file:FileEvent m) {
string msg = "Modify: " + m.name;
log:printInfo(msg);
}
}
Directory ListenerThe Directory Listener can be used to listen to a directory in the local file system. It will keep listening to the specified directory and notify the create, delete and modify file actions in the directory. |
|
import ballerina/file;
import ballerina/log;
|
|
endpoint file:Listener inFolder {
path:"/home/ballerina/fs-server-connector/observed-dir",
recursive:false
};
|
In this particular scenario, the listener is listening to the directory specified by path and will not trigger any events that happen to child directories as the recursive property set to false. |
service localObserver bind inFolder {
onCreate (file:FileEvent m) {
string msg = "Create: " + m.name;
log:printInfo(msg);
}
onDelete (file:FileEvent m) {
string msg = "Delete: " + m.name;
log:printInfo(msg);
}
onModify (file:FileEvent m) {
string msg = "Modify: " + m.name;
log:printInfo(msg);
}
}
|
|
$ ballerina run directory-listener.bal
ballerina: initiating service(s) in 'directory-listener.bal'
Create: /home/ballerina/fs-server-connector/observed-dir/test1.txt
Modify: /home/ballerina/fs-server-connector/observed-dir/test1.txt
Delete: /home/ballerina/fs-server-connector/observed-dir/test1.txt
|
When the user creates a new file called test1.txt in observed-dir and delete it. |