Saturday, 18 January 2014

JMX Remote connection & VisualVM

JMX - Java Management Extensions

Enable remote access in tomcat. Include following options with JAVA_OPTS environment variables in tomcat start-up script.
  • -Dcom.sun.management.jmxremote 
  • -Dcom.sun.management.jmxremote.port=8999 
  • -Dcom.sun.management.jmxremote.authenticate=false 
  • -Dcom.sun.management.jmxremote.ssl=false 
  • -Dcom.sun.management.jmxremote.password.file=pw file path  
  • -Dcom.sun.management.jmxremote.access.file=access file path 
  • -Djava.rmi.server.hostname=(Optional)
You can place the password and permissions files in any location, such as "$CATALINA_HOME/conf/". Make sure to modify the permissions of these files so that they can only be viewed by users with appropriate access :
  • $ chmod 600 /path/to/pw/file
  • $ chmod 600 /path/to/access/file
Include in start-up scipt start_admin.sh

export CATALINA_OPTS="-XX:MaxPermSize=512m -Xms1024M -Xmx2048M -XX:NewRatio=3 -XX:MaxNewSize=700M -XX:-UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.password.file=$TOMCAT_HOME/conf/jmxremote.password -Dcom.sun.management.jmxremote.access.file=$TOMCAT_HOME/conf/jmxremote.access -Djava.rmi.server.hostname=$TOMCAT_HOST"



Process arguments



test@[49]/log% pargs -a 2288
2288:   /usr/java1.6/bin/java -Djava.util.logging.config.file=/opt/ITREfdsc/tomcat/conf
argv[0]: /usr/java1.6/bin/java
argv[1]: -Djava.util.logging.config.file=/opt/ITREfdsc/tomcat/conf/logging.properties
argv[2]: -DthisProcess=CEPServer
argv[3]: -Djava.awt.headless=true
argv[4]: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
argv[5]: -XX:MaxPermSize=512m
argv[6]: -Xms1024M
argv[7]: -Xmx2048M
argv[8]: -XX:NewRatio=3
argv[9]: -XX:MaxNewSize=700M
argv[10]: -XX:-UseConcMarkSweepGC
argv[11]: -XX:+UseParNewGC
argv[12]: -XX:+CMSParallelRemarkEnabled
argv[13]: -Dcom.sun.management.jmxremote
argv[14]: -Dcom.sun.management.jmxremote.port=8999
argv[15]: -Dcom.sun.management.jmxremote.ssl=false
argv[16]: -Dcom.sun.management.jmxremote.authenticate=true
argv[17]: -Dcom.sun.management.jmxremote.password.file=/opt/ITREfdsc/tomcat/conf/jmxremote.password
argv[18]: -Dcom.sun.management.jmxremote.access.file=/opt/ITREfdsc/tomcat/conf/jmxremote.access
argv[19]: -Djava.rmi.server.hostname=ceb-zone
argv[20]: -Djava.endorsed.dirs=/opt/ITREfdsc/tomcat/endorsed
argv[21]: -classpath
argv[22]: /opt/ITREfdsc/tomcat/bin/bootstrap.jar
argv[23]: -Dcatalina.base=/opt/ITREfdsc/tomcat
argv[24]: -Dcatalina.home=/opt/ITREfdsc/tomcat
argv[25]: -Djava.io.tmpdir=/opt/ITREfdsc/tomcat/temp
argv[26]: org.apache.catalina.startup.Bootstrap
argv[27]: start
fdsc@ceb-zone[50]/opt/ITREfdsc/log%


Java SE 6 HotSpot[tm] Virtual Machine Garbage Collection Tuning

Java HotSpot VM Options

--------------------------------------------------------------------------------------------------------------------------
-Djava.awt.headless=true
To use any AWT libraries in your server code on a machine that's not running a windowing interface
CATALINA_OPTS is immensely useful for setting debug parameters, Java memory settings, etc. One very common use is to set the system property java.awt.headless to true. Most graphical applications (like Jasper, JFreechart, LiquidOffice, StyleReport, etc.) will halt Tomcat when they render if something isn't done to disable display rendering, and this is by far the easiest method. (This problem occurs with most J2EE servers, not just Tomcat). Here's how to make this setting in UNIX.
--------------------------------------------------------------------------------------------------------------------------
-Djava.util.logging.config.file=/opt/ITREfdsc/tomcat/conf/logging.properties
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
To provide customised log file config. It is default behaviour of Tomcat and included in catalina.sh
--------------------------------------------------------------------------------------------------------------------------
-XX:MaxPermSize=512m
The permanent space is where are stored the class, methods, internalized strings, and similar objects used by the VM and never deallocated.
-XX:PermSize specifies the initial size that will be allocated during startup of the JVM. If necessary, the JVM will allocate up to -XX:MaxPermSize.
For most applications the permanent generation is not relevant to GC performance. However, some applications dynamically generate and load many classes. For instance, some implementations of JSPs do this. If necessary, the maximum permanent generation size can be increased with MaxPermSize.
--------------------------------------------------------------------------------------------------------------------------
-Xms1024M
-Xmx2048M
-Xms defines the initial size of the heap and -Xmx defines the maximum size of the heap.

--------------------------------------------------------------------------------------------------------------------------
 -XX:NewRatio=3
By default, the young generation size is controlled by NewRatio. For example, setting -XX:NewRatio=3 means that the ratio between the young and tenured generation is 1:3. In other words, the combined size of the eden and survivor spaces will be one fourth of the total heap size.
--------------------------------------------------------------------------------------------------------------------------

-XX:MaxNewSize=700M
-XX:NewSize

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

Run Visualvm


Create remote connection


View remote process details

References:

Wednesday, 15 January 2014

Setup GXT 3 examples in Eclipse


TODO : Update step by step guide
Reference:
http://www.gwtproject.org/
http://www.sencha.com/examples/
http://eclectide.com/blog/2013/10/11/compiling-sencha-gxt-showcase/
http://www.sencha.com/forum/showthread.php?248378-GWT-2.5-support

Wednesday, 27 November 2013

Java Socket Example


Trail: Custom Networking


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server {

    public static void main(String[] args) {
        new Server().startServer();
    }

    public void startServer() {
        final ExecutorService clientProcessingPool = Executors.newFixedThreadPool(10);

        Runnable serverTask = new Runnable() {
            public void run() {
                try {
                    ServerSocket serverSocket = new ServerSocket(9999);
                    System.out.println("Waiting for clients to connect...");
                    while (true) {
                        Socket clientSocket = serverSocket.accept();
                        clientProcessingPool.submit(new ClientTask(clientSocket));
                    }
                } catch (Exception e) {
                    System.err.println("Unable to process client request");
                    e.printStackTrace();
                }
            }
        };
        Thread serverThread = new Thread(serverTask);
        serverThread.start();

    }

    private class ClientTask implements Runnable {
        private final Socket clientSocket;

        private ClientTask(Socket clientSocket) {
            this.clientSocket = clientSocket;
        }

        public void run() {
            System.out.println("Got a client !");
            // Do whatever required to process the client's request
            try {
                System.out.println("Just connected to " + clientSocket.getRemoteSocketAddress());
                OutputStream outToServer = clientSocket.getOutputStream();
                DataOutputStream out = new DataOutputStream(outToServer);

                out.writeUTF("Hello from " + clientSocket.getLocalSocketAddress());

                InputStream inFromServer = clientSocket.getInputStream();
                DataInputStream in = new DataInputStream(inFromServer);
                System.out.println("Server says " + in.readUTF());
                clientSocket.close();

                try {
                    clientSocket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}



import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;

public class GreetingServer extends Thread {
    private ServerSocket serverSocket;

    public GreetingServer(int port) throws IOException {
        serverSocket = new ServerSocket(port);
        serverSocket.setSoTimeout(1000000);
    }

    public void run() {
        while (true) {
            try {
                System.out.println("Waiting for client on port " +
                        serverSocket.getLocalPort() + "...");
                Socket server = serverSocket.accept();
                System.out.println("Just connected to "
                        + server.getRemoteSocketAddress());

                DataInputStream in =
                        new DataInputStream(server.getInputStream());
                System.out.println(in.readUTF());
                DataOutputStream out =
                        new DataOutputStream(server.getOutputStream());
                out.writeUTF("Thank you for connecting to "
                        + server.getLocalSocketAddress() + "\nGoodbye!");
                server.close();
            } catch (SocketTimeoutException s) {
                System.out.println("Socket timed out!");
                break;
            } catch (IOException e) {
                e.printStackTrace();
                break;
            }
        }
    }

    public static void main(String[] args) {
        int port = Integer.parseInt(args[0]);
        try {
            Thread t = new GreetingServer(port);
            t.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}




import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class GreetingClient {
    public static void main(String[] args) {
        String serverName = args[0];
        int port = Integer.parseInt(args[1]);
        try {
            System.out.println("Connecting to " + serverName
                    + " on port " + port);
            Socket client = new Socket(serverName, port);
            System.out.println("Just connected to "
                    + client.getRemoteSocketAddress());
            OutputStream outToServer = client.getOutputStream();
            DataOutputStream out =
                    new DataOutputStream(outToServer);

            out.writeUTF("Hello from "
                    + client.getLocalSocketAddress());
            InputStream inFromServer = client.getInputStream();
            DataInputStream in =
                    new DataInputStream(inFromServer);
            System.out.println("Server says " + in.readUTF());
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



public class TestJava {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

 }

}

Friday, 1 November 2013

Oracle Query slow due to incompatible data types Date and Timestamp

The performance of sql produced by hibernate will be affected  due to incompatible data types. It appears that column data type(DATE) and hibernate data type(TIMESTAMP) mismatch causes implicit data type conversion. Oracle uses INTERNAL_FUNCTION to transfer date column to match the passed bind variable hibernate data type TimeStamp.

Resolve this Issue:


  • Use  ojdbc6 version 11 (http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#08_01)

  com.oracle
  ojdbc6
  11.2.0.4
  • CustomDateTimeUserType class defined to set the date time as java.sql.Date object to map with DB Data column.               
preparedStatement.setObject(i, timestamp, java.sql.Types.DATE)



User defined hibernate date type
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Date;

import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CustomDateTimeUserType implements UserType {

 private final Logger logger = LoggerFactory.getLogger(this.getClass());

 @Override
 public int[] sqlTypes() {
  return new int[] { Types.TIMESTAMP };
 }

 @SuppressWarnings("rawtypes")
 @Override
 public Class returnedClass() {
  return Date.class;
 }

 @Override
 public boolean equals(Object x, Object y) throws HibernateException {
  return x == y || !(x == null || y == null) && x.equals(y);
 }

 @Override
 public int hashCode(Object x) throws HibernateException {
  assert (x != null);
  return x.hashCode();
 }

 @Override
 public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
   throws HibernateException, SQLException {
  Timestamp timestamp = rs.getTimestamp(names[0]);
  if (rs.wasNull()) {
   return null;
  }
  return new Date(timestamp.getTime());
 }

 @Override
 public void nullSafeSet(PreparedStatement st, Object value, int index)
   throws HibernateException, SQLException {
  // if (value == null) {
  // st.setNull(index, Types.TIMESTAMP);
  //
  // }
  // else {
  // Date date = (Date) value;
  // Timestamp timestamp = new Timestamp(date.getTime());
  // st.setTimestamp(index, timestamp);
  // }
  if (value == null) {
   st.setNull(index, Types.DATE);

  } else {
   Date date = (Date) value;
   Timestamp timestamp = new Timestamp(date.getTime());
   st.setTimestamp(index, timestamp);
   // st.setDate(index, new java.sql.Date(date.getTime()));
   st.setObject(index, timestamp, java.sql.Types.DATE);
  }
 }

 @Override
 public Object deepCopy(Object value) throws HibernateException {
  return value;
 }

 @Override
 public boolean isMutable() {
  return false;
 }

 @Override
 public Serializable disassemble(Object value) throws HibernateException {
  return (Serializable) value;
 }

 @Override
 public Object assemble(Serializable cached, Object owner)
   throws HibernateException {
  return cached;
 }

 @Override
 public Object replace(Object original, Object target, Object owner)
   throws HibernateException {
  return original;
 }
}


References: