Friday, February 10, 2017

Java - redirect standard output stream to file


Buy Moto series phones: Moto E, Moto G, Moto X or Other phones
Buy eBooks from Flipkart
-----------------------------------------------


 We can redirect all System.out.println output to a file with below code snippet.

 System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("/tmp/output.txt"))));

Monday, January 30, 2017

Create a desktop shortcut in Mac with Automator


Buy Moto series phones: Moto E, Moto G, Moto X or Other phones
Buy eBooks from Flipkart
-----------------------------------------------


Open Automator
File > New
select Application
Then add "Run Shell Script" option
Give the command to run, here I am giving Google chrome canary with no proxy option

/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary --no-proxy-server=
Then Save with the name you want.







By default this will show automator icon.
Here is how to set your favorite icon.
Right click on Chrome canary icon and select "Get Info"



Then click on the icon and press Cmd+c
to copy the icon.
Now open info window on newly created icon, select the icon in the same place (which shows automator icon) press Cmd+v to paste the previously copied icon.


Change proxy settings only for Chrome


Buy Moto series phones: Moto E, Moto G, Moto X or Other phones
Buy eBooks from Flipkart
-----------------------------------------------

Disable proxy setting
Add command line parameter --no-proxy-server= to the command. (In windows, add this to the target path in the shortcut properties)

Specify different proxy
Add command line parameter --proxy-server="127.0.0.1:8080" to the command

Specify no proxy server list
Add command line parameter like this --proxy-bypass-list="127.0.0.1;localhost"


Friday, February 26, 2016

Find out license information of all maven dependencies


Buy Moto series phones: Moto E, Moto G, Moto X or Other phones
Buy eBooks from Flipkart
-----------------------------------------------

You want to use a maven dependency and you also need to know about the license information. If it is only one dependency you can easily check it. But each maven dependency more dependencies itself to do its work. That means you have to find out license information of all those jars also.

For example, if you want to use cassandra-unit jar (https://github.com/jsevellec/cassandra-unit) you have to know the license of this jar and 41 other jars also. You see how painful it is. To use one jar you have to find out information of 41 jars, think if you want to use some 10 jars, how many more jars you need to check.

Here maven plugin maven-project-info-reports-plugin comes to the rescue. This plugin can find out all the information you need.

I followed below steps to solve my problem.

Step 1:
I have created a new maven project in the eclipse and added my dependency (cassandra-unit). Then I have added maven-project-info-reports-plugin in the reports section.

Now pom file looks like below.

Step 2

Then run maven goal mvn project-info-reports:dependencies  and it will create a html file at target/site/dependencies.html which gives you all the information you need.

You can see license details of the Maven site plugin


POM.XML

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.reddy</groupId>
    <artifactId>SampleDependencies</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.cassandraunit</groupId>
            <artifactId>cassandra-unit</artifactId>
            <version>2.2.2.1</version>
        </dependency>

    </dependencies>



    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>2.6</version>
            </plugin>
        </plugins>
    </reporting>



</project>


Monday, November 3, 2014

Find out which Java thread is causing the high CPU usage


Buy Moto series phones: Moto E, Moto G, Moto X or Other phones
Buy eBooks from Flipkart
-----------------------------------------------

You see your java application is consuming high CPU but you don't know specifically which thread is consuming CPU out of thousand threads.

The solution is very simple.
1. First we need to find out native thread ID that is using high CPU. Issue top -H command on terminal and it will show all threads and the percentage of the CPU they are consuming. Find out your application thread from the list and note down native thread id which will be shown in the first column named PID.

2. Take thread dump of your application.

3. Thread dump contains native thread ID but in hexadecimal format. So convert the decimal thread ID we have noted in the first step to hexadecimal value and search for this value in the thread dump and you will find the thread's stack trace.

 You can see more detailed explanation in the source link below. It gives an example program and also details how to do this in both Unix and Windows.

----------------------------------------------------------------------------------------------
Source: https://blogs.oracle.com/jiechen/entry/analysis_against_jvm_thread_dump

Monday, May 26, 2014

To get http status code from curl call


Buy Moto series phones: Moto E, Moto G, Moto X or Other phones
Buy eBooks from Flipkart
-----------------------------------------------

We can use below command to get http response code of a URL from bash script.

status_code=$(curl --write-out "\n%{http_code}\n" --silent --output /dev/null --insecure https://www.google.co.in/)

silent - to stop progress information in output
output - we are re-directing normal output to null device
write-out - whatever things we needed can be given here. Here we are printing http status code. It will be printed after completion, i.e., after printing all output.
insecure - this allows connections to SSL sites without certs

References: http://stackoverflow.com/a/2220646/324900

Wednesday, May 7, 2014

Overriding logback file embedded in jar file


I was trying to run a jar file by setting it up in cronjob. Now this jar file contains a logback configuration file included in it which is causing me problem because all logs are being written to a specific file name, like processnamelogs.log. All runs of jar file will append to the same log file.

Now this is not what I wanted since the log file is going to grow huge in few runs. Also if all runs of jar file are writing to same log file it will be difficult to setup a monitoring script to check for errors.

What I wanted is that each run of jar file writes to a separate log file with its name containing start time of that run. So it will be easy to go through logs in case of errors. Also I can easily setup a monitoring script to only search in the log files with current date in their names.

So I need to write my own logback configuration file and a way to make sure the jar file uses my configuration file instead of embedded one. I have searched and got one system property (logback.configurationFile) which does what I wanted.

e.g.,
  java  -Xms128m -Xmx512m -XX:MaxPermSize=256m -jar -Dlogback.configurationFile=./mylogback.xml ./process.jar

mylogback.xml


<configuration>

  <!-- Insert the current time formatted as "yyyyMMdd'T'HHmmss" under
       the key "bySecond" into the logger context. This value will be
       available to all subsequent configuration elements. -->
  <timestamp key="bySecond" datePattern="yyyyMMdd'T'HHmmss"/>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <!-- use the previously created timestamp to create a uniquely
         named log file -->
    <file>log-${bySecond}.txt</file>
    <encoder>
      <pattern>%logger{35} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="DEBUG">
    <appender-ref ref="FILE" />
  </root>
</configuration>






References:
  • http://logback.qos.ch/manual/configuration.html#configFileProperty
  • http://logback.qos.ch/manual/appenders.html#uniquelyNamed












Wednesday, April 30, 2014

find out number of occurrences of a character in mysql


How to find out count of a specific character or a char-sequence in mysql?

There is no simple method available to do that. However we can use below method to find out.

select length(x) - length(replace(x, ":", ""))

Thursday, April 10, 2014

What is Heartbleed Bug


It is basically a security bug (buffer overread) which is in the Open SSL implementation which is used by a looooooooot of websites on the internet to secure ( :) ) the communication between browser and website.

Jacob on stackexchange explained this issue in detail. Read his answer at superuser.com

Sunday, January 26, 2014

to view only directories in Unix


If you have ever wanted to show a list of subdirectories underneath the current directory while at the terminal prompt, there’s a simple command that can help.
Simply run the following command from the bash terminal prompt, which filters the file listing to only show folders:

ls -l | grep ^d
Since typing that whole command every time you want to show a list of directories might get a little tiring, you can use the alias feature to assign it to something you can more easily remember:
alias lsd="ls -l | grep ^d"
Now when you type “lsd” at the prompt, you will see a list containing only the directories, which can be very useful when you are trying to navigate through a messy set of folders.

Latest Posts