Monday, February 28, 2011

Java job scheduling

Other Links: Job Scheduling with java.util.Timer
                    Job Scheduling with Quartz Framework and JSP - Servlet
                    Job Scheduling with Quartz Framework and Java Spring Framework


Java job scheduling:

Today, we use mobile phone or several other things for this purpose but have we thought about the fact that how these devices remind us and what logic or program gives these information to the devices that present to us. As I used to bath in the ocean of Java, I am going to explain the above aspect in terms of Java.
Java provides 2 classes for handling the job scheduling task.  The term “Job” is used to explain what to be done and scheduling means defining the appropriate period or time at when this job will be fired and the task defined in this job will be performed. The corresponding java classes are:

1)      java.util.Timer
2)      java.util.TimerTask

Lets see how can we make ourselves safe from extreme mental torture from our girlfriends just by following java classes:

package net.nighttale.scheduling;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class ReportGenerator extends TimerTask {

  public void run() {
    System.out.println("Happy birthday");
    //put things here that we used to tell a lie to make our girlfriend happy. You can write here code for email and any other things.
  }

}


class MainApplication {

  public static void main(String[] args) {
    Timer timer  new Timer();
    Calendar date = Calendar.getInstance();
    date.set(
      Calendar.DAY_OF_WEEK,
      Calendar.SUNDAY
    );
    date.set(Calendar.HOUR, 0);
    date.set(Calendar.MINUTE, 0);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND, 0);
    // Schedule to run every Sunday in midnight
    timer.schedule(
      new ReportGenerator(),
      date.getTime(),
      1000 * 60 * 60 * 24 * 7
    );
  }
}

The above code will run the code written in the run() method on every Sunday at midnight. In the above example, the class ReportGenerator is the worker class as it does the user defined task and it must extends TimerTask which implements java.lang.Runnable. All that you need to do is to override run() method with the report generation code.
Then, we schedule this object's execution using one of the Timer's scheduling methods. In this case, we use the schedule() method that accepts the date of the first execution and the period of subsequent executions in milliseconds (since we repeat this report generation every week).

So gyes isn’t it very simple to make our financial-budget’s enemy( our girlfriend) happy by sending them birth day wish email on time. But gyes  don’t be fully dependent on the above methods, because this method has several limitations like:
1)      It can not resume its process again if it fails at scheduled time for any reason.
2)      It only shows at what intervals the job should be executed but it can not define the exact time of executing the job.

The above problems can be solved if we use certain 3rd party framework for java job scheduling. There are several 3rd party job scheduler are available in market but we will study the Quarts Framework in our next blog.

Friday, February 11, 2011

Java on Cloud - Stax Networks

We have already seen Google App Engine as the cloud environment and now we will try to explore Stax engine and we will go through the process of deploying our website on this environment and its benefits and disadvantages. So, first lets understand its benefits and disadvantages:


Benefit:
1)      we can get maximum five  servers on stax environment in cluster manner.
2)      Very easy to deploy.
3)      No need of the other software like winscp, filezilla, putti for deployment. The whole deployment process can be done by Stax Sdk.
4)      Very easy to download the code already deployed.
5)      Very easy in development. It needs minimum changes in our current project to make them suitable to run on stax environment.
6)      We can use any java editor for development.
7)      Provide the full support of mysql
8)      We can use any mysql gui editor for our development process. The online database can easily be connected with our mysql editor on our local PC by using IP address.
9)      It supports the tomcat web server.
10)  No change is needed in our existing mailing code or any other code.
11) It support Struts 2.0.




Disadvantages:
1)      Stax has not decided the definite pricing structure till now. It is in beta version.
2)      It provides maximum five servers as a cluster where the google app engine provides much more than this.
3)      We can deploy our project from local system to online by making the war file of our project. That means, each time we make some changes in our local files, we have to deploy whole project online. Although all the tasks like creating war file of our project, deploying it online can be done very easily just by writing a single command on Stax Console, yet it takes more time to deploy whole project war file.
4)    I have little doubt that whether it support struts 1.2 or not but it is confirmed that it support struts 2.0. 

Please click on any advertisement if you like this blog.


Deployment Process
Now let us go through the following steps and try to deploy our website or online application:
1)      Open http://www.stax.net/
2)      Create one login account on this portal.
3)      All activities like
creating a database,
creating a web application,
managing your application,
managing database
studying the corresponding price structer
            are done from your login account.
4)      create a database
5)      connect this database from gui tools like query browser, sqlyog etc on your local system to export-import database from local system to online and to perform any task  on database level on online from your local system.
6)      Create a web application online from your login account.
7)      Configure stax sdk on your local system for development  in stax environment.
8)      After development and testing complete, make corresponding changes in connection file to access database from online.
9)      Deploy the project from your local system to online by using stax console.
10)  After successful deployment, you will be provided an url to access you project online. Use this url to test your project online.

Below is the list of the commands that will be required to manage our online application on Stax environment from our local Pc:

Please click on any advertisement if you like this blog.
Some commands and ideas:
1)      create database on our local Pc
a)      stax db:create and provide database name, user name, password as demanded.
2)      configure stax sdk our local Pc on window environment variable.
a)      create STAX_HOME variable as follows:
STAX_HOME = E:\jitendra\stax-sdk-0.3.6\STAX_HOME
b)     make an entry of STAX_HOME in path variable
path = E:\jitendra\stax-sdk-0.3.6\STAX_HOME;
c)      create JAVA_HOME variable as follows:
JAVA_HOME = C:\Program Files\Java\jdk1.5.0_07
d)     make an entry of JAVA_HOME in path variable
path = JAVA_HOME\bin;
3)      run the stax tomcat
a)      go to your project directory our local Pc
b)     type the command like “stax run”
c)      now you can run the corresponding application by url like http://localhost:8080/ on any browser
d)     keep  your local tomcat stop while performing above operations.

4)      stop or restart stax tomcat
a)      if the stax tomcat is running now, press ctrl+c
b)     type command like stax restart
5)      project deployment from local system to online
a)      go to your project directory
b)     type the command like
stax deploy –u username –p password –a applicationId –m message
username = login name by user you login into your accout on stax.net
password = password of your account
application id = this can be found from top right corner on our account on application detail. It will be similar as Id = “abcd/xyz”
message =  nothing but any message. Not so useful and can be discarded.

6)      Important URLs:
a)      http://www.stax.net/  
b)     Study materials of stax environment : http://wiki.stax.net/w/index.php/Main_Page
c)      Forum on stax: http://developer.stax.net/


This is all about Stax Environment. I have already deployed one of my webapplication and its url is
http://looking4leads.jksnu.staxapps.net/

Please click on any advertisement if you like this blog.