Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Sunday, September 27, 2015

Apache Camel-JPA

Camel JPA allows to route the Entities as Messages to read and write using Java Persistence Architecture (JPA). Let's quickly look at the configuration required to make Camel to route the messages (in fact entities) using camel-jpa.

Dependency

Add the following dependency along with camel-core to enable Camel JPA
<dependency>
	<groupId>org.apache.camel</groupId>
	<artifactId>camel-jpa</artifactId>
	<version>2.15.3</version>
</dependency> 

Component

Create JPA component using the class org.apache.camel.component.jpa.JpaComponent and inject Entity Manager Factory (and Transaction Manager if required)
<bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent">
	<property name="entityManagerFactory" ref="entityManagerFactory" />
	<property name="transactionManager" ref="transactionManager" />
</bean>

EndPoint

Format of the JPA Endpoint URI to be defined as (Component, Entity Class and Options)
jpa://<fully-classified-entity-class-name?option1=value1&option2=value2.."

Few Notable Parameters 

  • entityType: Default value is Class Name of the entity. The option will override the entity class name.
  • consumeDelete: The table row will be deleted after processing. In order to avoid deleting - set it to false. 
  • consumer.query: Custom query to consume the data from the table. 
  • consumer.namedQuery: Custom named query to consume the data from the table.
  • consumer.nativeQuery: Custom native query to consume the data form the table.
  • consumer.delay: Polling interval in milli-seconds between two polls.

Reading from Endpoint

Selection

By default, JPA will read all the rows in the table. To read specific set of rows in the table - use consumer.query (, consumer.nativeQuery or consumer.namedQuery) to set the selection. 

Preserve Rows

After the row is read from the table, the endpoint deletes the processed row. In order to avoid deleting the row, set the parameter consumeDelete to false. 
If we set this parameter - there is a possibility of reading the same row again because the row still exists on the table. In order to avoid the same row to be picked again - we can update the row by creating a method (in the entity and changing the specific value) by annotating it with @Consumed. 

Sending to Endpoint

Send entity by entity to an endpoint, it writes row by row into the table. We can use aggregator to write a list of entities into the endpoint. In this case, we need to set an extra parameter : entityType as java.util.List to make all the rows persisted to the table. 

Example

Let's read rows from a table and write into the same table by changing few parameters.

Table

mysql> desc camel_test;
+---------+----------+------+-----+---------+-------+
| Field   | Type     | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| seq_no  | int(11)  | NO   |     | NULL    |       |
| status  | char(4)  | NO   |     | NULL    |       |
| column1 | char(20) | YES  |     | NULL    |       |
| column2 | char(40) | YES  |     | NULL    |       |
| column3 | int(11)  | YES  |     | NULL    |       |
| column4 | datetime | YES  |     | NULL    |       |
+---------+----------+------+-----+---------+-------+

Configuration

We will see the example using Hibernate and Spring JPA with MYSQL

persitance.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <!--Persistence Unit for Mysql database-->
    <persistence-unit name="testMysql" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <property name="hibernate.show_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

Datasource and Entity manager

<bean id="mysqltestDataSource"
	class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<property name="driverClassName" value="${jdbc.driverClassName}" />
	<property name="url" value="${jdbc.testurl}" />
	<property name="username" value="${jdbc.username}" />
	<property name="password" value="${jdbc.password}" />
</bean>

<context:property-placeholder location="classpath:jdbc.properties" />

<bean id="entityManagerFactory"
	class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
	<property name="dataSource" ref="mysqltestDataSource" />
	<property name="persistenceUnitName" value="testMysql" />
</bean>

Camel JPA

<bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent">
	<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

Entity Class

package com.test.entity;
//Imports here.. 
@Entity
@Table(name = "camel_test")
@NamedQuery(name = "selectQuery", query = "select m from CamelTest m where m.status = 'P'")
public class CamelTest
{
    @Id
    @Column(name="seq_no")
    private Integer seq;
    
    @Column(name="status")
    private String status;
    
    @Column(name="column1")
    private String column1;
    
    @Column(name="column2")
    private String column2;
    
    @Column(name="column3")
    private Integer column3;
    
    @Column(name="column4")
    private Date column4;

    //Getters and Setters here
    
    @Consumed
    public void afterConsume()
    {
        setStatus("D");
    }
}

Points to be noted

  • The class is a JPA entity with one Named Query which will be used by JPA endpoint to read the data from the Table.
    • It reads the data from table whose status is 'P'
  • Only one extra method added which annotated with @Consumed (This will be executed after the entity is processed.
    •  After Camel JPA processing the record, the entity is changing the status to 'D' to avoid it from further reading

Finally Camel Route

<camel:camelContext id="camelContext">
	<camel:route>
		<camel:from uri="jpa://com.test.entity.CamelTest?consumer.namedQuery=selectQuery&amp;consumer.delay=10000&amp;consumeDelete=false" />
		<camel:to uri="direct:sample" />
	</camel:route>
</camel:camelContext>

Points to be noted

  • jpa is the JPA Component of Camel JPA
  • com.test.entity.CamelTest is the entity class created corresponding to camel_test table (as above)
  • Three options are set
    • consumeDelete to false to make the entity preserved after processing
    • consumer.dealy is set to 10 seconds to poll the table for every 10 seconds
    • consumer.namedQuery is set to read a specific set of rows (see NamedQuery annotation on the Entity). 
The above route just reads the rows from the table using the named query (selectQuery) with a polling delay of 10 secs, maps the entities, executes the @Consumed method (which modifies the status column from P to D so that the next poll will not pick the same row)  in the entity class after processing and finally sends the processed message to direct:sample (one by one)

Read in a batch

To read the rows in a batch, use aggregator

Aggreagor

package com.test.camel.aggregator;
//Imports
public class SampleAggregator implements AggregationStrategy
{
    @SuppressWarnings("unchecked")
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange)
    {
        List<CamelTest> list = null;
        if(oldExchange == null || oldExchange.getIn() == null)
        {
            list = new ArrayList<CamelTest>();
            list.add(newExchange.getIn().getBody(CamelTest.class));
        }
        else
        {
            list = oldExchange.getIn().getBody(List.class);
            list.add(newExchange.getIn().getBody(CamelTest.class));
        }
        newExchange.getIn().setBody(list);
        return newExchange;
    }
}

Modified Route as below with Aggregator

<camel:route>
	<camel:from uri="jpa://com.test.entity.CamelTest?consumer.namedQuery=selectQuery&amp;consumer.delay=10000" />
	<camel:aggregate strategyRef="aggregateStrategy" completionInterval="2000" completionSize="20" >
		<camel:correlationExpression>
			<camel:constant>true</camel:constant>	
		</camel:correlationExpression>
		<camel:to uri="direct:sample" />
	</camel:aggregate>
</camel:route>

Write to Table in Batch

Modify the same route to write a duplicate entry into the table by changing the Sequence Number. See the processor below

Processor

package com.test.camel.processor;
//Imports
public class SampleProcessor implements Processor
{
    public void process(Exchange exchange) throws Exception
    {
        //Processing goes here. Set the output to same as input. (Dummy Processing) 
        List<CamelTest> list = exchange.getIn().getBody(List.class);
        
        for(CamelTest t : list)
        {
            t.setSeq(t.getSeq() + 1000); 
        }
        exchange.getOut().setBody(list);     
    }
}

  • Processor - processor is used here to change the primary key of the entities to write it back (Other we get primary key violation error) - this is just for the example purpose - added 1000 for each sequence number.

Modified Route

<camel:route>
	<camel:from uri="jpa://com.test.entity.CamelTest?consumer.namedQuery=selectQuery&amp;consumer.delay=10000" />
	<camel:aggregate strategyRef="aggregateStrategy" completionInterval="2000" completionSize="20" >
		<camel:correlationExpression>
			<camel:constant>true</camel:constant>	
		</camel:correlationExpression>
		<camel:process ref="processor" />	
		<camel:to uri="jpa://com.test.entity.CamelTest?entityType=java.util.List" />
	</camel:aggregate>
</camel:route>
  • URI on the route (camel:to) has a parameter called entityType as java.util.List to save a list of entities back to table. (See Sending to Endpoint above)
Happy Learning!!!

Sunday, September 13, 2015

Apache Camel - Simple Routing Example

This post is continuation of the previous post Apache Camel overview. In this post, we will see how to define a sample route using Apache Camel (with Spring Integration).

Dependencies

Apache Camel can work easily with Spring. The following dependencies are required to make Apache Camel to work with Spring.
  <dependency>
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-core</artifactId>
       <version>2.15.3</version>
  </dependency>
  <dependency>
       <groupId>org.apache.camel</groupId>
       <artifactId>camel-spring</artifactId>
       <version>2.15.3</version>
  </dependency>
camel-core is the actual dependency and camel-spring is required for the spring integration.

Spring Configuration

To add camel to Spring Configuration, add the following

Namespace

xmlns:camel="http://camel.apache.org/schema/spring"

Schema Location

xsi:schemaLocation="http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd"

Combined Spring XML

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:camel="http://camel.apache.org/schema/spring"
    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.xsd
              http://camel.apache.org/schema/spring 
              http://camel.apache.org/schema/spring/camel-spring.xsd">

</beans>         

Camel Context and Route

Camel context is the camel runtime. In Spring, we need to define the Camel Context to define the Route. Within the context, route must be defined.
Problem Statement: Read a file from a directory, process it and copy the processed file to a destination folder.

Define the Processor

package com.test.camel.processor;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SampleProcessor implements Processor
{
    private static final Logger LOG = LoggerFactory.getLogger(SampleProcessor.class);

    public void process(Exchange exchange) throws Exception
    {
        LOG.info("Input is "+exchange.getIn().getBody());
        //Processing goes here. Set the output to same as input. (Dummy Processing) 
        exchange.setOut(exchange.getIn());
    }

}

Points to be noted:

  • Processor is a class which implements org.apache.camel.Processor. 
  • Processor implements a method which takes only one argument (Exchange) - which consists of three messages (in, out and exception) and few other properties related to the route and it's camel context.

Define the Bean

     <bean id="processor" class="com.test.camel.processor.SampleProcessor" />

Define the Route

 <camel:camelContext id="camelContext">
      <camel:route id="file-route">
            <camel:from uri="file:/tmp/input.test" />
            <camel:process ref="processor" />
            <camel:to uri="file:/tmp/output.test" />
      </camel:route>
  </camel:camelContext>

Points to be noted:

  • Route is defined inside the Spring Config (using DSL). It also be defined using RouteBuilder. (We will concentrate only on DSL).
  • Define the route inside the Camel Context 
  • A very basic route consists of From (Where to fetch from), Processor (Which processor the input) and To (Where to write the processed input). 
  • Processor is optional - if we don't want to do any processing. 
  • Here in this route (file-route), files will be read from directory /tmp/input.test, and will be written into /tmp/output.test directory after processing.
  • Camel Context and Route(s) will be automatically started once the spring application context is loaded and started. So, once spring configuration file is loaded, camel looks for the files in the input directory. 
  • Camel Context and Route(s) will be shutdown once the application context is closed.

Exception Handling

Apache Camel provides try, catch and finally (optional) blocks to be defined within a route to handle the exceptions while routing or processing. The above route can be re-defined with try-catch blocks to check for Exception 
 <camel:camelContext id="camelContext">
      <camel:route id="file-route">
            <camel:from uri="file:/tmp/input.test" />
            <camel:doTry>
                 <camel:process ref="processor" />
                 <camel:to uri="file:/tmp/output.test" />
                 <camel:doCatch>
                      <camel:exception>java.lang.Exception</camel:exception>
                      <camel:log message="Exception while processing "></camel:log>
                      <camel:to uri="file:/tmp/error.test" />
                 </camel:doCatch>
            </camel:doTry>
      </camel:route>
  </camel:camelContext>
In the above case, if an exception is occurred while routing/processing, the exception will be caught, a log will be written and the file will be moved to another directory /tmp/output.test.

Full Spring Configuration

Complete spring configuration file is as below
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:camel="http://camel.apache.org/schema/spring" 
      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.xsd
           http://camel.apache.org/schema/spring 
           http://camel.apache.org/schema/spring/camel-spring.xsd">

     <bean id="processor" class="com.test.camel.processor.SampleProcessor" />

     <camel:camelContext id="camelContext">
         <camel:route id="file-route">
              <camel:from uri="file:/tmp/input.test" />
              <camel:doTry>
                   <camel:process ref="processor" />
                   <camel:to uri="file:/tmp/output.test" />
                   <camel:doCatch>
                        <camel:exception>java.io.IOException</camel:exception>
                        <camel:log message="Exception while processing "></camel:log>
                        <camel:to uri="file:/tmp/error.test" />
                   </camel:doCatch>
              </camel:doTry>
         </camel:route>
     </camel:camelContext>
</beans>
         
In the next post, we will see more details about routing. Happy Learning!!!!

Sunday, August 30, 2015

Apache Camel and KeyWords

To be very simple, Apache Camel is an open source Java Based API which implements most of the commonly used EIPs. So, the next question is what is an EIP?. Let me briefly explain

Enterprise Integration Patterns

EIPs is a set of design patterns (65 design patterns) on how to integrate different enterprise application running on various technologies and makes them to communicate.  These patterns are explained in a book by Gregor Hohpe and Bobby Woolf. So, EIPs is a book in which, the ways to create communication between applications and integrate them are defined. Check the link for more details.

Apache Camel

Let's come back to Apache Camel. As I mentioned at the beginning, Apache Camel is an implementation of EIPs. So, we can integrate the enterprise applications using Camel. It supports almost all types of protocols (FTP, HTTP, JMS, WebService, etc,), to communicate either by itself or by leveraging it. See below an overview of Camel
Using Camel, we can define a set of route(s) within Camel Context to create the communication. The received messages can be filtered, processed, validated, aggregated, split or routed to different other systems.

Camel Context

Camel Context creates runtime for Apache Camel.

EndPoint

As the name indicates, an endpoint is the final or intermediate source/destination of the communication. The endpoint can be a physical address like FTP server or a logical address like JMS Queue, WebService etc. Apache Camel uses URI (of course, Uniform Resource Identifier) to define endpoints. Ex: queue:SAMPLE for a Queue.

Exchange

Exchange is analogous to Message in JMS Specification. But the Exchange carries three messages i.e. Incoming, Outgoing and an Exception for processing. These three are defined as in, out and fault messages. Each of theses Messages has it's own headers.

Template  

CamelTemplate is a class which reads from/writes to an EndPoint. Earlier versions of the camel has the name as CamelClient, but the convention is changed in later versions to be in sync with the other implementations (like Spring JmsTemplate). Template reads/writes Exchanges to/from EndPoint.

Component

Component is a factory class through which you can create an instance of an EndPoint. JmsComponent is the factory for creating JMS Endpoints using a method called JmsComponent.createEndpoint();

Processor

The points discussed so far are very basic to integrate at-least two applications as it is (exchange the messages as it is with no processing). In order to process the message before sending to destination application, we need processor. The processor is an interface which has only one method called void process(Exchange exchange);

Route

A route is a step by step movement of the message between the two endpoints (including exception handling). There are two ways to define a route in Apache Camel. One is using the XML file (like spring bean file). Second is by using Java DSL (Domain Specific Language).

In the next post, we will see how to use these concepts to create a route and process the message between two endpoints (within the Camel Context).

Happy Learning!!!!!

Sunday, August 24, 2014

Spring Data with MongoDB

MongoDB is an open source document database and one of most popular NoSQL databases. See mongdb official page for more details.
Spring data has an integration with the MongoDB.

How to use

Add the following dependencies to use with maven.
  • Spring-data-mongodb
  • mongodb java driver
  • Spring context related modules (As spring requires these)
  <!-- Spring framework -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>3.2.2.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>3.2.2.RELEASE</version>
  </dependency>

  <!-- mongodb java driver -->
  <dependency>
   <groupId>org.mongodb</groupId>
   <artifactId>mongo-java-driver</artifactId>
   <version>2.11.0</version>
  </dependency>
  <dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-mongodb</artifactId>
   <version>1.5.2.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>cglib</groupId>
   <artifactId>cglib</artifactId>
   <version>2.2.2</version>
  </dependency>

Configuration

Configuration can be done in two ways a) Using @Configuration and b) Using Spring XML

a) Using Configuration

Create a class annotated with Configuration and add two methods : mongoDbFactory() and mongoTemplate() like this.
package com.test.mongodb.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import com.mongodb.MongoClient;

@Configuration
public class SpringMongoConfig
{
    public @Bean
    MongoDbFactory mongoDbFactory() throws Exception
    {
        return new SimpleMongoDbFactory(new MongoClient(), "sample");
    }

    public @Bean
    MongoTemplate mongoTemplate() throws Exception
    {

        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());

        return mongoTemplate;

    }
}

How to create context

Call the above configuration using : AnnotationConfigApplicationContext
    AbstractApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class);
    MongoOperations mongoOperation = (MongoOperations)ctx.getBean("mongoTemplate");

b) Using Spring XML File

See the following spring-config.xml file to configure mongo DB template
<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"
 xmlns:mongo="http://www.springframework.org/schema/data/mongo"
 xsi:schemaLocation="http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 <mongo:mongo host="127.0.0.1" port="27017" />
 <mongo:db-factory dbname="sample" />

 <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
  <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
 </bean>

</beans>

How to create context

Create the application context using spring-config.xml as follows:
     AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");
     MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");     
Any one of the options (a) and (b) is enough. XML configuration is always preferable.

MongoDB Document

Map the MongoDB Document using a class annotated using @Document annotation
package com.test.mongodb.entity;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "person")
public class Person
{
    @Id
    private String id;
    private String name;
    private int age;
    private double salary;
    public String getId()
    {
        return id;
    }
    public void setId(String id)
    {
        this.id = id;
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public int getAge()
    {
        return age;
    }
    public void setAge(int age)
    {
        this.age = age;
    }
    public double getSalary()
    {
        return salary;
    }
    public void setSalary(double salary)
    {
        this.salary = salary;
    }
    @Override
    public String toString()
    {
        String str = "Person details : [id:%s, name:%s ,age:%d, salary:%f]";
        return String.format(str, id,name,age,salary);
    }
}

Operations

Operations on the MongoDB Document can be performed using using mongoOperation object.

Application

The application class will be as follows
        Person p = new Person();
        p.setId("10");
        p.setName("Joe");
        p.setAge(30);
        p.setSalary(3000.00);
        mongoOperation.save(p);
        
        //Create Query for fetching the data
        Query sQuery = new Query(Criteria.where("salary").lte(3000.00));
        Person person = mongoOperation.findOne(sQuery, Person.class);
        if(person != null)
            System.out.println("After inserting : Found Person : " + person);
        else
            System.out.println("No person found");
        
        mongoOperation.updateMulti(sQuery, 
                Update.update("salary", "40000"),Person.class);
        
        List<Person> list = mongoOperation.findAll(Person.class);
        System.out.println("After updating Size is "+(list == null ? 0: list.size()));
        if(list != null)
        {
            for(Person pr : list)
            {
                System.out.println(pr);
            }
        }
        
        Query rQuery = new Query(Criteria.where("id").is("10"));
        mongoOperation.remove(rQuery, Person.class);
        
        list = mongoOperation.findAll(Person.class);
        System.out.println("After deleteing one Person. Size is "+(list == null ? 0: list.size()));
        if(list != null)
        {
            for(Person pr : list)
            {
                System.out.println(pr);
            }
        }
        ctx.close();
Output of the above program will be :
After inserting : Found Person : Person details : [id:10, name:Joe ,age:30, salary:3000.000000]
After updating Size is 2
Person details : [id:2, name:Joe ,age:30, salary:40000.000000]
Person details : [id:10, name:Joe ,age:30, salary:40000.000000]
After deleteing one Person. Size is 1
Person details : [id:2, name:Joe ,age:30, salary:40000.000000]
Happy Learning!!!!

Thursday, January 2, 2014

Spring Batch - Chunk Oriented Processing

This is continuation to post. In General, the batch processing needs to process tons tons of data instead of running simple tasks(as we saw in last post). Spring batch allows us to do this with Reader/Writer and Processors. As the name suggests, reader will read the data from the source, processor processes the source data and converts to output. Finally, Writers writes to the destination.
Looks simple, then how can we achieve chunk oriented processing with this. Spring simplified the process to the developers. We need just take care of from where to read(Reader), how to process it(Processor) and where and how to write the processed data(Writer).

Now will see the following with example:
  • Setup a job with steps ( This we already know)
  • Configure readers, writers and processor for each step
  • Conditional based routing to next step
  • Read and process one by one but commit once.
  • Exceptional cases
Spring config 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" xmlns:batch="http://www.springframework.org/schema/batch"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/batch  http://www.springframework.org/schema/batch/spring-batch-2.1.xsd">

 <!-- Transaction Manager -->
 <bean id="transactionManager"
    class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
  
 <!-- jobRepository Declaration -->
 <bean id="jobRepository"
  class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
  <property name="transactionManager" ref="transactionManager" />
 </bean>

 <!-- jobLauncher Declaration -->
 <bean id="jobLauncher"
  class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
  <property name="jobRepository" ref="jobRepository" />
 </bean>

 <!-- Readers -->
 <bean id="sucessTestReader" class="com.test.batch.TestReader">
  <property name="count" value="10" />
 </bean>
 
 <bean id="failedTestReader" class="com.test.batch.TestReader">
  <property name="raiseError" value="true" />
 </bean>

 <!-- Processor Bean Declaration -->
 <bean id="testProcessor" class="com.test.batch.TestProcessor" />

 <!-- Writer Bean Declaration -->
 <bean id="testWriter" class="com.test.batch.TestWriter" />
 
 <!-- Tasklet -->
 <bean id="testTasklet" class="com.test.batch.TestTasklet">
  <property name="fail" value="false" />
 </bean>
 <bean id="failTasklet" class="com.test.batch.TestTasklet" >
  <property name="fail" value="true" />
 </bean>

 <!-- Batch Job Declarations -->
 <batch:job id="sucessJob">
  <batch:step id="firstStep" next="secondStep">
   <batch:tasklet>
    <batch:chunk reader="sucessTestReader" processor="testProcessor"
     writer="testWriter" commit-interval="5" />
   </batch:tasklet>
  </batch:step>
  <batch:step id="secondStep">
   <batch:tasklet>
    <batch:chunk reader="sucessTestReader" processor="testProcessor"
     writer="testWriter" commit-interval="2" />
   </batch:tasklet>
  </batch:step>
 </batch:job>

 <batch:job id="anotherJob">
  <batch:step id="failStep">
   <batch:tasklet>
    <batch:chunk reader="failedTestReader" processor="testProcessor"
     writer="testWriter" commit-interval="2" />
   </batch:tasklet>
   <batch:next on="*" to="sucessStep" />
   <batch:next on="FAILED" to="failedStep" />
  </batch:step>
  <batch:step id="sucessStep">
   <batch:tasklet ref="testTasklet" />
  </batch:step>
  <batch:step id="failedStep">
   <batch:tasklet ref="failTasklet" />
  </batch:step>
 </batch:job>
</beans>

Explanation:
  • There are two jobs
    • sucessJob - Consists of two steps
      • firstStep - A combination of testReader, testWriter and testProcessor with commit-interval of 5. Once the step is completed, the job proceeds to secondStep (because next attribute is defined as secondStep). The commit-interval says that - five items will be read one by one and processed but the 5 items will be written at once
      • secondStep - Same as above but commit interval is 2
    • anotherJob - Consists of three steps
      • failStep - A combination of failedTestReader, testWriter and testProcessor with a commit-interval of 2.
        • Goes to sucessStep when completed properly
        • Goes to failedStep when there is an exception.
      • sucessStep - Job executes this step for all cases except when failed
      • failedStep - Job executes this step if the first step is failed or throws an exception.
  • Step can be defined as Tasklet(Simple task) or a combination of Reader, Writer and Processor (Chunk oriented processing). 
  • We can define next step either 
    • As attribute of step tag  - The next step will be executed irrespective of the success criteria
    • Separate tag inside step - The next step will be executed based on the outcome of current step.
  • Reader, Writer and Processor should implement ItemReader, ItemWriter and ItemProcessor respectively. (As shown in the next code snippet).
    • ItemReader has method read - which returns a generic type I (Should be an item which is to be read)
    • ItemProcessor has method process - takes I  as an input (which is read by Reader) and returns O (Should be an item which should be committed).
    • ItemWriter has method write - takes O as an input which is processed by Processor.
  • ItemWriter has the write method which takes list as an argument - Because commit will be done in batch (batch count will be equal to commit-interval).
  • Reader reads continuously (in a step), unless it returns a null. One reader returns null, the step will be completed after committing the batch. 
  • Writer commits the data once the reader read n (commit-interval) number of inputs and processor processed them or reader completed reading all the inputs (returns null).
  • When an exception occurs, writer ignores the data to write. This case need to handled explicitly - (Using failed step in the above XML). 
The below is the code representing the above jobs and their reader, writer, processor and tasklets with input and output data objects.

Source

Source.java - Which is used an input - prepared by Reader
package com.test.entity;

public class Source {
 private String input;
        //getters and setters
}
Destination.java - Which is prepared by Processor and used by Writer
package com.test.entity;

public class Destination {
 private String output;
        //getters and setters
}
TestReader.java
package com.test.batch;

import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import com.test.entity.Source;

public class TestReader implements ItemReader<Source> {
 
 private int count = 10;
 private boolean raiseError = false;
 private static int internalCount = 0;

 public Source read() throws Exception, UnexpectedInputException,
   ParseException, NonTransientResourceException {
  
  if(raiseError)
  {
   System.out.println("Exception occured while reading");
   throw new Exception("New Exception");
  }
  internalCount++;
  if(internalCount >= count)
  {
   System.out.println("Reached max count "+internalCount);
   return null;
  }
  Source source = new Source();
  source.setInput("Input "+internalCount);
  System.out.println("Reading item "+internalCount);
  return source;
 }
        //Getters and Setters
}
TestProcessor.java
package com.test.batch;

import org.springframework.batch.item.ItemProcessor;
import com.test.entity.Destination;
import com.test.entity.Source;

public class TestProcessor implements ItemProcessor {

 public Destination process(Source source) throws Exception {
  Destination destination = new Destination();
  destination.setOutput(source.getInput().replace("Input", "Output"));
  System.out.println("Converted "+source.getInput()+" to "+destination.getOutput());
  return destination;
 }

}
TestWriter.java
package com.test.batch;

import java.util.List;
import org.springframework.batch.item.ItemWriter;
import com.test.entity.Destination;

public class TestWriter implements ItemWriter {

 public void write(List arg0) throws Exception {
  for(Destination dest : arg0)
  {
   System.out.println("Writing : "+dest.getOutput());
  }
  System.out.println("Finished Writing");
 }

}
TestTasklet.java
package com.test.batch;

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;

public class TestTasklet implements Tasklet {

 private boolean fail = false;

 public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
   throws Exception {
  if (!fail) {
   System.out.println("Finished sucessfully");
   return RepeatStatus.FINISHED;
  } else {
   System.out.println("Exception... So rollback should take place");
   return RepeatStatus.FINISHED;
  }
 }
        //Getters and Setters
}

Output
  • When we run the job sucessJob
INFO: Executing step: [firstStep]
Reading item 1
Reading item 2
Reading item 3
Reading item 4
Reading item 5
Converted Input 1 to Output 1
Converted Input 2 to Output 2
Converted Input 3 to Output 3
Converted Input 4 to Output 4
Converted Input 5 to Output 5
Writing : Output 1
Writing : Output 2
Writing : Output 3
Writing : Output 4
Writing : Output 5
Finished Writing
Reading item 6
Reading item 7
Reading item 8
Reading item 9
Reached max count 10
Converted Input 6 to Output 6
Converted Input 7 to Output 7
Converted Input 8 to Output 8
Converted Input 9 to Output 9
Writing : Output 6
Writing : Output 7
Writing : Output 8
Writing : Output 9
Finished Writing
INFO: Executing step: [secondStep]
Reached max count 11
INFO: Job: [FlowJob: [name=sucessJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]
  • When we run the anotherJob. Output looks like
INFO: Executing step: [failStep]
SEVERE: Encountered an error executing the step
java.lang.Exception: New Exception
 at com.test.batch.TestReader.read(TestReader.java:24)
 at com.test.batch.TestReader.read(TestReader.java:1)
 at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:90)
 ...
Exception occured while reading
Jan 02, 2014 11:34:30 PM org.springframework.batch.core.job.SimpleStepHandler handleStep
INFO: Executing step: [failedStep]
Exception... So rollback should take place
INFO: Job: [FlowJob: [name=anotherJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]

Monday, November 4, 2013

Spring JMX - Expose POJOs as MBeans

Spring JMX allows you to easily expose POJOs as JMX MBeans. Even it allows the MBeans to deploy on application server which has MBean server running or can be run standalone. We can use jconsole to run the services provided by the MBean.
The configuration required to expose the POJO as Mbean is as follows. Here, MyBean is a POJO with at-least one public method.
<bean id="myMBean" class="com.test.MyBean"  />

<bean class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
   <property name="beans">
       <map>
          <entry key="bean:name=MyMBeanName" value-ref="myMBean" />
       </map>
   </property>
</bean>
MBeanExporter exposes the map of "beans" as MBeans. Here we exposed myMBean as "MyMMBeanName". This name will be shown in the jconsole. By default, all the public methods inside the POJO will be exposed as operations.
If the Spring JMX is not running under an application server, then we need a starter. Definition is as follows:
<!-- If not running on a server which has MBean server running, you must start here -->
<bean id="factory" class="org.springframework.jmx.support.MBeanServerFactoryBean" />
With the above configuration, The MBean can be accessed locally. The MBean can be exposed either using JMXJMP or RMI.
To expose using JMXJMP as follows. The default service URL to access is : service:jmx:jmxmp://localhost:9875
<bean class="org.springframework.jmx.support.ConnectorServerFactoryBean" />
To expose using RMI as follows.
<bean class="org.springframework.jmx.support.ConnectorServerFactoryBean"
    depends-on="rmiRegistry">
    <property name="objectName" value="connector:name=rmi" />
    <property name="serviceUrl"
        value="service:jmx:rmi://localhost/jndi/rmi://localhost:10099/myconnector" />
</bean>

<bean id="rmiRegistry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
    <property name="port" value="10099" />
</bean>
The complete configuration of exposing POJO using Spring JMX (With Remote access with RMI) without running an application server is as follows:
<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"
    xmlns:util="http://www.springframework.org/schema/util"
    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
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <bean id="myMBean" class="com.test.MyBean"  />

    <bean class="org.springframework.jmx.export.MBeanExporter"
       lazy-init="false">
       <property name="beans">
           <map>
           <entry key="bean:name=MyMBeanName" value-ref="myMBean" />
           </map>
        </property>
    </bean>

    <!-- If not running on a server which has MBean server running, you must start here -->
    <bean id="factory" class="org.springframework.jmx.support.MBeanServerFactoryBean" />

    <bean class="org.springframework.jmx.support.ConnectorServerFactoryBean"
        depends-on="rmiRegistry">
        <property name="objectName" value="connector:name=rmi" />
            <property name="serviceUrl"
               value="service:jmx:rmi://localhost/jndi/rmi://localhost:10099/myconnector" />
    </bean>

    <bean id="rmiRegistry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
        <property name="port" value="10099" />
    </bean>

</beans>
The POJO MyBean.java is :
package com.test;

public class MyBean {
    public void start()
    {
        System.out.println("Started");
    }
 
    public void stop()
    {
        System.out.println("Stopped");
    }
}
The Application program to start is :
package com.test.application;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Application {
   public static void main(String...args)
   {
       ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
       context.getBean("factory");
   }
}
When we run this program, it start the MBean. Connect to this using jconsole. The MBean with name "MyMBeanName" visible under MBeans Tab.

Happy Learning!!!!

Saturday, October 26, 2013

Getting started with Spring Batch 2.0

Spring batch 2.0 simpilified and improvised the batching framework. We have so many frameworks for MVC, Web, JDBC etc but batching frameworks are very rare. Spring batch is a lightweight and robust batch framework to process these big data sets. Spring offer Tasklet oriented and Chunk Oriented processing. In this post, we will see Simple Tasklet oriented processing.

Key Concepts:
  • Job - Job is a sequence of steps, each has an exit status. Execution of the next step depends on the exit status of previous step.
  • JobRepository - An interface which contains the meta data and corresponding entities of the Job. 
  • JobLauncer - Which launches a job by exposing the method to run.
  • Tasklet - An interface, can be instance of job which exposes a method called execute and returns the execution status. A tasklet will execute repeatedly until it returns FINSIHED.
Step to define (A simple job):
  • Define one job repository
  • Define one job launcher using the job repository.
  • Define Job(s) under the job launcher. Multiple jobs can be defined under one job launcher.
  • Create steps under the job. We can add multiple steps under the job with relations. By default, job launcher executes the job based on the steps defined. We can create dependency between each step. Literally, each step is a java class (Either Tasklet - a simple task or a combination of reader, writer and executiors for chunk oriented processing).
The below example shows how to define a job with single task (or step) using Tasklet.
spring-job.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/batch" xmlns:beans="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-3.0.xsd http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd">

  <beans:bean id="transactionManager"
    class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
  
  <beans:bean id="jobRepository"
    class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
    <beans:property name="transactionManager" ref="transactionManager" />
  </beans:bean>

  <beans:bean id="jobLauncher"
    class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
      <beans:property name="jobRepository" ref="jobRepository" />
  </beans:bean>

  <job id="sampleJob" job-repository="jobRepository">
    <step id="step1">
       <tasklet ref="sampleTasklet" />
    </step>
  </job>

  <beans:bean name="sampleTasklet" class="com.test.springbatch.SampleTasklet" />
</beans:beans>
The SampleTasklet class should be a type of Tasklet, which provides one execute. The execute method returns RepeatStatus to know the status of the task. The tasks executes multiple times if it returns CONTINUABLE otherwise stops after execution.
package com.test.springbatch;

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;

public class SampleTasklet implements Tasklet
{
    public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception
    {
        System.out.println("Do Something; ");
        // Return RepeatStatus.CONTINUABLE if something goes wrong so that it
        // repeats; Otherwise Return FINISHED to complete
        return RepeatStatus.FINISHED;
    }
}

How to run the job:
Spring provides you with CommandLineJobRunner which runs the job using the two parameters. One the spring context file and the other is the batch name.
CommandLineJobRunner.main(new String[] { "spring-batch.xml", "sampleJob" });
In the next post, we will see "Chunk Oriented processing with Spring Batch"
Happy Learning !!!!

Saturday, September 7, 2013

Spring Data - JPA

A lot of effort is required for configuration and using JPA. Spring Data JPA has reduced that to a major extent. Takes little effort to configure and easy to use. Let's look at this with an example.
The spring data dependency and hibernate dependencies using maven are as follows. (Note: All other spring core, bean, context dependencies are required along with the below dependencies)
        <!-- Spring Data JPA -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.0.2.RELEASE</version>
        </dependency>
        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
Create a persistence xml file in META-INF file.
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <!--Persistence Unit for Mysql database-->
    <persistence-unit name="testMysql" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.test.entity.Employee</class>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <property name="hibernate.show_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>
Now, need to configure Spring Data - JPA using the persistent unit "testMysql"
<?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:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--Following data source for Mysql-->
    <bean id="mysqltestDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.testurl}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--Following entity manager for Mysql database-->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="mysqltestDataSource"/>
        <property name="persistenceUnitName" value="testMysql"/>
    </bean>

    <!--Transaction manager for both H2 and Mysql-->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    
    <jpa:repositories transaction-manager-ref="transactionManager" 
        entity-manager-factory-ref="entityManagerFactory" base-package="com.test.dao">
    </jpa:repositories>
    
</beans>
Explanation of above spring configuration Create one datasource.

  • Create one driver manager datasource named "mysqltestDataSource", by loading jdbc.properties file from the classpath using line <context:property-placeholder location="classpath:jdbc.properties"/>
  • Create entity manager factory using the datasource created and persistent unit created in persistent.xml
  • Inject entity manager factory into transaction manager. 
  • Finally configure both transaction manager and entity manager factory to set of repositories in a package (defined using base-package)
Now, create repository, an interface which extends JpaRepository inside package (defined in base-package). The typed arguments required are one entity and other is Id. In this case, entity is Employee and Id of the Employee is Long.
package com.test.dao;

import org.springframework.data.jpa.repository.JpaRepository;

import com.test.entity.Employee;

public interface EmployeeDAO extends JpaRepository<Employee, Long>
{
}
The entity is as follows
package com.test.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "EMP")
@Entity
public class Employee
{
    private int id;
    private String name;
    private int age;
    @Id
    @Column(name = "id", unique = true, nullable = false)
    public int getId()
    {
        return id;
    }
    public void setId(int id)
    {
        this.id = id;
    }
    @Column(name = "name")
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    @Column(name = "age")
    public int getAge()
    {
        return age;
    }
    public void setAge(int age)
    {
        this.age = age;
    }
}
That's it on the configuration part. By using the repository, we can do all types of database operations for that entity. There are numberous in-built methods are provided by JpaRepository interface. Sample code for that will be
        EmployeeDAO dao = (EmployeeDAO)context.getBean("employeeDAO");
        List list = dao.findAll();
        for(Employee e : list)
        {
            //Do Something
        }

Sunday, July 28, 2013

Spring OXM

OXM is Spring's Object/XML Mapping support which helps in conversion of Object to XML(Marshalling) and XML to Object(Unmarshalling). For this, spring has two interfaces Marshaller and Unmarshaller defined in spring-oxm under the package org.springframework.oxm.Marshaller . These can be defined as normal beans like other spring beans.
Marshaller and Unmarshaller has one method each for the actions as below.
public interface Marshaller {
    /**
     * Marshals the object graph with the given root into the provided Result.
     */
    void marshal(Object graph, Result result) throws XmlMappingException, IOException;
}
public interface Unmarshaller {
    /**
     * Unmarshals the given provided Source into an object graph.
     */
    Object unmarshal(Source source) throws XmlMappingException, IOException;
}
Implementation:
There are different marshaller instances in spring-oxm. We will go through CastorMashaller because it's easy to implement and configure.
<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-3.0.xsd">

 <bean id="converter" class="com.test.spring.oxm.converter.Converter">
  <property name="marshaller" ref="castorMarshaller" />
  <property name="unmarshaller" ref="castorMarshaller" />
 </bean>
 
 <bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller">
  <property name="mappingLocation" value="classpath:mapping.xml" />
 </bean>
</beans>
CastorMarshaller is the marshaller and it requires one optional property "mappingLocation" which defines the mapping of class to xml. Sample mapping.xml file is
<?xml version="1.0" encoding="UTF-8"?>
<mapping>
 <class name="com.test.spring.oxm.entity.Employee">
  <map-to xml="employee" />
  <field name="empid" type="integer">
   <bind-xml name="id" node="element" />
  </field>
  <field name="name" type="string">
   <bind-xml name="ename" node="element" />
  </field>
   <field name="dob" type="date" >
   <bind-xml name="dob" node="element"  />
  </field>
   <field name="salary" type="double">
   <bind-xml name="salary" node="element" />
  </field>
 </class>
</mapping>
Points to be noted
  • property "mappingLocation" is optional. We can create CastorMarshaller without mappingLocation but it takes default values while binding
  • Each class tag defines the Object which to be marshalled/unmarshalled with set of field tags. Each corresponds to XML and object mapping.
  • type on the field indicates the type of the node and is mapped to Java Type.
  • map-to and bind-xml tags defines the elements and attributes in the xml. 
  • node attribute in bind-xml defines whether the field should be attribute or element

Code for bean "converter" is as below. Converter internally calls the marshall and unmarshall methods of CastorMarshaller.
package com.test.spring.oxm.converter;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;

public class Converter
{
    private Marshaller marshaller;
    private Unmarshaller unmarshaller;

    public Marshaller getMarshaller()
    {
        return marshaller;
    }

    public void setMarshaller(Marshaller marshaller)
    {
        this.marshaller = marshaller;
    }

    public Unmarshaller getUnmarshaller()
    {
        return unmarshaller;
    }

    public void setUnmarshaller(Unmarshaller unmarshaller)
    {
        this.unmarshaller = unmarshaller;
    }
    
    public String convertToXml(Object obj,String filepath)
    {
        String finalString = new String();
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(filepath);
            getMarshaller().marshal(obj, new StreamResult(fos));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            if(fos != null)
            {
                try
                {
                    fos.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return finalString;
    }
    
    public Object convertToObject(String filepath)
    {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(filepath);
            return getUnmarshaller().unmarshal(new StreamSource(fis));
        } 
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally {
            if(fis != null)
            {
                try
                {
                    fis.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
The object which to marshalled or unmarshalled is
package com.test.spring.oxm.entity;

import java.util.Date;

public class Employee
{
    private int empid;
    private String name;
    private Date dob;
    private Double salary;

    public int getEmpid()
    {
        return empid;
    }
    public void setEmpid(int empid)
    {
        this.empid = empid;
    }
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public Date getDob()
    {
        return dob;
    }
    public void setDob(Date dob)
    {
        this.dob = dob;
    }
    public Double getSalary()
    {
        return salary;
    }
    public void setSalary(Double salary)
    {
        this.salary = salary;
    }
}
Finally, the main program is
package com.test.spring.oxm;

import java.util.Date;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.spring.oxm.converter.Converter;
import com.test.spring.oxm.entity.Employee;

public class Application
{
    public static void main(String[] args)
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
        Converter converter = (Converter)context.getBean("converter");
        Employee emp = new Employee();
        
        emp.setDob(new Date());
        emp.setEmpid(1234);
        emp.setName("Employee Name");
        emp.setSalary(2500.122);
       
        converter.convertToXml(emp,"cust.xml");
        
        Employee emp2 = (Employee)converter.convertToObject("cust.xml");
        System.out.println("Empid : "+emp2.getEmpid());
        System.out.println("Name  : "+emp2.getName());
        System.out.println("Salary: "+emp2.getSalary());
        System.out.println("DOB   : "+emp2.getDob());
    }
}
The xml file generated out of it is :
<?xml version="1.0" encoding="UTF-8"?>
<employee>
 <id>1234</id>
 <ename>Employee Name</ename>
 <dob>2013-07-28T21:17:29.331+05:30</dob>
 <salary>2500.122</salary>
</employee>

Tuesday, March 5, 2013

Dependency Injection and Spring

When it comes to Spring, it always said that Dependency Injection is the major feature of Spring. In the last post i gave an example on how to use spring with a very basic example.

Let's see what's dependency injection is?

In Object oriented world, each and every application is collection of objects working together. In order to work together, there might me some relation (dependency in general sense) between the objects.

For example, i take an application of polymorphism in Java.

package com.test.appl;
public interface Shape 
{ 
     public abstract void draw();
}
package com.test.appl;
public class Rectangle implements Shape 
{
   @Override
   public void draw() 
   {
       System.out.println("Drawing Rectangle");
   }
}
package com.test.appl;

public class Triangle implements Shape 
{
   @Override
   public void draw() 
   {
       System.out.println("Drawing Triangle");
   }
}
It's a very old story. Let me repeat it again. Triangle and Rectangle are types of Shape. The methods of types will be called based on the object assigned to reference of Shape Class.

package com.test.appl;

public class Main 
{ 
   public static void main(String...strings)
   {
       Shape traingle = new Triangle();
       traingle.draw();
  
       Shape rectangle = new Rectangle();
       rectangle.draw();
   }
}

But there is dependency between the objects creation and assigning it to Shape references, and we do call at the same time when it was created. In short, they are tightly coupled.

We can slightly remove the dependency between Parent and its types, by creating an helper class, say ShapeHelper as defined below.

package com.test.appl;

public class ShapeHelper 
{ 
    private Shape shape;
 
    public ShapeHelper(Shape shape)
    {
        this.shape = shape;
    }
 
    public void doDraw()
    {
        shape.draw();
    }
}

ShapeHelper is the class which accepts an object of type Shape (can be Triangle or Rectangle in the current scenario . A method doDraw() is added which internally calls the draw method of Shape type. So, if we assign shape to an object of Rectangle , it calls the draw method of Rectangle(basic of runtime polymorphism). 

package com.test.appl;

public class MainWithHelper {
 
    public static void main(String...strings)
    {
        ShapeHelper sHelper = new ShapeHelper(new Triangle());
        sHelper.doDraw();
        Triangle t = new Triangle();
        sHelper = new ShapeHelper(t);
        sHelper.doDraw();
        sHelper = new ShapeHelper(new Rectangle());
        sHelper.doDraw();
    }
}

From the above example, the dependency in assigning of the objects to Shape has been removed with the help of an Helper Class. The shape is said to be injected into the ShapeHelper class.

This way of removing the dependency between the objects by injecting is called Dependency Injection (in other words. but not exactly)

Spring does this for us, with a simple xml configuration. Instantiation of objects and injecting the dependencies will be done by Spring configuration xml file(In short spring context file).

Let's write the same example in Spring with spring-context file as spring context file
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
    <bean class="com.test.appl.Triangle" id="triangle" ></bean>
    <bean class="com.test.appl.Rectangle" id="rectangle"></bean>
  
    <bean class="com.test.appl.ShapeHelper" id="helper1">
        <property name="shape" ref="triangle">
    </property></bean>
  
</beans>
I have created two different beans (one for triangle and one for rectangle). and one bean for Helper class. I injected the triangle bean into ShapeHelper. In the same way, we can inject rectangle bean.

Now, the question is how do we access them. See the below code on how to access. We need to create the ApplicationContext and then get the bean to call it's methods

package com.test.spring.appl;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.appl.ShapeHelper;

public class DIApplication 
{
    public static void main(String...strings)
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
        ShapeHelper helper = (ShapeHelper)context.getBean("helper1");
        helper.doDraw();
    }
}

The the helper will call the draw() of triangle as the injected object is triangle  To call the draw method to Rectangle, we can change the xml file rather than the actual code. That is another advantage of Spring.

Sunday, March 3, 2013

Getting started with Spring

There are only three simple steps to do it. Let's see a simple example on how to start with Spring.

1. Create a bean class, beans are nothing but simple POJOs.

package com.myexamples.spring;
class Student
{
      private String name;
      public String getName()
      {
           return name;
      }
      public void setName(String name)
      {
           this.name = name;
      }
}

2. Create bean configuration file - spring-context.xml
<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="studentBean" class="com.myexamples.spring.Student">
  <property name="name" value="John" />
 </bean>
</beans>
In spring-context.xml, we have defined one bean which is a plain pojo and passing an argument to it named "name"

3. Create the Application, which invokes the Bean
class MainClass
{
      public static void main(String...args)
      {
           ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
    Student std = (Student)context.getBean("studentBean");
    System.out.println(std.getName());
      }
}
MainClass is the actual application, which access the bean using ApplicationContext.

Steps to access the bean from Application Context

  1. Get an Instance of ApplicationContext, there are many types to get it. Here am using XmlApplicationContext.
  2. Get the bean from the context using getBean method. The beans created by Spring are by default singleton. So if an instance of studentBean exists then created otherwise returns the same instance again. While creating the instance, spring initializes with the properties mentioned in the context xml file (spring-context.xml in this case)
  3. Now if you print, getName(), it will print "John" on the console

Saturday, February 23, 2013

Spring Introduction

Introduction
Spring is a light-weight open source framework. Its initially written by Rod Johnson. Spring enables you to develop applications using POJOs. This got a laryered structure, so that you can choose to use any part of it as an isolation.
Spring Includes
  1. An IOC Light weight Container
  2. AOP Functionality
  3. Abstraction Layer for JDBC/Transaction management
  4. Web Integration Layer
  5. MVC Web Application Framework
Spring Modules
The following are the modules in Spring Architecture
spring-modules

Each of the Components can stand on its own or can be implemented jointly one or more jointly. The functionality of each component is as follows:
  1. Core: This is the basic component in the framework. It has the features of IoC and Dependency Injection. It provides a basic Container called BeanFactory. 
  2. AOP: It introdues aspect oriented programming feature in the Spring framework. Spring-AOP addresses the cross-cutting functionality. 
  3. DAO: This package supports both programmatic and declarative approaches to manage transactions. These can be used to add transaction capability not only for classes implementing special interfaces, but for all ordinary POJOs (plain old Java objects).
  4. ORM: The ORM package provides integration layers for popular object-relational mapping APIs such as JPA, JDO, Hibernate, and iBatis. Using the ORM package you can use these frameworks in combination with all the other features Spring offers, such as the simple declarative transaction management feature.
  5. Enterprise Integration: This package provides an integration layer for popular enterprise services including several remoting technologies, EJB, JMS, JMX, e-mail, and scheduling.
  6. Web Framework Integration: Spring’s Web package provides integration layer for popular web frameworks such as WebWork, Struts, JSF etc. It also provides features such as multipart file-upload functionality, initialization of the IoC container using servlet listeners and a web-oriented application context.