Configuration by Exception


Coupled with the ability to specify behavior declaratively is the strong use of intelligent defaults in EJB. Much behavior
is attached automatically to an EJB or an entity without it being declared explicitly, such as the transactional behavior
of session bean methods and the names of the table and columns that map to an entity’s persistent data properties.
An annotation, or its counterpart in XML, needs to be specified explicitly only when non-default behavior is desired.
In the most common cases, where the default behavior is leveraged, this approach leads to very sparse, clean code.
This development model is known as configuration by exception, because only in exceptional (non-default) cases is it
necessary to configure the behavior of the component explicitly.

Ref : Beginning EJB 3 Java EE 7 Edition[p.3]



me on google plus+Jirawong Wongdokpuang

Install CouchDB 1.3.1 on Ubuntu


step 1
install required

sudo apt-get install make g++ 
sudo apt-get install erlang-base erlang-dev erlang-eunit erlang-nox
sudo apt-get install libmozjs185-dev libicu-dev libcurl4-gnutls-dev libtool 

step 2
download Source http://couchdb.apache.org/#download

wget http://mirror.symnds.com/software/
Apache/couchdb/source/1.3.1/apache-couchdb-1.3.1.tar.gz
tar xzvf apache-couchdb-1.3.1.tar.gz

step 3
install

cd apache-couchdb-1.3.1
./configure  
make  
sudo make install

step 4
start CouchDb

sudo couchdb


me on google plus+Jirawong Wongdokpuang

How to set Ubuntu proxy in Terminal


for HTTP Proxy

export http_proxy='http://USERNAME:PASSWORD@PROXY_IP:PROXY_PORT'

example

export http_proxy='http://localhost:3128'
export http_proxy='http://admin:passw0rd@localhost:3128'

for HTTPS Proxy

export http_proxy='https://USERNAME:PASSWORD@PROXY_IP:PROXY_PORT'

example

export http_proxy='https://localhost:3128'
export http_proxy='https://admin:passw0rd@localhost:3128'

reset proxy

unset http_proxy
unset https_proxy


me on google plus+Jirawong Wongdokpuang

Hibernate 4 Example (XML Mapping Style)


This is a quick guide to create hibernate project using hibernate xml mapping, in this example we use following tools

– Eclispe Kepler
– maven
– h2 database
– hibernate tools (eclipse plugin from jboss.org)

Install Hibernate Tools
first we need to install hibernate tools for eclipse using update site.

http://download.jboss.org/jbosstools/updates/stable/kepler/

in eclispe goto menu Help->Install new Software…
insert http://download.jboss.org/jbosstools/updates/stable/kepler/ to work with
and filter hibernate
then select Hibernate Tools and JBoss Maven Hibernate Configurator which showed in following image.

Hibernate Tools

click next to proceed. Once the installation is finished, eclispe will prompt to restart.
Continue reading “Hibernate 4 Example (XML Mapping Style)”

Quartz scheduler JobListener Example


This example will show how to run JobListener for track status of Quartz job.


above image show project structure (this project used maven)

1. Add denpendency
add Quartz maven dependency in pom.xml

        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.1.7</version>
        </dependency>

2. Create Job
create quartz job

package com.example.quartzlistener;

import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class SimpleJob implements Job {

    public void execute(JobExecutionContext jec) throws JobExecutionException {
        System.out.println("doing simple job. " + new Date());
    }
}

3. Create JobListener
create joblistener by implement JobListener interface

package com.example.quartzlistener;

import java.util.Date;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;

public class SimpleJobListener implements JobListener{

    public String getName() {
        return "SimpleJobListener";
    }

    public void jobToBeExecuted(JobExecutionContext context) {
        System.out.println("jobToBeExecuted has execute. "+new Date());
    }

    public void jobExecutionVetoed(JobExecutionContext context) {
        System.out.println("jobExecutionVetoed has execute. "+new Date());
    }

    public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
        System.out.println("jobWasExecuted has execute. "+new Date()+"\r\n");
    }
    
}

4. Run Job
Create file for execute job

package com.example.quartzlistener;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class SimpleScheduler {

    public static void main(String[] args) throws SchedulerException {
        JobDetail jobDetail = JobBuilder
                .newJob(SimpleJob.class)
                .withIdentity("SimpleJob")
                .build();

        Trigger trigger = TriggerBuilder
                .newTrigger()
                .withIdentity("SimpleJob")
                .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
                .build();

        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.getListenerManager().addJobListener(new SimpleJobListener());
        scheduler.start();
        scheduler.scheduleJob(jobDetail, trigger);
    }
}

run SimpleScheduler we will get result

jobToBeExecuted has execute. Fri Mar 29 18:52:35 ICT 2013
doing simple job. Fri Mar 29 18:52:35 ICT 2013
jobWasExecuted has execute. Fri Mar 29 18:52:35 ICT 2013

jobToBeExecuted has execute. Fri Mar 29 18:52:40 ICT 2013
doing simple job. Fri Mar 29 18:52:40 ICT 2013
jobWasExecuted has execute. Fri Mar 29 18:52:40 ICT 2013

from out put show jobToBeExecuted befor do job and after job complete will show jobWasExecuted ,
not jobExecutionVetoed has execute.

ok, jobExecutionVetoed has execute when job has veto by TriggerListener, i will show code to veto job below.

Create TriggerListener

package com.example.quartzlistener;

import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.TriggerListener;

public class SimpleTriggerListener implements TriggerListener{

    public String getName() {
        return "SimpleTriggerListener";
    }

    public void triggerFired(Trigger trigger, JobExecutionContext context) {
        System.out.println("triggerFired ");
    }

    public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context) {
        //return true when need veto job
        return true;
    }

    public void triggerMisfired(Trigger trigger) {
        System.out.println("triggerMisfired ");
    }

    public void triggerComplete(Trigger trigger, JobExecutionContext context, Trigger.CompletedExecutionInstruction triggerInstructionCode) {
        System.out.println("triggerComplete ");
    }
    
}

after that edit SimpleScheduler.java by add TriggerListener to Scheduler

...
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.getListenerManager().addJobListener(new SimpleJobListener());
        scheduler.getListenerManager().addTriggerListener(new SimpleTriggerListener());
        scheduler.start();
        scheduler.scheduleJob(jobDetail, trigger);
...

we will get output

triggerFired 
jobExecutionVetoed has execute. Fri Mar 29 18:59:50 ICT 2013
triggerFired 
jobExecutionVetoed has execute. Fri Mar 29 18:59:55 ICT 2013
triggerFired 
jobExecutionVetoed has execute. Fri Mar 29 19:00:00 ICT 2013
triggerFired 
jobExecutionVetoed has execute. Fri Mar 29 19:00:05 ICT 2013

jobExecutionVetoed has execute and job has veto.

Download Source From : GitHub



me on google plus+Jirawong Wongdokpuang

Oracle Database Change Notification JDBC Style Example


this code example test in oracle 11g R2 and run on user scott, first we must grant privilage for notification

grant change notification to scott;

this grant can use scott account grant to it self.

project structure need only one file (OracleDCN.java) and JDBC library

OracleDCN.java

package com.example.dcn;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import oracle.jdbc.OracleConnection;
import oracle.jdbc.OracleDriver;
import oracle.jdbc.OracleStatement;
import oracle.jdbc.dcn.DatabaseChangeEvent;
import oracle.jdbc.dcn.DatabaseChangeListener;
import oracle.jdbc.dcn.DatabaseChangeRegistration;

public class OracleDCN {
    static final String USERNAME = "scott";
    static final String PASSWORD = "tiger";
    static String URL = "jdbc:oracle:thin:@localhost:1521:stingdev";
    
    public static void main(String[] args) {
        OracleDCN oracleDCN = new OracleDCN();
        try {
            oracleDCN.run();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    private void run() throws Exception{
        OracleConnection conn = connect();
        Properties prop = new Properties();
        prop.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS, "true");
        DatabaseChangeRegistration dcr = conn.registerDatabaseChangeNotification(prop);
        
        try{
            dcr.addListener(new DatabaseChangeListener() {

                public void onDatabaseChangeNotification(DatabaseChangeEvent dce) {
                    System.out.println("Changed row id : "+dce.getTableChangeDescription()[0].getRowChangeDescription()[0].getRowid().stringValue());
                }
            });
            
            Statement stmt = conn.createStatement();
            ((OracleStatement) stmt).setDatabaseChangeRegistration(dcr);
            ResultSet rs = stmt.executeQuery("select * from EXAMPLE where ID=1");
            while (rs.next()) {
            }
            rs.close();
            stmt.close();
        }catch(SQLException ex){
            if (conn != null)
            {
                conn.unregisterDatabaseChangeNotification(dcr);
                conn.close();
            }
            throw ex;
        }
    }

    OracleConnection connect() throws SQLException {
        OracleDriver dr = new OracleDriver();
        Properties prop = new Properties();
        prop.setProperty("user", OracleDCN.USERNAME);
        prop.setProperty("password", OracleDCN.PASSWORD);
        return (OracleConnection) dr.connect(OracleDCN.URL, prop);
    }
}

when something change in EXAMPLE table event will triggle and print ROW_ID

after database change notification has registed we can check using query

select * from USER_CHANGE_NOTIFICATION_REGS;

Download Source From : Github

Ref : Database Change Notification



me on google plus+Jirawong Wongdokpuang

Quartz scheduler Example


This example show how to run Quartz with SimpleSchedule and CronSchedule. Quartz can download from official website

above image show project structure (this project used maven)

1. Add denpendency
add Quartz maven dependency in pom.xml

        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.1.7</version>
        </dependency>

2. Create Job
create Quartz Job by implement Job

package com.example.quartz;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class SimpleJob implements Job {

    public void execute(JobExecutionContext jec) throws JobExecutionException {
        System.out.println("doing simple job.");
    }
}

3. Create JobDetail
Create SimpleJob.java with detail

        JobDetail job = JobBuilder
                .newJob(SimpleJob.class)
                .withIdentity("SimpleJob")
                .build();

4. Create Trigger
Quartz has provide 2 type of trigger

  • SimpleTrigger (use SimpleScheduleBuilder)
  • CronTrigger (use CronScheduleBuilder)

Create SimpleSchedule

        Trigger trigger = TriggerBuilder
                .newTrigger()
                .withIdentity("SimpleJob")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever())
                .build();

Create CronSchedule

        Trigger trigger = TriggerBuilder
                .newTrigger()
                .withIdentity("SimpleJob")
                .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
                .build();

5. Start Job
Job Start use Scheduler execute

        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        scheduler.scheduleJob(job, trigger);

Full source code

package com.example.quartz;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class SimpleSchedule 
{
    public static void main( String[] args ) throws SchedulerException
    {
        JobDetail job = JobBuilder
                .newJob(SimpleJob.class)
                .withIdentity("SimpleJob")
                .build();
        
        Trigger trigger = TriggerBuilder
                .newTrigger()
                .withIdentity("SimpleJob")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever())
                .build();
        
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        scheduler.scheduleJob(job,trigger);
    }
}

Download Source From : Github



me on google plus+Jirawong Wongdokpuang