Saturday, February 20, 2010

Working on remote linux server

Logging into remote linux server:
$ ssh username@remoteserver.com
Now enter your password and voila :D. Probably you know this. Just warming up a bit.

» What if ssh server is opened at a different port other than default 22?
If you server opened ssh in a different port, you can use this.
$ ssh -p 9009 username@remoteserver.com

» Want to transfer a war file to remote server?
$ scp demo.war username@remoteserver:/opt/apache/tomcat/webapps/
Or copying log files back to your machine?
$ scp -r username@remoteserver:/opt/apache/tomcat/logs /home/myself/

» You may not want to use password every time you log into the server.
You can create public-private key pair on you machine and share the public key with the remote server for ssh to use it.
Create key pair on your machine:
$ ssh-keygen -f ~/.ssh/id_fun
You'll find two files in ~/.ssh folder id_fun and id_fun.pub which are the private and public key respectively.
share id_fun.pub with the remote server and add to ~/.ssh/authorized_keys files
$ scp ~/.ssh/id_fun.pub username@remoteserver:/home/username/.ssh/
$ ssh username@remoteserver
# you need to enter password now
username@remoteserver$ cat ~/.ssh/id_fun.pub >> authorized_keys


Go back to you machine and try 'ssh username@remoteserver' and log into remote server without any password.
Do i have to tell you that you don't share id_fun with any one.

» Didn't like scp'ing. Why not mounting remote server's file system to yours?
$ mkdir /mnt/serverishere
$ sshfs
username@remoteserver:/home/username/eveything /mnt/serverishere
Now you can find everything folder is mounted to /mnt/serverishere on you machine.

Friday, February 19, 2010

Monitor you Java application remotely

Perhaps you used jconsole or newer jvisualvm to monitor Java application on your machine.
Or, may be you monitored your tomcat server remotely using this type of URI in jconsole
'service:jmx:rmi:///jndi/rmi://server:9024/jmxrmi'

Now, its time to do the same for the Java application you have written.
The idea to expose your platform MBean server through RMI

// get the MBean server.
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
// expose the MBean server through a service URL.
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/server");
JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null,mbs);
cs.start();

Ohh.. you have to open an RMI registry at port 9999, for sure.
rmiregistry 9999

Now, what are the benefits are you having monitoring remotely?
- First of all, using jconsole, jvisualvm can be a big performance issue for the application you are running on the same machine. Moreover, you production server is an Ubuntu server, so you cannot run Jconsole.

Monday, February 15, 2010

JDK useful commands and tools

jps - see all the PID's of all Java processes running on the machine

$ jps
11767 Jps
6854 Main
8205 Bootstrap

jstat - See how many classes are loaded by JVM for a Java program

$ jstat -class [pid]
Loaded Bytes Unloaded Bytes Time
10271 12074.2 0 0.0 11.10

jstack - Create thread dump
$ jstack [pid]

Useful usage of jmap

See summary of the heap memory
$ jmap -heap [pid]
Dump a heap file
$ jmap -dump:binary=b,file=[filename.dump] [pid]
Dump a heap file with only live objects
$ jmap -dump:live,binary=b,file=
Number of objects of a specific type
$ jmap -histo [pid] | grep org.example.MyClass

jinfo - To see system properties and VM flags
$ jinfo [pid]

Visual tools
jconsole - Graphical management console to see a number of attributes and graphs of a Java program. Simple usage:
$ jconsole [pid]

jvisualvm - This is newer than jconsole. Thread dump and heap dump creator and profiler for heap dump. This is also a monitoring tool like jconsole.

jhat - See the heap created. (probably this is not a very useful one. Some profiler/jvisualvm can be used)
$ jhat -port 8081 now.heap