Thursday, May 21, 2015

Tomcat Not Accessible Outside Due To Blocked Port

You have installed Tomcat in your Linux machine. You can access it inside the machine using localhost:8080 or 127.0.0.1:8080. But when you try to connect to it remotely from another machine, socket connection is timing out and you are not able to access tomcat. That can be because of that specific port is blocked in Linux firewall. There's an easy way to check. Try to create a telnet connection to the specific host and port like:
telnet host-name 8080
If the connection is denied or you get a timeout, then the port is not open. So now question is how you can open the port from Linux firewall. To open a port for TCP connection is easy in Linux machine. You just have to use iptables program. Open a command line. Type below command:
iptables -I INPUT -p tcp --dport 8080 --syn -j ACCEPT
That's all. Now you can try to access tomcat on port 8080 remotely and it should work.
You may want to execute the following command after this:
service iptables save
This will persist the changes you made in iptables across system restarts.

Java Thread Interruption With join() Method Explained

This is an example how Java thread interrupt() works with join() method. Suppose there are two threads, t1 & t2. So t2 calls join method on t1 from a method like below:
try {
     t1.join();
} catch (InterruptedException e) {
      System.out.println("INTERUPPTED!");
}
Now the question is when will code enter into catch block. Do you need to call interrupt() method on the calling thread which in this case is t2? Or do you need to to call interrupt() on the thread on which join is called which in this case is t1?

I have written a simple program that will clarify this.


public class TestInterrupt {
   
    public static void main(String[] args) {
         R2 r2 = new R2();
         Thread t2 = new Thread(r2);
         t2.start();
    }
   
    private static class R1 implements Runnable {
        public void run() {
            while(true) {
                System.out.println("do nothing!");
            }
        }
    }
    private static class R2 implements Runnable {
        public void run() {
            R1 r1 = new R1();
            Thread t1 = new Thread(r1);
            t1.start();
            t1.interrupt();
            try {
                t1.join();
            } catch (InterruptedException e) {
                System.out.println("INTERUPPTED!");
                System.exit(0);
            }
        }
    }

}

If you look at the above code, basically t2 calls t1.join() method and before that it calls t1.interrupt() method. Now if you run it, it will keep on printing "do nothing" in the console. So that means using interrupt on the thread on which join() is called is not working. Otherwise it would have exited the program.
Now run the below code:


public class TestInterrupt {
   
    public static void main(String[] args) {
         R2 r2 = new R2();
         Thread t2 = new Thread(r2);
         t2.start();
    }
   
    private static class R1 implements Runnable {
        public void run() {
            while(true) {
                System.out.println("do nothing!");
            }
        }
    }
    private static class R2 implements Runnable {
        public void run() {
            R1 r1 = new R1();
            Thread t1 = new Thread(r1);
            t1.start();
            Thread t2 = Thread.currentThread();
            t2.interrupt();
            try {
                t1.join();
            } catch (InterruptedException e) {
                System.out.println("INTERUPPTED!");
                System.exit(0);
            }
        }
    }

}

It calls t2.join(). And if you execute it, the code would exit after a few seconds after printing "INTERUPPTED!". So that's how it works. You need to interrupt the thread that is calling join() method on another thread and it will throw InterruptedException.

Wednesday, May 6, 2015

How To Find Out How Many CPU Cores You Have In Windows

From command prompt, execute below command:
WMIC CPU Get /Format:List

Then check for NumberOfCores value. That will give total number of physical CPU cores that you have in you machine. And look for NumberOfLogicalProcessors value too. If Hyper-Threading is enabled, you will get double of total number of physical cores.
You can also go to Task Manager and check CPU Usage History under Performance tab. But the number of boxes there basically represents all the logical processors in your machine. My Windows laptop is dual core with Hyper-Threading enabled. So I will get four boxes there. But that doesn't represent the total number of physical CPU cores that I have which is 2. And there is no way to differentiate logical processors and physical cores there.


So it's better to use the above command. That will clearly display total number of physical cores and logical processors on a single screen.

Sunday, May 3, 2015

Take A Java Thread Dump With A Few Clicks

Using VisualVM, taking a thread dump can be a matter of few clicks. And not only local JVM, you can also connect to remote JVM irrespective of the underlying platform of the remote machine. It can be a Windows or a Linux machine, it doesn't matter as long as it has a JVM to connect to. You just need to specify the port to create a JMX connection to the remote host and that's it.





Check the above screenshot. It's a real time snapshot of local JVM threads in my Windows machine and at the right top corner you can see a button for thread dump. When you click on it, it will generate thread dump and will display it in ui console. Then you can copy or save it as you want. So no need to go to command line and type command to get thread dump in catalina.out or write it in a separate file. VisualVM will take care of it cleanly with a simple interface. VisualVM is shipped with Oracle JDK distribution by default. So you don't need to install it separately. Just go to JDK bin folder and click on jvisualvm.exe to start. It has other cool features. But today I just wanted to focus on taking a thread dump easily. I use it frequently in my Windows laptop to take a java thread dump from remote tomcat servers.

How To Find Java Installation Location In Linux

First run below command:
  • $ whereis -b java
The above command will search for Java binaries. And it will return some results like below:
java: /usr/bin/java /etc/java /usr/lib/java /usr/local/java

Next step would be to check whether the location returned is symbolic link or not. That can be easily found. Run below command:
  • $ ls -l /usr/bin/java
It returned something like below in my case:
lrwxrwxrwx. 1 root root 21 Dec 20  2013 /usr/bin/java -> /etc/alternatives/java

So the location was actually a symbolic link. Next run the following command using the above location:
  • $ ls -l /etc/alternatives/java
In my machine it returned result similar to below:
lrwxrwxrwx. 1 root root 46 Dec 20  2013 /etc/alternatives/java -> /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java

That's my actual Java location. So basically you have go deeper through the symbolic links and you will find your actual JDK location.