Friday, November 15, 2019

File Changes WatchService - When a file is updated in java


import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.stream.Collectors;

public class DirectoryWatchDemo {
public static void main(String[] args) {
try {
WatchService watcher = FileSystems.getDefault().newWatchService();
Path dir = Paths.get("D:\\WatchService");
dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
System.out.println("Watch Service registered for dir: " + dir.getFileName());
long initialCount = 0;
while (true) {
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException ex) {
return;
}

for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();

@SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path fileName = ev.context();

String filePath = dir.toString() + "\\" + fileName;

BufferedReader br1 = new BufferedReader(new FileReader(filePath));
long thisCount = br1.lines().count();
if (thisCount != 0) {

BufferedReader br2 = new BufferedReader(new FileReader(filePath));
System.out.println(br2.lines().skip(initialCount).collect(Collectors.toList()));
br2.close();
initialCount = thisCount;
}
br1.close();
/*
* if (kind == ENTRY_MODIFY &&
* fileName.toString().equals("DirectoryWatchDemo.java")) {
* System.out.println("My source file has changed!!!"); }
*/
}

boolean valid = key.reset();
if (!valid) {
break;
}
}

} catch (IOException ex) {
System.err.println(ex);
}
}
}

Output:
File in the Directory: D:\\WatchService\\ExampleText.txt

Watch Service registered for dir: WatchService
[Good Morning, How are you ?, Where are you going ?, When will you come?, GGood, Check, Good , Check, Show, Love You, Told you, Tuuu]
[]
[]
[Good]

Sunday, October 6, 2019

Login to MySQL Server in Ubuntu 18.0 .

Login to MySQL Server in Ubuntu 18.0 .

In the latest versions of the MySQL Server in Ubunte 18.0 .
Login to MySQL Server process is changed .
So, first we have to find the password  in the following way and then need to login to the MySQL Server.


nagaraju@nagaraju:~$ sudo cat /etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = LnW2fO2BdFpjj62L
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = GoodMan
socket   = /var/run/mysqld/mysqld.sock

nagaraju@nagaraju:~$ mysql -u debian-sys-maint -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.27-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql>

Friday, July 26, 2019

Rest API Response Types and Required Keys and Optional Keys


When setting up a JSON API, you'll have all kinds of different types of calls and responses. Send separates responses into some basic types, and defines required and optional keys for each type:
Type
Description
Required Keys
Optional Keys
success
All went well, and (usually) some data was returned.
status, data
fail
There was a problem with the data submitted, or some pre-condition of the API call wasn't satisfied
status, data
error
An error occurred in processing the request, i.e. an exception was thrown
status, message
code, data

Thursday, July 4, 2019

Eclipse Shortcut for Ubuntu Unity

Eclipse Shortcut for Ubuntu Unity

a shortcut to run Eclipse on Ubuntu Unity and to register Eclipse with the left Launcher

Installation

In your terminal,
cd ~/.local/share/applications
wget https://gist.github.com/raw/2922285/eclipse.desktop
chmod u+x eclipse.desktop
vim eclipse.desktop
# edit eclipse.desktop to replace "<ECLIPSE_DIR>" with your eclipse directory

Register Eclipse with Unity Launcher

  1. In your terminal, xdg-open ~/.local/share/applications to open the directory with the file browser
  2. Drag and drop the eclipse.desktop on Unity Launcher
# Eclipse Shortcut
# https://gist.github.com/2922285
[Desktop Entry]
Type=Application
Name=Eclipse
Comment=Eclipse Integrated Development Environment
Icon=<ECLIPSE_DIR>/icon.xpm
Exec=<ECLIPSE_DIR>/eclipse
Terminal=false
Categories=Development;IDE;Java;
StartupWMClass=Eclipse

Reference:

Friday, June 21, 2019

Example Remote Java Application Debugging

Step 1:

Create a Runnuable Jar file.

Step 2:

Run the Jar file as following:

java -Xdebug -Xrunjdwp:transport=dt_socket,address=0.0.0.0:8000,server=y,suspend=y -jar jar file

Example:



Step 3 : Eclipse Configurations and running.

Note : we should check the IP Address , Port Number is same .. Otherwise Debug will not work.
When you click on debug then the Jar fill will start running.
When we keep debug point then the Application will stop at the point.



Saturday, May 11, 2019

Having doubt and continuing a work

Experience : 

  • One day, I have given my bike for service in the morning . 
  • They have completed the service and i have started to servicing center to bring my bike.
  • When I started , There is raining and I have reached to servicing center by bus with helmet.
  • I have taken bike and started to come to home.
  • There is heavy raining and even though i am come to home . 
  • My mind have a doubt that I have mobile and it may be damaged because of the rain. 
  • But, i did not listen because of the mobile pouch. 
  • I have reached home and checked the mobile then i came to know that my mobile's sound is coming differently compared to previous sound.

Lesson :  Whenever , we have doubt on that we have to get clarity and that doubt and then go ahead. 

Effect of Postpone in Life

Experience : 

  • One day I came from Office and being ready to go to my home town.
  • On that day in my home water is going empty in the cans. 
  • So, my mind suggest me to bring the new water cans.
  • But , I did not listen to that and wasted my time and left home to home town.
  • I have completed some work at home town and come back to Bangalore on Sunday night .
  • I have went  to Office and come back in the evening.
  • Started preparing dinner then I came to know that cans are empty. So, I have called immediately to  cans to supplier . He told that there is raining so that it will take some time to deliver.
  • At this time, it take 3 hours to the cans supplier to come to my home and deliver the water.
  • I have realized at that moment , that because of postponement of water bringing before going to hometown, this happens.
Lesson:  we should not postpone of any work , if possible. if we do not do the work at that time , then when we have to do that work when we are busy.        

Recent Post

Databricks Delta table merge Example

here's some sample code that demonstrates a merge operation on a Delta table using PySpark:   from pyspark.sql import SparkSession # cre...