Web Article - Quartz HelloWorld Example

pom.xml:


 4.0.0
 com.javacodegeeks.snippets.enterprise
 quartzexample
 0.0.1-SNAPSHOT

 
  
   org.quartz-scheduler
   quartz
   2.2.1
  
 


MyApp.java
package com.javacodegeeks.snippets.enterprise.quartzexample;

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

import com.javacodegeeks.snippets.enterprise.quartzexample.job.ByeJob;
import com.javacodegeeks.snippets.enterprise.quartzexample.job.HelloJob;

public class MyApp {

 public static void main(String[] args) {
  try {
   JobDetail job1 = JobBuilder.newJob(HelloJob.class).withIdentity("helloJob", "group1").build();

   Trigger trigger1 = TriggerBuilder.newTrigger().withIdentity("simpleTrigger", "group1")
     .withSchedule(SimpleScheduleBuilder.repeatSecondlyForTotalCount(30)).build();   
   Scheduler scheduler1 = new StdSchedulerFactory().getScheduler(); 
   scheduler1.start(); 
   scheduler1.scheduleJob(job1, trigger1); 
   
   JobDetail job2 = JobBuilder.newJob(ByeJob.class).withIdentity("byeJob", "group2").build();
   Trigger trigger2 = TriggerBuilder.newTrigger().withIdentity("cronTrigger", "group2")
     .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?")).build();
   Scheduler scheduler2 = new StdSchedulerFactory().getScheduler();
   scheduler2.start(); 
   scheduler2.scheduleJob(job2, trigger2); 
   }
  catch(Exception e){ 
   e.printStackTrace();
  }
 }

}

Source: javacodegeeks

Web Article - Tomcat DataSource JNDI Example


APACHE_TOMCAT_DIR/conf/context.xml:

< Resource auth="Container" 
driverClassName="com.mysql.jdbc.Driver" 
maxActive="100"  maxIdle="30" maxWait="10000"
name="jdbc/TestDB" type="javax.sql.DataSource" 
url="jdbc:mysql://localhost:3306/jcg?rewriteBatchedStatements=true" 
username="username"  password="password" />
web.xml:

MySQL Datasource example 
jdbc/TestDB 
javax.sql.DataSource 
Container 


DBConnection.java:
package com.javacodegeeks.examples;


import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


/**
 * @author Chandan Singh
 */
public class DBConnection
{
 public static Connection getConnection() throws SQLException, ClassNotFoundException, NamingException
 {
  Context ctx = new InitialContext();
  DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/TestDB");
  Connection connection = ds.getConnection();
                System.out.println(connection);

  return connection;
 }
}




Source: javacodegeeks.com

Powered by Blogger