Friday, August 21, 2015

Java Singleton Class

Java Singleton class

Singleton is one of the design pattern under the creation pattern category.
(There are three type of design patterns :
     1. Creational patterns
     2. Structural patterns
     3. Behavioral patterns
)

Below find the sample singleton class :

When you create the singleton class the following points are important.
1. Define the attribute with private and static modifier. (Line no : 3)
2. Create the private constructor (Line no : 12)
3. Create the instance when the instance is null. (Line no : 17)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.findanidea.singleton;

import java.util.logging.Logger;

public class SimpleSingleton {

 private static Logger LOG = Logger.getLogger("SimpleSingleton");

 private static SimpleSingleton simpleSingleton;

 // Enforce Singleton
 private SimpleSingleton() {
  // Singleton
 }

 public static SimpleSingleton getSingletonInstance(){
  if (simpleSingleton == null) {
   simpleSingleton = new SimpleSingleton();
   LOG.info("Simple Singleton instace created....");
  }
  return simpleSingleton;
 }
}

Is the above singleton class is thread safe ?

Just think..... 


Sunday, November 30, 2014

Tomcat JDBC Realm Security

Tomcat JDBC Realm Security Example

In previous article we already had a look on that, how to use the UserDatabase realm security and the web.xml configurations. In this article you able to learn, how to configure the JDBC realm in tomcat server and how to use the realm security in the web application. 

The main benefit of the JDBC realm is that, its possible to update or maintain the users and their relevant roles dynamically than server start up. In the UserDatabase Realm, the users are configured in 'tomcat-users.xml', which is loading when the server start up

The main objective of the tutorial is that, using the tomcat container set JDBC realm for the access the secure resources. In order to achieve the objective mentioned above, we are going to develop an web application with using eclipse and set the security access in application web.xml file. In addition to that, we are going to do small configuration in tomcat 'server.xml' file, in order to support the JDBC realm.

Requirements : 
  • Eclipse Java IEE IDE
  • JDK 1.6
  • Appache tomcat - 6.0.26
  • MySQL server 5.5
Step 1 : Create a table 'users' and 'users_roles' table in MySQL test database with using the following script.

create table users (
  user_name         varchar(15) not null primary key,
  user_pass         varchar(15) not null
);

create table user_roles (
  user_name         varchar(15) not null,
  role_name         varchar(15) not null,
  primary key (user_name, role_name)
);

Insert the test data to created table with using the below scrip. 
INSERT INTO users VALUES (
'test', 'test123'
);

INSERT INTO user_roles VALUES (
'test', 'VIP'
);

INSERT INTO users VALUES (
'john', 'john123'
);

INSERT INTO user_roles VALUES (
'john', 'Admin'
);

INSERT INTO users VALUES (
'ruki', 'ruki123'
);

INSERT INTO user_roles VALUES (
'ruki', 'Member'
);

INSERT INTO users VALUES (
'james', 'james123'
);

INSERT INTO user_roles VALUES (
'james', 'Member'
);

INSERT INTO users VALUES (
'mark', 'mark123'
);

INSERT INTO user_roles VALUES (
'mark', 'Member'
);


INSERT INTO users VALUES (
'manager', 'manager123'
);

INSERT INTO user_roles VALUES (
'manager', 'manager'
);

So far we completed the data setup. The next step we are going to configure the tomcat server for in order to support the JDBC realm security.

Step 2 : In this step we are going to look at the tomcat configuration for support JDBC Realm. In order to do that, first comment the following lines in the server.xml file located in apache-tomcat-6.0.26\conf directory. 

1
2
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>

Secondly add the following lines to the 'server.xml' file in which was located in apache-tomcat-6.0.26\conf directory.

1
2
3
4
5
<Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
   driverName="com.mysql.jdbc.Driver"
   connectionURL="jdbc:mysql://localhost:3306/test?user=root&amp;password=123"
   userTable="users" userNameCol="user_name" userCredCol="user_pass"
   userRoleTable="user_roles" roleNameCol="role_name"/>

In the above configuration we mentioned that My SQL driver, connection URL with username and password. In addition to that, we configured the users and user_roles table with the columns names. That's all for the tomcat configuration.

Step 3 : Create a Dynamic web project in Eclipse and name it as 'JDBCRealmSecurity'. The way of creating the dynamic web project as below. 
File > New > Dynamic Web Project

Step 4 : Create a 'index.jsp' file under the WebContent folder. In this jsp file contains the link to access secure jsp file name it as 'confidential.jsp'. 

Step 5 : Create the 'confidential.jsp' file under the WebContent\confidential directory. In this 'confidential.jsp' file contains 'This is confidential resource.' message. 

Step 6 : In this example we will use the 'web.xml' security configuration which we defined in the UserDatabase Realm Security article in step 5.

Step 7 : If run the 'JDBCRealmSecurity' project and access the 'confidential.jsp' page with click the URL defined in the 'index.jsp' the browser will ask the username and password. Until specify the VIP role user's username and password you unable to access the secure page.

Thursday, November 20, 2014

Create Web service client with using Axis2 in Eclipse

Create web service client with using Axis2 in eclipse

Web service helps to communicate the different application with out consuming more time even the applications are deployed in different platform. The reason behind was, the all communication is in XML. In previous tutorials briefly explain that how to create the WSDL and How to write the web service and deploy to the server and test. 

In this tutorial you able to learn that, to build the client with using the WSDL generated here

Objective : Create a web service client with using the Axis2 in eclipse.

Scope : Create we web service client with using the Axis2 and test the client service.

Requirements : 
  • Ecipse Java EE IDE
  • Axis2 - 1.6.2
  • Apache tomcat 6.0.26
Step 1 : Create a Dynamic web project  with using eclipse and name it as "ServiceClient". Right click on the "ServiceClient" project and create a web service client with using eclipse. 

The way of creating the web service client in eclipse mentioned as below. 

       New > Other > Web Service > Web Service Client


Step 2 : Chose the WSDL, which we created in the previous article. In additionally in the client generation wizard, select the Axis2 as web service run time and Tomcat 6 as a server. Click the next button to continue the wizard process.




Step 3 : Click the next button with out changing any of the parameters and finally click the finish button. Once you pressed the finish button, the auto generated code will added to the  "ServiceClient" project. (The auto generated classes are HelloworldCallbackHandler .java and HelloworldStub.java


Step 4 : In order to test the generated client, you have to write a separate class in java. In our example in order to test the client, you able to use the following "TestClient" class.

/ServiceClient/src/findanidea/webservice/services/helloworld/TestClient.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package findanidea.webservice.services.helloworld;

import java.rmi.RemoteException;

import findanidea.webservice.services.helloworld.HelloworldStub;
import findanidea.webservice.services.helloworld.HelloworldStub.SayHello;


public class TestClient {
 public static void main(String[] args) throws RemoteException {
  HelloworldStub stub = new HelloworldStub();
  SayHello sayHello = new SayHello();
  sayHello.setName("John");
  System.out.println(stub.sayHello(sayHello).getOutput());
 }
}


Once you execute the above mentioned "TestClient" class, you able to retrieve the "Hello... John" message in application console.



Hope its helps.... 

Sunday, November 16, 2014

Tomcat UserDatabase Realm Security


Tomcat UserDatabase Realm Security example

Authentication can be controlled by application level, or by the container level (for example tomcat or jboss) that the application runs in. In this tutorial we are going to explain the example with using the tomcat container.

In the tomcat's container the application security managed by based on the realm configured. 

In this tutorial you can learn that, how to set up the UserDatabase realm (Which is default active realm in tomcat configuration), and how to define the security details in web.xml file. 

Object : The main objective of the tutorial is that, using the tomcat container set UserDatabase realm for access the secure resources.

Scope : In order to achieve the objective mentioned above, we are going to develop a web application with using eclipse and set the security access details in web.xml file for access the secure resources with using UserDatabase realm.

Requirements : 
  • Eclipse Java IEE IDE
  • JDK 1.6
  • Apache Tomcat - 6.0.26

Step 1 : First we have to set the tomcat users and roles for access the secure resources. In order to achieve that add the following specific roles and user credentials to "tomcat-users.xml" file. You have to specify the same username and password for when access the secure resources.

1
2
3
4
5
6
<tomcat-users>
  <role rolename="VIP"/>
  <role rolename="ADMIN"/>
  <user username="vip" password="vip123" roles="VIP"/>
  <user username="admin" password="admin123" roles="ADMIN"/>
</tomcat-users>

In the above file we specified two roles VIP, ADMIN and their credential details. If user have the VIP privileges they have to specify the username and password are respectively 'vip'  and 'vip123'.
If the user have the ADMIN privileges then the username and password are respectively 'admin' and 'admin123'.

Step 2 : Create a Dynamic web project with using the Eclipse and Name it as "UserDataBaseAuthentication". The path of create the dynamic web project as below.
                   File > New > Dynamic Web Project

Step 3 : Create a "index.jsp" file under the WebContent folder. The index file have link to access the secure jsp file name it as "confidential.jsp"

/UserDataBaseAuthentication/WebContent/index.jsp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>findanidea.blogspot.com</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <a href="confidential/confidential.jsp">Access the connfidential page.</a>
    </body>
</html>

Step 4 :  Create a "confidential.jsp" file under the WebContent\confidential folder. The file have "This is Confidential resource." message.

Step 5 : Now the time to configure the security elements to "web.xml". For the authentication process we are going to add the <security-role>,<login-config> and <security-constraint> tags to web.xml file.

/UserDataBaseAuthentication/WebContent/WEB-INF/web.xml


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 <security-role>
  <role-name>VIP</role-name>
 </security-role>

 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>

 <security-constraint>
  <web-resource-collection>
   <web-resource-name>CONFITEST</web-resource-name>
   <http-method>POST</http-method>
   <http-method>GET</http-method>
   <url-pattern>/confidential/*</url-pattern>
  </web-resource-collection>
  <auth-constraint>
   <role-name>VIP</role-name>
  </auth-constraint>
  <user-data-constraint>
   <!-- transport-guarantee can be CONFIDENTIAL, INTEGRAL, or NONE -->
   <transport-guarantee>NONE</transport-guarantee>
  </user-data-constraint>
 </security-constraint>

In this example we define the role name as VIP only have the access the resources inside the folder 'confidential'. In addition to that the secure resources can be access via GET and POST http methods only.

There are four type of authentication types available such as BASIC, FORM, DIGEST and CLIENT-CERT. In this example we used BASIC authentication type.

We can specify three type of transport guarantee parameters such as NONE, CONFIDENTIAL and INTEGRAL in the line number 21. 

Step 6 : When you run the "UserDataBaseAuthentication" project, the "index.jsp" file will display in browser. When try to access the "confidential.jsp" via click the link, the browser prompt will ask the username and password details for authenticate before access the resource. If VIP privilege user enter the correct username and password then the user able to access the confidential page. Otherwise they will get error message.



Friday, November 7, 2014

View Remote Server Console with using Java


View Remote Server Console with using Java

In the software development, the developer spend significant amount of time for debug the code and reproduce the bug for find the issues and solutions. In the JEE applications almost all the errors are identified through the servers console. The best practice, developers debug the code in their local server and try to reproduce the bug, in case if it is not possible, download the remote server logs local machines and identify the issues.

The following tutorial going to explain that, how we are going to view the remote server console in local machine with using the Java application. It may helps to developers to reduce the time for analysis.

Objective : The main objective of the tutorial is view the remote server console with using Java application.

Scope : Write a simple java code for view the server console which was run remotely.

Requirements : The below basic requirements we need to set it in order to achieve our objective.
  • Eclipse Java EE IDE 
  • jsch-0.1.50.jar. The jar you able to download from here.

Step 1 : Create a new Java project in Eclipse and name it as "ViewRemoteServerConsole".
The path of creating the new Java project is below mentioned.
                    File > New > Java Project

Step 2 : Create a "RemoteConnector" java class under the "com.findanidea.remote.connector" package.

The following class create the channel with using jsch along with given input parameters.

/ViewRemoteServerConsole/src/com/findanidea/remote/connector/RemoteConnector.java


package com.findanidea.remote.connector;

import java.io.InputStream;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class RemoteConnector {

 public static void main(String[] arg) {

  try {
   JSch jsch = new JSch();
   String host="",user="",passwd = null;

   if (arg.length == 3) {
    host = arg[0];
    user = arg[1];
    passwd = arg[2];
   }else{
    System.out.println("Wrong parameters... ");
    System.out.println("Please provide the host as first parameter...");
    System.out.println("Please provide the user as second parameter...");
    System.out.println("Please provide the password as third parameter...");
   }
   Session session = jsch.getSession(user, host, 22);
   session.setPassword(passwd);

   ServerDetails ui = new ServerDetails() {
    public boolean promptYesNo(String message) {
     return true;
    }
   };

   session.setUserInfo(ui);
   session.connect(3000); 

   Channel channel = session.openChannel("shell");
   channel.setInputStream(System.in);
   channel.setOutputStream(System.out);


   InputStream in = channel.getInputStream();
   channel.connect(3 * 1000);
   byte[] tmp = new byte[1024];
   while (true) {
    while (in.available() > 0) {
     int i = in.read(tmp, 0, 1024);
     if (i < 0)
      break;
     System.out.print(new String(tmp, 0, i));
    }
    if (channel.isClosed()) {
     System.out.println("Channel is Coluse : Status is : " + channel.getExitStatus());
     break;
    }
    try {
     Thread.sleep(1000);
    } catch (Exception ee) {
    }
   }

  } catch (Exception e) {
   System.out.println(e);
  }
 }

}

In the above class file expects the 3 run time input parameters. There are,
1. host (ex: 172.19.11.11)
2. username (ex : testuser)
3. password (ex : password)


Based on the above mentioned parameters the channel will create and connect to the remote server console. 

Step 3 : Create a "ServerDetails" class under the same "com.findanidea.remote.connector" package.



package com.findanidea.remote.connector;

import com.jcraft.jsch.UIKeyboardInteractive;
import com.jcraft.jsch.UserInfo;

public class ServerDetails implements UserInfo,UIKeyboardInteractive {

 @Override
 public String[] promptKeyboardInteractive(String arg0, String arg1, String arg2, String[] arg3, boolean[] arg4) {
  return null;
 }

 @Override
 public String getPassphrase() {
  return null;
 }

 @Override
 public String getPassword() {
  return null;
 }

 @Override
 public boolean promptPassphrase(String arg0) {
  return false;
 }

 @Override
 public boolean promptPassword(String arg0) {
  return false;
 }

 @Override
 public boolean promptYesNo(String arg0) {
  return false;
 }

 @Override
 public void showMessage(String arg0) {
 }

}

All right... We did it... Now the time to test the application... 


If you execute the "RemortConnector" class with the input parameters such as remote server host, username and password, you able to see the remote server console with in you application console. 



You can find here the way for set the run time parameters in eclipse.

Hope its help to you..

Tuesday, November 4, 2014

Quartz Scheduler Application in Java

Quartz Scheduler Simple Application in Java

Most of the time Quartz is helps to Java developers to build the schedule based applications. In the Quartz, developers able to schedule the job for predefined date and time. Developers able to define multiple schedule jobs in a single application.

In this tutorial you able to learn for how to schedule the simple job with using Quartz. 

Objective : In this tutorial we are going to develop a scheduler application with using Quartz and print the Welcome message in console at predefined time period.

Scope : Develop a Scheduler application with using Quartz.

Requirements : In order to achieve the above objective we need to set it up the below items.
  • Eclipse Java EE IDE
  • Maven

Step 1 : Create a Maven project as "SpringQuartzSample" with using Eclipse. 

While you creating the project provide the input parameters are below.
 - New > Other > Maven > Maven Project
 - Click Next button with out modify any default settings.
 - The default archetype is "maven-archetype-quickstart". No need to change any of the default setting click Next.
 - Enter "TestQuartz" as GroupId, "SpringQuartzSample" as artifactId and "com.findanidea.quartz.service" as package .

Step 2 :  Update the pom.xml as mentioned below.
In this pom.xml file we specified the spring also one of the dependency in addition of quartz.

/SpringQuartzSample/pom.xml


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<project
    xmlns = "http://maven.apache.org/POM/4.0.0"
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>TestQuartz</groupId>
    <artifactId>SpringQuartzSample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>SpringQuartzSample</name>
    <url>http://maven.apache.org</url>
    <properties>
        <spring.version>3.1.1.RELEASE</spring.version>
        <log4j.version>1.2.16</log4j.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.2</version>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>opensymphony</groupId>
            <artifactId>quartz</artifactId>
            <version>1.6.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
    </dependencies>
</project>


Step 3 : Create "TestService" class under the package defined in the step 1.

The following service method will invoke the time we specified as cron expression. 

/SpringQuartzSample/src/main/java/com/findanidea/quartz/service/TestService.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package com.findanidea.quartz.service;

import java.util.Date;
import org.springframework.stereotype.Service;

@Service
public class TestService {
 public int counter = 0;

 public void testServiceMethod() {
  Date date = new Date();
  System.out.println(counter + "  - The testServiceMethod() invoked: "
      + date.toString() + "."
      + " Welcome to findanidea.blogspot.com");
  counter++;
 }
}


Step 4 : Now the time to play with the spring configurations. In the below spring configuration file we defined the following items.

1. TestService class defined in the line number 11 as testService Bean.
2. In the line number 16 configured the testServiceMethod as targetMethod in jobDetail bean.
3. The cron expression configured in the line number 22 as cronExpression property in the cronTrigger bean.
4. Finally the cron trigger added to the schedule factory bean at line number 28.

/SpringQuartzSample/src/main/resources/spring-config.xml


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">


 <!-- Our test service bean -->
 <bean id="testService" class="com.findanidea.quartz.service.TestService" />

 <bean id="jobDetail"
  class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <property name="targetObject" ref="testService" />
  <property name="targetMethod" value="testServiceMethod" />
  <property name="concurrent" value="false" />
 </bean>

 <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail" ref="jobDetail" />
  <property name="cronExpression" value="0/2 * * * * ?" />
 </bean>

 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
   <list>
    <ref bean="cronTrigger" />
   </list>
  </property>
  <property name="quartzProperties">
   <props>
    <prop key="org.quartz.scheduler.skipUpdateCheck">true</prop>
   </props>
  </property>
 </bean>

</beans>


You able to define multiple schedule jobs with different cron expression in a single "SchedulerFactoryBean" bean.

That's all...  Now move to next step for test the quartz service.

Step 5 : Create a "RunTest" class under the "com.findanidea.quartz.service" package. In the following class we specified that, the thread sleep time at 30 sec.

/SpringQuartzSample/src/main/java/com/findanidea/quartz/service/RunTest.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.findanidea.quartz.service;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RunTest {
 public static void main(String[] args) {

  ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
  System.out.println("*************************************************************************");
  System.out.println("***************** The Quarts Service going to Start. ********************");
  System.out.println("*************************************************************************");
  try {
   Thread.sleep(30000);
  } catch (InterruptedException ex) {
   System.out.println(ex.getMessage());
  }
  System.out.println("*************************************************************************");
  System.out.println("***************** The Quarts Service Stoped. ********************");
  System.out.println("*************************************************************************");
  applicationContext.stop();

 }
}

Once execute the RunTest class, then you able to see the below output in your console...


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
INFO: Scheduler org.springframework.scheduling.quartz.SchedulerFactoryBean#0_$_NON_CLUSTERED started.
*************************************************************************
***************** The Quarts Service going to Start. ********************
*************************************************************************
0  - The testServiceMethod() invoked: Tue Nov 04 12:53:54 IST 2014. Welcome to findanidea.blogspot.com
1  - The testServiceMethod() invoked: Tue Nov 04 12:53:56 IST 2014. Welcome to findanidea.blogspot.com
2  - The testServiceMethod() invoked: Tue Nov 04 12:53:58 IST 2014. Welcome to findanidea.blogspot.com
3  - The testServiceMethod() invoked: Tue Nov 04 12:54:00 IST 2014. Welcome to findanidea.blogspot.com
4  - The testServiceMethod() invoked: Tue Nov 04 12:54:02 IST 2014. Welcome to findanidea.blogspot.com
5  - The testServiceMethod() invoked: Tue Nov 04 12:54:04 IST 2014. Welcome to findanidea.blogspot.com
6  - The testServiceMethod() invoked: Tue Nov 04 12:54:06 IST 2014. Welcome to findanidea.blogspot.com
7  - The testServiceMethod() invoked: Tue Nov 04 12:54:08 IST 2014. Welcome to findanidea.blogspot.com
8  - The testServiceMethod() invoked: Tue Nov 04 12:54:10 IST 2014. Welcome to findanidea.blogspot.com
9  - The testServiceMethod() invoked: Tue Nov 04 12:54:12 IST 2014. Welcome to findanidea.blogspot.com
10  - The testServiceMethod() invoked: Tue Nov 04 12:54:14 IST 2014. Welcome to findanidea.blogspot.com
11  - The testServiceMethod() invoked: Tue Nov 04 12:54:16 IST 2014. Welcome to findanidea.blogspot.com
12  - The testServiceMethod() invoked: Tue Nov 04 12:54:18 IST 2014. Welcome to findanidea.blogspot.com
13  - The testServiceMethod() invoked: Tue Nov 04 12:54:20 IST 2014. Welcome to findanidea.blogspot.com
14  - The testServiceMethod() invoked: Tue Nov 04 12:54:22 IST 2014. Welcome to findanidea.blogspot.com
*************************************************************************
***************** The Quarts Service Stoped. ********************
*************************************************************************







In the above output able to see that the welcome message printed every 2 sec.

Additional details : 
  • You able to find the Quartz API here.
  • Using the "CronMaker" you able to create an cron expression based on your requirements.