Thursday, May 23, 2013

Interrupt a Quartz's job using InterruptableJob

/* 
 * DemoInterruptableJob.java
 */

package com;

import java.util.Calendar;

import org.quartz.InterruptableJob;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.UnableToInterruptJobException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * An implementation of an InterruptableJob
 * 
* * @author thanooj kalathuru */
public class DemoInterruptableJob implements InterruptableJob { // logging services private static Logger _log = LoggerFactory.getLogger(DemoInterruptableJob.class); // job name private String _jobName = ""; private boolean isInterrupted = false; /** * * Empty constructor for job initialization *
*/
public DemoInterruptableJob() { } /** * * Called by the {@link org.quartz.Scheduler} when a * {@link org.quartz.Trigger} fires that is associated with the * Job. *
* * @throws JobExecutionException * if there is an exception while executing the job. */
public void execute(JobExecutionContext context) throws JobExecutionException { int i = 0; try { _jobName = context.getJobDetail().getFullName(); _log.info("---- " + _jobName + " executing at " + Calendar.getInstance().getTime()); while (i < 1000000000L) { if(isInterrupted){ _log.info("- inside isInterrupted -"); throw new InterruptedException(); }else{ if(i%100000000L == 0) _log.info("---- i:: " + i); i++; } } _log.info("---- " + _jobName + " completing at " + Calendar.getInstance().getTime()); }catch (InterruptedException e) { _log.info("- isInterrupted at --- i:: " + i); return; } } @Override public void interrupt() throws UnableToInterruptJobException { _log.info("--INTERUPTING-- "); isInterrupted = true; } } *********************************** /* * InterruptExample.java * */ package com; import java.util.Iterator; import java.util.List; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.JobExecutionContext; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.SchedulerMetaData; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class InterruptExample { private static Logger log = LoggerFactory.getLogger(InterruptExample.class); public void run() throws Exception { System.setProperty("org.quartz.scheduler.skipUpdateCheck", "true"); SchedulerFactory sf = new StdSchedulerFactory(); Scheduler scheduler = sf.getScheduler(); JobDetail job = new JobDetail("interruptableJob1", "group1", DemoInterruptableJob.class); CronTrigger trigger = new CronTrigger(); trigger.setName("dummyTriggerName"); trigger.setCronExpression("0/7 * * * * ?"); scheduler.scheduleJob(job, trigger); scheduler.start(); try { @SuppressWarnings("unchecked") List<JobExecutionContext> jobsList = (List<JobExecutionContext>) scheduler .getCurrentlyExecutingJobs(); Iterator<JobExecutionContext> jobsIterator = jobsList .listIterator(); log.info("----------jobsList size: " + jobsList.size()); while (jobsIterator.hasNext()) { JobExecutionContext context = (JobExecutionContext) jobsIterator .next(); log.info("----------" + context.getJobDetail().getFullName()); } Thread.sleep(3000L); log.info("----****--- Started interrupt Scheduler -----------------"); scheduler.interrupt(job.getName(), job.getGroup()); log.info("----****--- ended interrupt Scheduler -----------------"); } catch (Exception e) { e.printStackTrace(); } SchedulerMetaData metaData = scheduler.getMetaData(); log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs."); } public static void main(String[] args) throws Exception { InterruptExample example = new InterruptExample(); example.run(); } } ****************************************** 2013-05-23 17:05:06 INFO SimpleThreadPool:270 - Job execution threads will use class loader of thread: main 2013-05-23 17:05:06 INFO SchedulerSignalerImpl:60 - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl 2013-05-23 17:05:06 INFO QuartzScheduler:222 - Quartz Scheduler v.1.8.5 created. 2013-05-23 17:05:06 INFO RAMJobStore:139 - RAMJobStore initialized. 2013-05-23 17:05:06 INFO QuartzScheduler:244 - Scheduler meta-data: Quartz Scheduler (v1.8.5) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED' Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally. NOT STARTED. Currently in standby mode. Number of jobs executed: 0 Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads. Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered. 2013-05-23 17:05:06 INFO StdSchedulerFactory:1280 - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties' 2013-05-23 17:05:06 INFO StdSchedulerFactory:1284 - Quartz scheduler version: 1.8.5 2013-05-23 17:05:06 INFO QuartzScheduler:500 - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started. 2013-05-23 17:05:06 INFO InterruptExample:42 - ----------jobsList size: 0 2013-05-23 17:05:07 DEBUG SimpleJobFactory:50 - Producing instance of Job 'group1.interruptableJob1', class=com.DemoInterruptableJob 2013-05-23 17:05:07 DEBUG JobRunShell:215 - Calling execute on job group1.interruptableJob1 2013-05-23 17:05:07 INFO DemoInterruptableJob:56 - ---- group1.interruptableJob1 executing at Thu May 23 17:05:07 IST 2013 2013-05-23 17:05:07 INFO DemoInterruptableJob:64 - ---- i:: 0 2013-05-23 17:05:07 INFO DemoInterruptableJob:64 - ---- i:: 100000000 2013-05-23 17:05:07 INFO DemoInterruptableJob:64 - ---- i:: 200000000 2013-05-23 17:05:07 INFO DemoInterruptableJob:64 - ---- i:: 300000000 2013-05-23 17:05:08 INFO DemoInterruptableJob:64 - ---- i:: 400000000 2013-05-23 17:05:08 INFO DemoInterruptableJob:64 - ---- i:: 500000000 2013-05-23 17:05:08 INFO DemoInterruptableJob:64 - ---- i:: 600000000 2013-05-23 17:05:08 INFO DemoInterruptableJob:64 - ---- i:: 700000000 2013-05-23 17:05:09 INFO InterruptExample:49 - ----****--- Started interrupt Scheduler ----------------- 2013-05-23 17:05:09 INFO DemoInterruptableJob:77 - --INTERUPTING-- 2013-05-23 17:05:09 INFO InterruptExample:51 - ----****--- ended interrupt Scheduler ----------------- 2013-05-23 17:05:09 INFO DemoInterruptableJob:60 - - inside isInterrupted - 2013-05-23 17:05:09 INFO InterruptExample:57 - Executed 1 jobs. 2013-05-23 17:05:09 INFO DemoInterruptableJob:70 - - isInterrupted at --- i:: 781135745 2013-05-23 17:05:14 DEBUG SimpleJobFactory:50 - Producing instance of Job 'group1.interruptableJob1', class=com.DemoInterruptableJob 2013-05-23 17:05:14 DEBUG JobRunShell:215 - Calling execute on job group1.interruptableJob1 2013-05-23 17:05:14 INFO DemoInterruptableJob:56 - ---- group1.interruptableJob1 executing at Thu May 23 17:05:14 IST 2013 2013-05-23 17:05:14 INFO DemoInterruptableJob:64 - ---- i:: 0 2013-05-23 17:05:14 INFO DemoInterruptableJob:64 - ---- i:: 100000000 2013-05-23 17:05:14 INFO DemoInterruptableJob:64 - ---- i:: 200000000 2013-05-23 17:05:14 INFO DemoInterruptableJob:64 - ---- i:: 300000000 2013-05-23 17:05:15 INFO DemoInterruptableJob:64 - ---- i:: 400000000 2013-05-23 17:05:15 INFO DemoInterruptableJob:64 - ---- i:: 500000000 2013-05-23 17:05:15 INFO DemoInterruptableJob:64 - ---- i:: 600000000 2013-05-23 17:05:16 INFO DemoInterruptableJob:64 - ---- i:: 700000000 2013-05-23 17:05:16 INFO DemoInterruptableJob:64 - ---- i:: 800000000 2013-05-23 17:05:16 INFO DemoInterruptableJob:64 - ---- i:: 900000000 2013-05-23 17:05:16 INFO DemoInterruptableJob:68 - ---- group1.interruptableJob1 completing at Thu May 23 17:05:16 IST 2013

Thursday, May 9, 2013

TortoiseSVN's Settings - Save/Clear Data Settings



To check the saved SVN credentials - use TSvnPwd tool.

It will show you the cached credentials files in the path:
C:\Users\[username]\AppData\Roaming\Subversion\auth\svn.simple (windows7).

OR

goto TortoiseSVN -> Setting -> Saved Data ->  Authentication data and clear it.



For more details: tsvn-dug-settings

Tuesday, May 7, 2013

Generating Sonar reports using maven




Steps to follow the generate Sonar reports:
1. Download and Unzip the Sonar version corresponding to your Maven version from http://www.sonarsource.org/downloads/ . example – version 3.5.1
2. Download plugins from http://docs.codehaus.org/display/SONAR/Sonar+PDF+Plugin and place under the folder “sonar-3.5.1\extensions\plugins” which is downloaded in step 1.
3. Sonar dependency to the POM file

 4. Run the batch file \sonar-3.5.1\bin\windows-x86\StartSonar.bat.
5. Use the maven command mvn sonar: sonar to generate the Sonar report (You will have to increase the Heap size via the JMV arguments).
6. The report will be generated at localhost:9000
7. We have attached the sonar PDF reports in the mingle

Note: Increase Heap size in eclipse in case of generating sonar report from eclipse:
      -Xms512m -Xms1024m
If we use command prompt to generate sonar report the set environment variable:
MAVEN_OPTS: -Xmx1024m

Monday, May 6, 2013

Apache Commons Collections - shot note


Apache Commons Collections


1. Closures are functions that can alter the object and get a reference to each object in the collection.
2. Transformers are responsible for transforming data from one format to another or from one object  to another.
3. Predicates simply execute a conditional test against each item in a collection and return true or false for each item.

/**
 * model to show details.
 */
package com.apachecommonscollections.ApacheCommonsCollections;

/**
 * @author Thanooj
 * 
 */
public class ShowDetails {

 private int id;
 private String name;
 private String addr;

 public ShowDetails() {
  super();
 }

 public ShowDetails(int id, String name, String addr) {
  super();
  this.id = id;
  this.name = name;
  this.addr = addr;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getAddr() {
  return addr;
 }

 public void setAddr(String addr) {
  this.addr = addr;
 }

 @Override
 public String toString() {
  return name + " " + addr + " " + id;
 }

}

--------------------------
package com.apachecommonscollections.ApacheCommonsCollections;

import org.apache.commons.collections.*;

public class PrintMe implements Closure {
 // This class implements a Singleton Pattern
 private static PrintMe ourInstance = new PrintMe();

 /**
  * Get a singleton instance of PrintIt
  */
 public static PrintMe getInstance() {
  return ourInstance;
 }

 private PrintMe() // This is a singleton, dont change this!
 {
 }

 public void execute(Object o) {
  System.out.println(o.toString());
 }
}
-----------------
package com.apachecommonscollections.ApacheCommonsCollections;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import 
java.util.List;

import org.apache.commons.collections.Closure;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.Transformer;
import org.apache.commons.lang.StringUtils;

/**
 * 1. Closures are functions that can alter the object and get a reference to each object in the collection. 
 * 2. Transformers are responsible for transforming data from one format to another or from one object to another.
 * 3. Predicates simply execute a conditional test against each item in a collection and return true or false for each item.
 */
public class App {
 public static void main(String[] args) {
  System.out.println("1. Closures are functions that can alter the object and get a reference to each object in the collection.");

  List<ShowDetails> collectionOfShowDetails = Arrays.asList(
    new ShowDetails(1, "srirama", "ayodhya"), new ShowDetails(2,
      "seetha", "midhila"), new ShowDetails(3, "lakshmana",
      "ayodhya"), new ShowDetails(4, "hanuma", "kishkindha"));
  
  CollectionUtils.forAllDo(collectionOfShowDetails, new Closure() {
   public void execute(Object o) {
    ShowDetails showDetails = (ShowDetails) o;
    assert showDetails != null;
    String s = StringUtils.defaultIfEmpty(showDetails.getName(),
      "null");
    showDetails.setName("Jai! " + s);
   }
  });
  CollectionUtils.forAllDo(collectionOfShowDetails, PrintMe.getInstance());
  System.out.println("\n---------------------------------------------------------------------------------------------------------------");
  System.out.println("\n2. Transformers are responsible for transforming data from one format to another or from one object to another.");

  Collection<String> stringOfNumbers = Arrays.asList("0", "1", "2", "3", "4", "5");
  @SuppressWarnings("unchecked")
  Collection<Integer> ints = CollectionUtils.collect(stringOfNumbers,
    new Transformer() {
     public Object transform(final Object o) {
      return Integer.valueOf((String) o);
     }
    });
  CollectionUtils.forAllDo(ints, PrintMe.getInstance());
  System.out.println("\n-------------------------------------------------------------------------------------------------------------------------");
  System.out.println("\n3. Predicates simply execute a conditional test against each item in a collection and return true or false for each item.");
  
  List<String> charsNums = Arrays.asList("A", "0", "B", "1", "C", "3", "D", "4", "E", "5");  
    @SuppressWarnings("rawtypes")
    Collection numbersOnlyList = CollectionUtils.predicatedCollection(new ArrayList(),  
        new Predicate() {  
            public boolean evaluate(Object o) {  
                try {  
                    Integer.valueOf((String) o);  
                    return true;  
                } catch (NumberFormatException e) {  
                  return false;  
                }  
            }  
        });  
    for (String s : charsNums) {  
        try {  
            numbersOnlyList.add(s);  
        } catch (IllegalArgumentException e) {  
            System.out.println("Its a String!");  
        }  
    }  
    CollectionUtils.forAllDo(numbersOnlyList, PrintMe.getInstance());  
 }
}

 


Output: 

1. 
Closures are functions that can alter the object and get a reference to each object in the collection.
Jai! srirama ayodhya 1
Jai! seetha midhila 2
Jai! lakshmana ayodhya 3
Jai! hanuma kishkindha 4

---------------------------------------------------------------------------------------------------------------

2. Transformers are responsible for transforming data from one format to another or from one object to another.
0
1
2
3
4
5

-------------------------------------------------------------------------------------------------------------------------

3. Predicates simply execute a conditional test against each item in a collection and return true or false for each item.
Its a String!
Its a String!
Its a String!
Its a String!
Its a String!
0
1
3
4
5