Tuesday, April 30, 2013

SQL :: Convert Integer to String

Convert Integer to String
you can convert an integer to string using the following query:

select CAST(ID as varchar2(50)) as ID_STRING from TABLE_NAME.

Have a nice day.

Sunday, April 28, 2013

Java :: Quartz

Steps to use Quartz 

1- Download Quartz
From http://quartz-scheduler.org/downloads/catalog and after free registration.
Extract the archive file and copy quartz-2.1.7.jar (or the name of the downloaded version) to your project libraries.

2- Download slf4j
From http://www.slf4j.org/download.html copy slf4j-api-1.7.5.jar (or the name of the downloaded version) to your project libraries.

3- Create Main Class and Implement Job Interface
Use the bellow example:


public static void main(String[] args) {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched;
try {
sched = sf.getScheduler();
JobDetail job = JobBuilder.newJob(Main.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);

} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

or you can download the source from:
https://github.com/firask86/QuartzExample

The example demonstrate simple and cron schedule.




Thursday, April 18, 2013

Java :: Singleton Pattern

Definition
Singleton pattern is one of the most commonly used patterns, it is used to ensure that only one instance of an object is created, and as in the implementation example you can see that a check for a STATIC variable if not exists create one (and it will be only the first time off-course) then the same instance will be returned always.
Focus on that the main constructor for the class should be implemented as private to make sure that no one can create an object for your class using else than getInstance method

Implementation

 public class SingletonClass {
   private static SingletonClass instance;

   private SingletonClass() {}

   public static SingletonClass getInstance() {
    if(instance == null) {
     instance = new SingletonClass();
     return instance;
    } else {
      return instance;
   }
 }