Thursday, 3 April 2014

Spring Database connection with HSQLDB using datasource

Below is a simple example to try JDBC connection in Spring.  This example does not have the complete code


DAO

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.sql.DataSource;

public class EmployeeDAO {

    private DataSource datasource;

    public EmployeeDAO() {
        
    }
    
    public EmployeeDAO(DataSource datasource) {
        super();
        this.datasource = datasource;
    }


    public DataSource getDatasource() {
        return datasource;
    }


    public void setDatasource(DataSource datasource) {
        this.datasource = datasource;
    }


    public void countEmployee() throws Exception{
        Connection con = null;
        try {
            con = datasource.getConnection();
            PreparedStatement ps = con.prepareStatement("select count(*) from employeebimap");
            ResultSet rs = ps.executeQuery();
            if(rs.next())
            {
                System.out.println("select count(*) from employeebimap ---> "+ rs.getString(1));
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (con != null)
                con.close();
        }

    }
}




database-beans.xml in classpath

<beans br="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean br="" class="org.springframework.jdbc.datasource.DriverManagerDataSource" name="datasource">
        <property name="driverClassName" value="org.hsqldb.jdbc.JDBCDriver">
        <property name="url" value="jdbc:hsqldb:hsql://localhost/section1">
        <property name="username" value="sa">
        <property name="password" value="">
    </property></property></property></property></bean>

    <bean class="EmployeeDAO" name="EmployeeDAO">
        <property name="datasource" ref="datasource">
    </property></bean>

    <bean autowire="constructor" br="" class="EmployeeDAO" name="EmployeeDAOAutowired">
</bean>
</beans>



EmployeeDAOTest

import EmployeeDAO;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class EmployeeDAOTest {

    ApplicationContext ctx = new ClassPathXmlApplicationContext("database-beans.xml");
    
    @Test
    public void testEmployee() throws Exception{
    
        EmployeeDAO dao = (EmployeeDAO) ctx.getBean("EmployeeDAO");
        dao.countEmployee();
    }
    
    @Test
    public void testEmployeeAutowiredConstructor() throws Exception{
    
        EmployeeDAO dao = (EmployeeDAO) ctx.getBean("EmployeeDAOAutowired");
        dao.countEmployee();
    }

}


OUTPUT:

select count(*) from employeebimap ---> 2
select count(*) from employeebimap ---> 2


Monday, 31 March 2014

buildSessionFactory() after Hibernate 4


Configuration cfg = new Configuration().configure();
cfg.buildSessionFactory()


cfg.buildSessionFactory() is deprecated starting hibernate 4 onwards.


Following code can be used to create session factory starting from hibernate 4


Configuration cfg = new Configuration().configure();
ServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
factory = cfg.buildSessionFactory(registry);

Monday, 3 March 2014

[Book Review] Mastering Elasticsearch - Not for Begineers



Book Details
Language : English
Paperback : 386 pages [ 235mm x 191mm ]
Release Date : October 2013
ISBN : 178328143X
ISBN 13 : 9781783281435
Author(s) : Rafał KućMarek Rogoziński
Topics and Technologies : All Books, Big Data and Business Intelligence, Open Source



Review :  This books starts with introduction to lucene. The authors have organized the first few topics so well, that it gives very good insight for the people who are new to Lucene. This book is for advanced Elasticsearch users who are interested to know more about ES architecture and turning the performance. This book is NOT for beginners.

Friday, 14 February 2014

Jenkins - Setup to schedule your repetitive scripts

What is Jenkins & when to use It?

When you have bunch of repeated jobs or scripts that you want to run, scheduled or sometimes manually triggered. Go for jenkins.


Its as easy as 

  •    Step 1: download
  •    Step 2: Install
  •    Step 3: Install Basic Plugins (optional)
  •    Step 4: Setup security
  •    Step 5: Create new job(s)
  •    Run


Jenkins is also used these days in the case of continuous integration, working with the Source Control, Review Boards, and other quality monitoring tools.


Step 1 : Download  : http://jenkins-ci.org/

Based on your environment, you can download the installer as shown below and install it.



Step 2 : Install   - Follow instructions.

  • Follow the installer instructions.
  • On Linux, firewall to be disabled if it blocks 8080 port
    service iptables off
  • After installation accessible at  http://<hostname>:8080  . Example http://localhost:8080
You will see the below screen after successful installation & start-up


Step 3 : Install  Plugins (Optional)

There are some default plugins. If you need more, pick and choose from the plugin list as shown below.



For Manual installation.
  • Download plugins from https://updates.jenkins-ci.org/download/plugins/
  • copy the *.hpi/*.jpi file into the $JENKINS_HOME/plugins directory. 
  • Restart Jenkins (many containers let you do this without restarting the container)
I picked up few extra plugins Greenballs, XShell, setenv

Step 4 : Enable Security

1. Manage Jenkins -> Configure Global Security -> Enable Security.  




2. Configure as shown in the below picture.

  1. Enable Security
  2. Save  in jenkins database with Signup Enabled. This will enable you to add users later.
  3. Add privileges for admin user (which you will add through signup process)
  4. anonymous will have no privileges. Admin user - Select all checkboxes



  1. 5. Save, which will take you to the Login page as shown in the next image.

3. Signup for admin user from the Login Page. 



You are done with the basic security. You can go ahead and add other privileges using the admin user that is created.

Step 5 : Create new job




The dashboard now shows the job that was created.  and can now be run to see the progress

Some special settings when running on Linux Environment.

Jenkins in linux is created as a service and can be started using service jenkins start

The service will not be able access scripts or files within /root user  Say java -jar /root/test.jar will give you permission denied, even after setting the chmod appropriately. 

To solve this problem, the sudoers file has to be edited as mentioned below and execute the command prefixed with sudo. 

sudo java -jar /root/test.jar

Changes in the sudoers file
  • requiretty to be disabled for the jenkins service.  Add the Defaults:jenkins !requiretty  afterDefaults requiretty
    • visudo .(edit sudoers files)
    • Defaults requiretty
    • Defaults:jenkins !requiretty
  • add this line jenkins        ALL=(ALL)       NOPASSWD: ALL at the end

Friday, 7 February 2014

[Solution] Elasticsearch 0.90 insallation with .rpm and logging Errors

Problem : 

I downloaded the latest ES rpm and installed on centos. When tried running the script in foreground, i see that the logging is not enabled.

tried both /etc/init.d/elasticsearch -f and   /usr/share/elasticsearch/bin/ -f both. 

log4j:WARN No appenders could be found for logger (node).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.


and nothing gets logged.

Tried the following in vain : 
  • export ES_CLASSPATH=/etc/elasticsearch/logging.yml 
  • changed patch.conf, path.data in elasticsearch.yml file

But, Nothing seems to fix the logging problem

Solution:

edit /usr/share/elasticsearch/bin/elasticsearch  
give absolute paths for the home, and all files.

This file should be changed, so "any" startup of elasticsearch will work in foreground without errors


ES_JAVA_OPTS="-Des.config=/etc/elasticsearch/elasticsearch.yml -Des.path.conf=/etc/elasticsearch/ -Des.path.home=/usr/share/elasticsearch -Des.path.logs=/var/log/elasticsearch -Des.path.data=/var/lib/elasticsearch -Des.path.work=/tmp/elasticsearch -Des.path.plugins=/usr/share/elasticsearch/plugins"

Monday, 7 October 2013

[Book Review] ElasticSearch Server for fast scalable flexible search solution

ElasticSearch Server 



This is a good book for someone who wants understand and try out the latest search framework ElasticSearch built on top of Lucene. This claims to provide the best, flexible, distributed and enterprise Search Framework

It covers almost all the topic that is required
  1. Introduction to Elastic Search
  2. Understanding Shards, Nodes and cluster.
  3. Indexing and Searching Techniques
  4. Templates, Mapping
  5. Different API's
  6. Plugins.
  7. etc.,

I would suggest this book for someone interested in ElasticSearch Server.

Thursday, 26 September 2013

Java: Code to unquote string using Regex


str="\"hello\"" // or "'hello'"

public static unquote(String str)
{
        str = str.trim();
if (str.startsWith("'") && str.endsWith("'"))
{
    str = match(str, "(?<=').*(?=')");
}
else if (str.startsWith("\"") && str.endsWith("\""))
{
         str = match(str, "(?<=\").*(?=\")");
}
}

//matches the pattern
private static String match(final String string, final String regex)
{
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
matcher.find();
return matcher.group(); 
}