Tuesday 7 April 2015

Exception in hibernate-spring-data

Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set

Set the below property in the persistence.xml file

      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

Wednesday 28 January 2015

Nuke python console output

When you start a python/cherrypy web server, by default, will log onto the console. One can disable this by using property "log.screen" in global properties

log.screen = False

or cherrypy configuration in the code

cherrypy.config.update({..., 'log.screen':False,})

Read properties from configuration file in Python

It's always recommended to keep properties such as username, password, .. etc in .cfg files, instead of code. (Debate of appropriate/correct approach is out of context :) )

Let's say we have application configurations in a file "app.cfg". Contents of the "app.cfg" file:

[default]
host=abc.xyz.com
...
...

[mysql]
username=root
password=root
...

Let's look at the python code snippet to read the above application properties.

import ConfigParser

# class definition
class <class_name>:
    def __init__(self):
        ##### read properties
        config = ConfigParser.RawConfigParser()
        config.read(self.__file_path('app.cfg'))
        user = config.get('mysql', 'user')
        password = config.get('mysql', 'password')

# some other code funtionality
....


Default thread pool for Cherrypy web server

In the previous post, we have seen creating a web service using cherrypy. Cherrypy has thread default pool size as 10. One should modify/change the configuration by giving property "server.thread_pool".

Code snippet for configuration of thread pool is:

cherrypy.config.update({'server.socket_host': '0.0.0.0','server.socket_port': 8090,'server.thread_pool': 50,})

Note: For other attributes of cherrypy.quickstart(), please refer here

Tuesday 27 January 2015

Python web server using Cherrypy

Recently, I came across a situation where I was supposed to use python to build a web service. At that time, I didn't even know the syntax of python language. Thanks to Cherrypy module, it's very easy and robust.

I will be showing here the code snippet to create a web service (restful api end point)

(I am not giving steps to install python or cherrypy module installation)

Create a file, say, helloworld.py and add below content.

# import cherrypy module
import cherrypy

# class definition
class HelloWorldService(object):
     # this annotation exposes the behavior
    @cherrypy.expose
    # status behavior definition
    def status(self):
        return"OK"

    @cherrypy.expose
    # sayhello behavior with path params

    def sayhello(selfi, *args):
        pathParam = args[0]
        msg = "Hello %s" %(pathParam)
        return msg

# define configuration
if __name__ == '__main__':
    conf = {
          '/' : {
                        'tools.response_headers.on': True,
                        'tools.response_headers.headers': [('Content-Type', 'application/json'), ('Server', '')]
           }
    }

webapp = HelloWorldService()
# starting the server, by default it run on port 8080, we can change this un-comment below line

# cherrypy.config.update({'server.socket_host': '0.0.0.0','server.socket_port': 8090,})
cherrypy.quickstart(webapp, '/', conf)

After this, start the server.
$ python helloworld.py

Once it starts, try accessing below url.
http://localhost:8080/status/
http://localhost:8080/sayhello/sateesh/




Tuesday 18 November 2014

How to push maven artifacts to s3 bucket

I have written here about pushing third party artifacts to remote repository . Now, I will share here details about pushing artifacts to s3.

If we use default maven wagon connector to push artifacts to s3 buckett, we would come across below issue.

No connector available to access repository  <repository_name> (<repository_url>) of type default using the available factories WagonRepositoryConnectorFactory -> [Help 1]


To overcome this, we should use below code-snippet within <build> tags.

        <extensions>
            <extension>
                <groupId>org.springframework.build</groupId>
                <artifactId>aws-maven</artifactId>
                <version>${aws-maven.version}</version>
            </extension>
        </extensions>


Now, what does this code snippet do?

This means, adding the artifact "aws-maven" in the class path of maven build, so that aws-maven wagon connector will push artifacts to s3 bucket.

Wednesday 15 October 2014

Implementation of ThreadPoolExecutor

I will explain implementation of ThreadPoolExecutor.class here!


 ThreadPoolExecutor class has four constructors, using which one can obtain ThreadPoolExecutor instance.

(1)
public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), defaultHandler);
    }


(2)
public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             threadFactory, defaultHandler);
    }


(3)
public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              RejectedExecutionHandler handler) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), handler);
    }


(4)
public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {
        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        if (workQueue == null || threadFactory == null || handler == null)
            throw new NullPointerException();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }



Out of 4 constructors mentioned above, if you observer,  both 1 & 2, variable defaultHandler is passed to create the instance. So, what is defaultHandler?

private static final RejectedExecutionHandler defaultHandler =
        new AbortPolicy();



public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            throw new RejectedExecutionException("Task " + r.toString() +" rejected from " +  e.toString());
        }



defaultHandler throws RejecctedExecutionException whenever there is no thread available to execute the job.

If you want to have metric logs for thread rejection execution exception, it is recommended to create a separate implementation for RejectedExecutionHandler.

This can be done as mentioned below.

(a) Create RejectedExecutionHandlerImpl class

public class RejectedExecutionHandlerImpl implements RejectedExecutionHandler {

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        // log rejection metrics here
    }
}


(b) Create ThreadPoolExecutor

RejectedExecutionHandlerImpl rejectionHandler = new RejectedExecutionHandlerImpl();

ThreadFactory threadFactory = Executors.defaultThreadFactory();

ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(minThreadCount, maxThreadCount, threadAliveTime, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(poolQueueSize), threadFactory, rejectionHandler);