Showing posts with label General Utility. Show all posts
Showing posts with label General Utility. Show all posts

Thursday, 23 November 2017

Reading Jar file contents using java

Hi,

To read the jar file data, we can use JarFile and JarEntry classes and read files. Below is the video demo and code for the same.

Video Demo



Code:

package other;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class ReadJarFilesContents {

public static void main(String[] args) {
String JAR_PATH = "/home/sachin/created jar/url-scanner-0.0.1-SNAPSHOT.jar";
readJarContents(JAR_PATH);

}
public static void readJarContents(String jarFileToRead) {
JarFile jarFile = null;
try {
jarFile = new JarFile(jarFileToRead);
JarEntry entry = jarFile.getJarEntry("BOOT-INF/classes/application.properties");
InputStream inputStream = jarFile.getInputStream(entry);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader= new BufferedReader(inputStreamReader);
String read = null;
StringBuffer sb = new StringBuffer();
while((read = bufferedReader.readLine()) != null) {
    sb.append(read+ "\n");
}
System.out.println(sb.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

OutPut:

spring.datasource.url= jdbc:mysql://localhost:3306/test1
spring.datasource.username=sachin
spring.datasource.password=Sachin@123
#spring.jpa.hibernate.ddl-auto=create-drop
# Number of ms to wait before throwing an exception if no connection is available.
spring.datasource.tomcat.max-wait=10000

# Maximum number of active connections that can be allocated from this pool at the same time.
spring.datasource.tomcat.max-active=150

# Validate the connection before borrowing it from the pool.
spring.datasource.tomcat.test-on-borrow=true
# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.hibernate.ddl-auto = update

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

#logging
logging.level.org.springframework.web=ERROR
com.qualys.urlscanner=DEBUG
logging.file=/home/sachin/log/url-scanner.log



Sunday, 11 June 2017

Shell script to deploy build and start SpringBoot project on Linux/Ubuntu

Provide the below paths at top in below test.sh file:

Step1: Run with gradlew

create below gradle.sh file.

gradle.sh :
export GRADLE_HOME=/opt/gradle/gradle-3.0;
export PATH="$PATH:$GRADLE_HOME/bin";
cd <PROJECT_HOME_PATH>;
gradle clean bootRun -PdebugPort=8000  --debug ;
---------------------------------------------------------------------

$sh gradle.sh  ---for runing sh file



Step2: Run with nohup, So we can close console

for Better use, create below folders:
-- /<my-dir>/checkouts - Here cheeckout/copy springboot project
--/<my-dir>/deployment - here we we will add bootPackage run details from /<my-dir>checkouts/<project>/build/app-classpath/* and /<my-dir>checkouts/<project>/build/libs/<project-version.jar>

Steps:
1. checkout gradle project in /<my-dir>/<gradle_project>
2.  go to /<my-dir>/<gradle_project> and run below commands

chmod +x gradlew
./gradlew clean eclipse
./gradlew clean bootRepackage -Pprofile=<local/qa9>

3. copy /<my-dir>checkouts/<project>/build/app-classpath/* to <my-dir>/deployment/<gradle_project_name>/config/ folder. here we need to create confiig folder before copy

4.copy  /<my-dir>checkouts/<project>/build/libs/<project-version.jar> to /<my-dir>/deployment/<project>/
5. go to  /<my-dir>/deployment/<project>/

6. Run below nohup command

nohup java -Dloader.path="config/*" -Xms256m -Xmx1024m  -jar <project-version.jar> --spring.config.location=config/<gradle_project_name>.yml --logging.config=config/log4j2-spring.yml --spring.profiles.active=<local/qa9> > console.log &


To automate above steps in shell script: below is deploy.sh file:

Steps:
1. Put this deploy.sh file in checkouts/<projectName>/ path
2. Make sure Checkouts and deployment folder at parallel
3. run sh deploy.sh <profile_name> command from terminal

deploy.sh:
--------------------------------
project_root_dir="$PWD"
profile=$1;
project_name=$( echo $project_root_dir | rev | cut -d/ -f1 | rev);
echo "Project name: $project_name";
deployment_dir=$project_root_dir/../../deployment;
echo "Deploy project profile : $profile";
echo "project root dir: $project_root_dir";

cd $project_root_dir;
pwd;

cd $deployment_dir;
pwd;
echo "Creating project folder in deployment";
echo "Removing config and jar";
rm -rf $project_name/config $project_name/content*.jar;
echo "Removed config and jar";
mkdir $deployment_dir/$project_name;
cd $deployment_dir/$project_name;
pwd;
ls;
mkdir config;
echo "Dir created :$project_root_dir/../../deployment/config ";


echo "Creating jar and config the springBoot project";
cd $project_root_dir;
chmod +x gradlew;
./gradlew clean eclipse;
./gradlew clean bootRepackage -Pprofile=$profile;
date;
echo "Copying jar and config folder to deployment folder";
cp -r $project_root_dir/build/app-classpath/*  $deployment_dir/$project_name/config;
echo "Copied $project_root_dir/../../deployment/$project_name/config";
cd $project_root_dir/../../deployment/$project_name/config;
ls -lart;

cp -r $project_root_dir/build/libs/content*.jar  $deployment_dir/$project_name/;
echo "Copied jar file";
cd $deployment_dir/$project_name;
ls -lart;
echo "Runing Jar Dir:$pwd";

echo "Running jar with nohup";

echo "nohup java -Dloader.path=config/* -Xms256m -Xmx512m  -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:"$project_name"_gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=20000k -XX:+UseG1GC -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath="$project_name"_java_pid.hprof  -jar "$project_name"-1.0.0.jar --spring.config.location=config/"$project_name".yml --logging.config=config/log4j2-spring.yml --spring.profiles.active=$profile > console.log &";

nohup java -Dloader.path=config/* -Xms256m -Xmx512m  -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:"$project_name"_gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=20000k -XX:+UseG1GC -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath="$project_name"_java_pid.hprof  -jar "$project_name"-1.0.0.jar --spring.config.location=config/"$project_name".yml --logging.config=config/log4j2-spring.yml --spring.profiles.active=$profile > console.log & 

tail -f console.log;

-----------------------------------




Extract error records while inserting into db table using JDBCIO apache beam in java

 I was inserting data into postgres db using apache beam pipeline. it works perfectly with JdbcIO write of apache beam library. But, now, i ...