Showing posts with label Java APIs. Show all posts
Showing posts with label Java APIs. Show all posts

Friday, 25 March 2016

JUL vs. Log4j


JUL (Java Util Logging) vs. Log4j

Similarities
  • Log4j and JUL are almost conceptually identical.
    But JUL renamed some concepts : For example appenders are "handlers," layouts are "formatters," and LoggingEvents are "LogRecords"
  • JUL uses log levels the same way that Log4J uses levels.
    Although JUL has 9 default levels instead of 7
  • JUL organizes loggers in a hierarchy and JUL loggers inherit properties from their parent loggers.
    Same way Log4j organizes its loggers.

Differences
  • JUL uses API java.util.logging
  • Log4j uses API org.apache.commons.logging
  • JUL has Unclear log levels :
    SEVERE - WARNING - INFO - CONFIG - FINE - FINER - FINEST 
  • Log4j has Clear log level using different names :
    FATAL - ERROR - WARN - INFO - DEBUG - TRACE
  • No ERROR level in JUL
  • There is an ERROR level in Log4j
  • To log an exception in JUL, it is unclear what Level to use and why do I need to pass e.getMessage() parameter, Like :
    logger.log(Level.SEVERE, e.getMessage(), e);
  • Simpler logging syntax in Log4j, Like
    logger.error(e);
  • In JUL, there is no way to put a configuration in the classpath to be automatically picked up by the logging framework.
    To configure either modify the logging.properties in your JRE/lib folder
    OR 

    Set JVM parameter -Djava.util.logging.config.file=/my/folder/logging.properties
  • In Log4j, there is a possibility to put an already configured configuration file.
  • Another method to create a logger is missing in JUL, Like : 
    Logger.getLogger(class)


    Method to create logger is :
    private static Logger logger =
              Logger.getLogger(MyCLass.class.getName());

  • Log4j has method to create a logger with syntax : 
    private Logger logger = Logger.getLogger(getClass());

What is JUL (Java Util Logging) ?


Java Util Log­ging (JUL) is Java 's own log­ging frame­work to make log­ging more con­fig­urable then the pre­vi­ous usage of System.out.println(), System.err.println() and e.printStackTrace() usage.

JUL is provided by Java package java.util.logging (introduced in 2002 with Java 1.4)

Java Util Log­ging is not very devel­oper friendly.
It con­tains just a very basic way of log­ging, a method called log where you always have to indi­cate the level.

And those lev­els are not very clear for usage :
  • SEVERE
  • WARNING
  • INFO
  • CONFIG
  • FINE
  • FINER
  • FINEST

How to schedule jobs using XML in Quartz ?


Create “quartz.properties” and “quartz-config.xml” and ensure they are located at project classpath.

1. Create 3 Quartz’s jobs

public class Job1 implements Job {
   @Override
   public void execute(JobExecutionContext context) throws JobExecutionException {
      System.out.println("Job 1 is runing");
   }
}

public class Job2 implements Job {
   @Override
   public void execute(JobExecutionContext context) throws JobExecutionException {
      System.out.println("Job 2 is runing");
   }
}


2. Create quartz.properties

org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
org.quartz.plugin.jobInitializer.class =org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = quartz-config.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true


3. Create quartz-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<job-scheduling-data
  xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData
  <schedule>
    <job>
      <name>Job1</name>
      <group>myGroupy</group>
      <description>This is Job 1</description>
      <job-class>com.shaan.Job1</job-class>
    </job>

    <trigger>
      <cron>
        <name>myTriggerName1</name>
        <job-name>Job1</job-name>
        <job-group>myGroup</job-group>
        <!-- It will run every 5 seconds -->
        <cron-expression>0/5 * * * * ?</cron-expression>
      </cron>
    </trigger>
  </schedule>

  <schedule>
    <job>
      <name>Job2</name>
      <group>myGroup</group>
      <description>This is Job 2</description>
      <job-class>com.shaan.Job2</job-class>
    </job>

    <trigger>
      <cron>
        <name>myTriggerName2</name>
        <job-name>Job2</job-name>
        <job-group>myGroup</job-group>
        <!-- It will run every 5 seconds -->
        <cron-expression>0/5 * * * * ?</cron-expression>
      </cron>
    </trigger>
  </schedule>
</job-scheduling-data>


4. Entry in web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app ...>
  <listener>
    <listener-class> 
      org.quartz.ee.servlet.QuartzInitializerListener 
    </listener-class>
  </listener>
</web-app>

How to run multiple jobs in Quartz ?



In Quartz scheduler framework, each job will be attached to an unique trigger and run it by scheduler.

1. Create 3 Quartz’s jobs

public class Job1 implements Job { 
   @Override
   public void execute(JobExecutionContext context) throws JobExecutionException {
      System.out.println("Job 1 is runing");
   }
}

public class Job2 implements Job {
   @Override
   public void execute(JobExecutionContext context) throws JobExecutionException {
      System.out.println("Job 2 is runing");
   }
}

public class Job3 implements Job {
   @Override
   public void execute(JobExecutionContext context) throws JobExecutionException {
      System.out.println("Job 3 is runing");
   } 
}


2. Declare above 3 jobs, assign to 3 particular triggers and schedule it

JobKey jobKey1 = new JobKey("job1", "group1");
JobDetail job1 = JobBuilder.newJob(Job1.class).withIdentity(jobKey1).build();

JobKey jobKey2 = new JobKey("job2", "group1");
JobDetail job2 = JobBuilder.newJob(Job2.class).withIdentity(jobKey2).build();

JobKey jobKey3 = new JobKey("job3", "group1");
JobDetail job3 = JobBuilder.newJob(Job3.class).withIdentity(jobKey3).build();

Trigger trigger1 = TriggerBuilder.newTrigger().withIdentity("myTriggerName1", "group1")
    .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();
Trigger trigger2 = TriggerBuilder.newTrigger().withIdentity("myTriggerName2", "group1")
    .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();
Trigger trigger3 = TriggerBuilder.newTrigger().withIdentity("myTriggerName3", "group1")
    .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();

Scheduler scheduler = new StdSchedulerFactory().getScheduler();

scheduler.start();
scheduler.scheduleJob(job1, trigger1);
scheduler.scheduleJob(job2, trigger2);
scheduler.scheduleJob(job3, trigger3);