SVN Server Configuration in Windows

Sunday, January 9, 2011

Setup is actually pretty straightforward. To use SVN in standalone mode (without Apache), just download the installer (I used svn-1.6.15-setup.exe).

Installing Subversion
- Double-click on the installer and run through it. Shouldn't take too long. Once installed, add an environment variable for SVN_EDITOR. You can do this by right-click on my Computer > Properties. Go to advanced. Hit the Environmental Variables button. Just add another in there called SVN_EDITOR with a value of C:\windows\notepad.exe

Creating a Repository
- Now you need to create a repository. Go to the command prompt (Start > run > cmd). type "svnadmin create D:\Programming\MySVNRepos". (Change the path to whatever path you want to use.) The repos is basically a special directory that will hold your files as well as various subversion config options.

Modifying Security and Authentication Settings
- Next up, you need to modify the permissions and users. Navigate to the folder you just created. In my example, I used D:\Programming\MySVNRepos". In this directory, you will find a few folders. One folder will be called conf. Navigate to it and double-click on the svnserv.conf file. Opening it with notepad will be fine.

- The svnserv.conf file in a configuration file for the svnserv executible. For our purposes, just uncomment out the following lines:

anon-access = read
auth-access = write
password-db = passwd

- Save your changes. And double-click on the passwd file in the same directory. The file stores all usernames and passwords in the format:
$username = $password

- Make sure the [Users] line is uncommented and you can just add a new line for your username and password. For example, if you wanted your username to be josh and your password to be testing, just create a line like this:

josh = testing

- Save it and you're done configuring.

Serving Your Repos for the First Time
Now, you can test it out. Go to the command prompt again and type:
svnserve --daemon --root "D:\Programming\MySVNRepos"
Change the path to your specified path. Once this is done, the daemon should be listening and waiting for requests.

Creating your First Project folder
- Next, we can create a project folder in subversion. To do this, open up a new command prompt. We can't use the old one as our daemon will shutdown. Once you're in the new window, type the following:
svn mkdir svn://localhost/TestProject

- Once you type this in, notepad will appear (assuming you set the SVN_EDITOR variable at the very beginning). This is basically used for the log so you can just leave it be for our test example and close it out.

- After you close out notepad, subversion will ask for your username and password. However, keep in mind, it may also ask for your computer's Admin password. If it does, you can simply type in your computer's admin password. Once you hit return, you can then go ahead and type in your subversion username and then your subversion password. Your subversion username and password is exactly what you typed in the passwd file. (i.e. user: josh pass:testing)

- If you've successfully typed in your username and password, it should say Commited Revision 1. This is a good thing.

Finishing Up
- After that's completed, you're basically done. Your subversion repos has been setup. However, we have a few problems.
1. You probably don't want the command window up all the time when running your subversion daemon.
2. Accessing subversion via command prompt is ok, but it could be better.

Running svnserv as a Windows Service
If you're running XP (Win2k, Win2k3, or Vista), you can tell the process to run as a service. Stop your existing svnserv daemon by closing out of the existing command prompt window. Now, go to the command prompt (again). Type:

sc create svnrepos binpath= "\"C:\program files\subversion\bin\svnserve.exe\" --service --root \"D:\Programming\MySVNRepos" displayname= "Subversion Repository" depend= Tcpip start= auto

Ed. Note: The key/value pairs must be in the form key=_value where the underscore is representative of a space. Also, if you mess up here and the service is still installed, you will need to uninstall the service. You can do so by using sc delete svnrepos (or whatever service name you used). You will need to log off and back on for the service to be completely uninstalled. Also, the "Subversion Repository" is simply a label, you may also call this whatever you'd like.

This simply creates a new service in Windows. Of course, you will need to replace "D:\Programming\MySVNRepos" with the path to your repos that you created. Assuming that you install subversion in the default location, you should just have to run that. Now, just start it by typing:

net start svnrepos

You're done. If you want to change some service settings, such as turning on auto start, just go to Start > run. Type services.msc. Find the service with the Display Name set to "Subversion Repository", right-click on it and select Properties. You can change a few different things in there.

Download TortoiseSVN
Go to tortoisesvn.tigris.org and download TortoiseSVN. The tool is essentially a Windows shell extension that allows you to execute SVN commands from Windows Explorer. It's, honestly, the only subversion tool you really need if you're on a Windows computer. Once you install this, you can then view your repos via a tree view type situation.

For example, go to start > run and type:
svn://localhost

If your service/daemon is started, you should be able to expand the nodes to view your projects and files. You can also create new folders and do a plethora of different tasks in it as well. Also, if you go into Windows explorer, and right-click on any folder, you'll see a new TortoiseSVN submenu, with a few different options. You can then easily import an entire project folder into subversion with ease.

Spring Batch Hello World

Thursday, January 6, 2011

Spring batch is used to run the number of tasks parallelly

In this section, we will see Spring Batch in action by looking into an example. This example simply prints message in console

In Spring Batch, a Tasklet represents the unit of work to be done and in our example case, this would be printing a message.

Look at the following example


The following class implements Tasklet interface. this interface contains execute method. we have to override this method with our functionality.

In our example we are simply printing the passed message

HelloWorldTasklet.java

package com.raj.spring.batch.helloWorld;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

/**
 * @author nagarajuv
 *
 */

public class DynamicJobParameters implements JobParametersIncrementer {
public class HelloWorldTasklet implements Tasklet {

 private String printMessage;

 public void setPrintMessage(String printMessage) {
  this.printMessage = printMessage;
 }

 // @Override
 public RepeatStatus execute(StepContribution stepContribution,
   ChunkContext chunkContext) throws Exception {

  System.out.println("In Hello World Tasklet.!");
  System.out.println("Welcome to spring batch --"+printMessage);
  return RepeatStatus.FINISHED;
 }
}

in applicationContext.xml file we have to specify jobLauncher, datasource, jobRepository

for db configuration we have to run specific db scripts, you can get those from hereSpringBatchScripts
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository"/>
    </bean>
    
    <!-- DATASOURCE, TRANSACTION MANAGER AND JDBC TEMPLATE -->
 <bean id="dataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="jdbc:mysql://localhost:3306/mydbtest" />
  <property name="username" value="raju" />
  <property name="password" value="raju" />
 </bean>
    
    <!-- JOB REPOSITORY - WE USE IN-MEMORY REPOSITORY FOR OUR EXAMPLE -->
 <bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
     <property name="dataSource" ref="dataSource"/>
       <property name="transactionManager" ref="transactionManager"/>
 </bean>
 
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"/>
   </bean>
 
 <!-- With out DB connection -->
 <!-- 
    <bean id="jobRepository" class="org.springframework.batch.core.repository.support.SimpleJobRepository">
        <constructor-arg>
            <bean class="org.springframework.batch.core.repository.dao.MapJobInstanceDao"/>
        </constructor-arg>
        <constructor-arg>
            <bean class="org.springframework.batch.core.repository.dao.MapJobExecutionDao" />
        </constructor-arg>
        <constructor-arg>
            <bean class="org.springframework.batch.core.repository.dao.MapStepExecutionDao"/>
        </constructor-arg>
        <constructor-arg>
            <bean class="org.springframework.batch.core.repository.dao.MapExecutionContextDao"/>
        </constructor-arg>
    </bean>
         
    <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
    -->
</beans>

In helloworldJob.xml file we are importing applicationContext.xml file. and in this class we are declring jobs and tasklets. in our example job name is hellowWorldJob with two tasklets welcomeTasklet, printTasklet. we can write no of tasklets

In our web application, In particular class we can set jobParameters to the job in the following way

helloworldJob.xml
<?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
         
  <import resource="applicationContext.xml"/>
    
  <bean id="welcomeTasklet" class="com.raj.spring.batch.helloWorld.HelloWorldTasklet">
   <property name="printMessage" value="Santosh"/>
  </bean>
  
  <bean id="printTasklet" class="com.raj.spring.batch.helloWorld.HelloWorldTasklet">
   <property name="printMessage" value="Nagaraju"/>
  </bean>
  
  <bean id="taskletStep" abstract="true"
   class="org.springframework.batch.core.step.tasklet.TaskletStep">
   <property name="jobRepository" ref="jobRepository"/>
  </bean>
  
  <bean id="helloWorldJob" class="org.springframework.batch.core.job.SimpleJob">
   <property name="restartable" value="true" />
   <property name="name" value="helloWorldJob" />
   <property name="steps">
    <list>
     <bean parent="taskletStep">
      <property name="tasklet" ref="welcomeTasklet"/>
      <property name="transactionManager" ref="transactionManager"/>
     </bean>
     <bean parent="taskletStep">
      <property name="tasklet" ref="printTasklet"/>
      <property name="transactionManager" ref="transactionManager"/>
     </bean>
    </list>
   </property>
   <property name="jobRepository" ref="jobRepository"/>
  </bean>
 </beans>
Through spring batch CommandLineJobRunner we can run this example by passing jobxml file name,jobname.
Main.java
package com.raj.spring.batch.helloWorld;

 import org.springframework.batch.core.launch.support.CommandLineJobRunner;

 /**
  * @author nagarajuv
  *
  */

 public class Main {
  public static void main(String[] args) {
   CommandLineJobRunner.main(new String[]{"helloWorldJob.xml", "helloWorldJob"});
  }
 }

If you want you can download this example at SpringBatchHelloWorld

Tags:Spring batch introduction, Sprig batch helloworld, DB-configuration in spring batch, commandline runner

Spring Batch DB configuration Sql Scripts

For Database configuration in spring batch we need to run the sql script.

The following are the sql scripts for different databases.

The sql scripts are availalble at spring-batch-core jar. we can simply extract the jar for these scripts

other wise you can simply download from the following location

Spring-Batch-All-Scripts

/* To empty the tables */
DELETE FROM BATCH_STEP_EXECUTION_CONTEXT;
DELETE FROM BATCH_JOB_EXECUTION_CONTEXT;
DELETE FROM BATCH_STEP_EXECUTION;
DELETE FROM BATCH_JOB_EXECUTION;
DELETE FROM BATCH_JOB_PARAMS;
DELETE FROM BATCH_JOB_INSTANCE;

/* To view the data in the tables */
select * from BATCH_JOB_INSTANCE;
select * from BATCH_JOB_PARAMS;
select * from BATCH_JOB_EXECUTION;
select * from BATCH_STEP_EXECUTION;
select * from BATCH_JOB_EXECUTION_CONTEXT;
select * from BATCH_STEP_EXECUTION_CONTEXT;

Tags:Spring batch job scripts for different databases. Spring batch Mysql scripts

Rescheduling quartz job programatically

Monday, January 3, 2011

so many people looks for updating cron trigger with new cron expression. updating cron expression is very easy

First get the existing cron and now create new cronTrigger with the existing cron trigger details

The following code will explains how to reschedule job with new cron expression
UpdatingCronExp.java

package com.raj;

/**
 * @author nagarajuv
 *
 */

public class UpdatingCronExp {
     public void updateCronExpression(org.quartz.impl.StdScheduler stdSchedular, Supplier supplier, String triggerName) {
  CronTrigger cronTrigger = (CronTrigger) stdSchedular.getTrigger(triggerName, groupName);
  String newCronExpression = "0 20 9 * * ?"
  try {
   // Creating a new cron trigger
   CronTrigger updatedCronTrigger = new CronTrigger();
   updatedCronTrigger.setJobName(cronTrigger.getJobName());
   updatedCronTrigger.setName(triggerName);
   updatedCronTrigger.setCronExpression(newCronExpression);
   // Reschedule the job with updated cron expression
   stdSchedular.rescheduleJob(triggerName, groupName, updatedCronTrigger);
  } catch (ParseException e) {
   e.printStackTrace();
  }
 }
}


Tags:Quartz job, Rescheduling job programatically, CronTrigger updation

Struts Flow

Sunday, January 2, 2011

So many people looks for struts flow

Please find the following step by step process


Struts Flow
1) When application server gets started container loads the web.xml

2) When first the request is made from a JSP/HTML/XSLT to the server with a particular URI(/something.do) the control first reaches Web.xml file.

3) It checks the mapping for /something.do in web.xml and finds the ActionServlet and loads ActionServlet.
......
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
.....

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

4) As part of loading ActionServlet calls the init() as every other servlet does.
(Note: ActionServlet extends HttpServlet only)
......................
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
........................

5) In init() it takes the init-parameters as struts-config.xml and loads the xml file.

6) Then the ActionServlet calls the process() method of RequestProcessor class.

7) process() method checks for the appropriate action in the action mapping for current request and maps the URI to the action in the struts-config.xml.

8) Then it creates an object to ActionForm associated to that action.

9) It then calls the setters and reset() methods in the form bean(Which extends ActionForm) corresponds to the mapped action.

10) If the action tag in struts-config.xml contains validate "true" then the validate method is called.

11) if any validation errors it return to view component (any jsp XSLT) which is specified as an attribute of input "/error.jsp"

12) if there are no validation errors it then search for execute method in the action class and executes it.

13) execute method performs the bussinesslogic which you provide and returns with an ActionForward key.

14) Now the returned key is searched in the mapped action which is specified as forward tag.

15) It looks for the appropriate view component and performs the getters on that action form corresponding to this view component and loads the view page in the browser.



Tags:Struts step by step flow in detail

A job instance already exists and is complete for parameters

org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException: A job instance already exists and is complete for parameters={}. If you want to run this job again, change the parameters.

Solution for the above exception is:

The meaning of the above exception is one instancce(instance name) is already exists in db
To avoid the above exception we have to follow the following process

The following class is used to set job parameters dynamically

DynamicJobParameters.java

package raj.spring.batch;

import java.util.Calendar;
import java.util.Date;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersIncrementer;

/**
 * @author nagarajuv
 *
 */

public class DynamicJobParameters implements JobParametersIncrementer {
 Date date = null;
 public JobParameters getNext(JobParameters parameters) {
  date = new Date();
    
  parameters = new JobParametersBuilder().addLong("currentTime", new Long(System.currentTimeMillis())).toJobParameters();
  
  return parameters;
 }
}

define the dynamicJobParameters bean .xml file

FileToTableJob.xml
<bean id="fileWritingJob" class="org.springframework.batch.core.job.SimpleJob" incrementer="dynamicJobParameters">

<bean id="dynamicJobParameters" class="com.ecomputercoach.DynamicJobParameters" />

Other wise we can follow other way alos

In our web application, In particular class we can set jobParameters to the job in the following way
Job job = null;
        JobParametersBuilder builder = null;
        JobParameters jobParameters = null;
        JobLauncher launcher = null;

        ApplicationContext context = AppContext.getApplicationContext();

        job = (Job) context.getBean("myjob");
        builder = new JobParametersBuilder();
        builder.addLong("currentTime", new Long(System.currentTimeMillis()));

        jobParameters = builder.toJobParameters();
        launcher = (JobLauncher) context.getBean("jobLauncher");
  launcher.run(job, jobParameters);


Tags:Spring batch job, dyamic parameters

Spring Application Context Provider

Accessing the appolication context in java class is very easy. if you want to access the application context beans in any java class we have to set the application context at the time of server loading for that follow the
following process

The following class is used to set values in the application context dynamically.

AppContext.java

package com.raj;

import org.springframework.context.ApplicationContext;

/**
 * @author nagarajuv
 *
 */

public class AppContext {

    private static ApplicationContext ctx;

    /**
     * Injected from the class "ApplicationContextProvider" which is automatically loaded during Spring-Initialization.
     */
    public static void setApplicationContext(ApplicationContext applicationContext) {
        ctx = applicationContext;
    }

    /**
     * Get access to the Spring ApplicationContext from everywhere in your Application.
     * 
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return ctx;
    }

}

The following class is used to set the application context at the time of loading the server.

ApplicationContextProvider.java
package com.raj;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.raj.AppContext;

/**
 * @author nagarajuv
 *
 */
public class ApplicationContextProvider implements ApplicationContextAware {

    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        // Wiring the ApplicationContext into a static method
        AppContext.setApplicationContext(ctx);
    }
}

Place the following line in applicationContext.xml file

applicationContext.xml
<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
  "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<!-- Application Context Provider -->
 <bean id="contextApplicationContextProvider" class="com.raj.ApplicationContextProvider"></bean>


Tags:Spring Application Context provider, in any java class, Application context, dynamically