Tuesday, September 3, 2019

Spring Batch Tutorial for beginners

This post is intended for java developers who want to write there first Spring Batch Application.

Please get the code from here.

I will explain this application in detail so that you can make modifications accordingly.

What this application does :

This post is about reading a CSV file(for this example, it is part of the application can be externalized in real time), validate the it, if valid record writes to one table, else writes to another and finally creates a email with all the records processed. In this example, email would not work since it runs on local, but if deployed on weblogic, email can be sent.

Explanation of Code:
Once you have the code add this application to your local weblogic, you can see the following files:
1. The main method of App.java, kick starts the batch. "oms-bulk-job.xml" is the file which has the batch configurations.

2. The xml file, has the reference of context.xml and database.xml which has the necessity Spring and DB configurations. Please make sure you provide your own DB configurations in dataSource section of database.xml file

Also, the following section creates the necessary DB tables which is commented out in this example, Please run the batch with this section for first run and later comment out. 


3.  Coming back to the main xml file. Following section does the real job:

<batch:job id="OMSBulkOrgJob">
<batch:step id="step1">
<batch:tasklet>
<batch:chunk reader="cvsFileItemReader" writer="customWriter"
processor="orgItemProcessor" commit-interval="10">
<batch:listeners>
<batch:listener ref="customStepListener" />
< batch:listener ref="customItemReaderListener" />
<batch:listener ref="customItemWriterListener" />
</batch:listeners>
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>

This defines the reader, processor and writer with listeners.

4. The DAO impl classes write the valid data in BATCH_VALID_ORG and BATCH_VALID_ADDRESStable and invalid data in BATCH_INVALID_ORG table.

This example even the converts the input data as ACTIVE into the relevant code by looking into the master table.

5. All the listeners mentioned in this example are not exactly required, but i have kept them so that the users can get better understanding of the how the flow happens through the SOPs written



Please feel free to contact in case of any doubts!!!








Wednesday, December 12, 2018

REST Webservice with Hibernate Framework

In my previous REST Webservice tutorial i explained how to write your first REST Webservice and the other tutorial was about using the Hibernate APIs.

In this example, we shall write the webservice which would perform the database operations using Hibernate APIs.

The example we need a table in database as
 CREATE TABLE TESTPLAYERRESTWS
   ( "ID" NUMBER NOT NULL ENABLE,
"NAME" VARCHAR2(50 BYTE),
"SPORTS" VARCHAR2(50 BYTE)
   )

Please download the sample code from here.

For this example we have created 4 methods:

get all player -- > /player
get player by id --> /player/{playerid}
update player -- > /updatePlayer
create player -- > /createPlayer


I have created a tester file along with the application which can be used to test the REST Webservice and its DB operations.


The example is self explanatory. Please feel free to contact in case of any questions

Monday, December 3, 2018

Hibernate For Beginners

In my last post i did gave an example of how write your first REST Webservice. In this post we will learn to write our first Hibernate Application.

I am developing my application using Hibernate 3. for higher versions, please refer https://www.javatpoint.com/example-to-create-hibernate-application-in-eclipse-ide

Step 1 : We need a table in our database to perform operations. for this example i have created a table named CITY with two fields: Id(Number), name (varchar2)

Step 2: create hibernate.cfg.xml file.

The hibernate.cfg.xml file contains following entry:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hbm2ddl.auto">update</property>
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="connection.url">jdbc:oracle:thin:@<DBHOST>:<DBPORT>/<DBSERVICENAME></property>
        <property name="connection.username"><USERNAME></property>
        <property name="connection.password"><PASSWORD></property>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <mapping resource="city.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


Note :

Step 3 : Create the "city.hbm.xml" . This file contains the mapping of CITY table as :



Note : Here the entry :
<class name = "com.loiane.com.model.City" table = "CITY">

points to pojo class named City.java which contains the fields mentioned in CITY table

Step 4: create City.java as:
Step 5 Create CityDao.java to perform DB operations:

necessary jars can be found in the attached source code here. I have written an additional MainClass to test the application. In next tutorial, i shall be explaining how to write a REST Webservice to perform database operations through Hibernate



Please feel free to post your questions on the same.

Write Your First REST Webservice

I recently learned writing REST based webservice with the help of tutorialspoint.com.
With this blog i intend to write down my learning in an oversimplified form so that it can come handy for anyone who is a naive.

Steps:
1. Create a new Dynamic Web project (in Eclipse goto file -- > New --> Project -- >Web -- > DynamicWeb project)

for this example i intend to deploy it in weblogic which is why selected the mentioned configuration. you may change it as per your requirement.

2. For this example, i intend to create a static list of player and save it in .dat file and i shall be able to access the same through its id or shall be able to access all the players.
Required jar files can be found in attached source code's lib folder
lets jump on coding part:

create 3 java class(detailed code can be found in the attached application, i am putting screenshot to better understand the code):
Player.java : pojo class to contain the player info


PlayerDao.java : to perform the save and retrieve function

PlayerService.java : This class has the methods which will be accessible through REST calls

Here @Path determines the url which would be used to access the webservice
web.xml shall be automatically generated for you, for this example add following entries:

Please note that i have specified the url to start as rest in this case

3. Right click on the project in eclipse and select export --> war File and select a location where the war would be created.

4. deploy this war in weblogic .
The Webservice can be access at :

To get all the players:
http://localhost:<port>/TestRestWSWeblogic/rest/PlayerService/player


to get player by id
http://localhost:<port>/TestRestWSWeblogic/rest/PlayerService/player/2


Here:
TestRestWSWeblogic : is the contect of the application
rest: defined in web.xml in url-pattern tag
PlayerService : @path defined for PlayerService.java class
player: @Path defined for getTrackInJSON method
player/2 :  @Path("/player/{playerid}") defined in getUser method


Sourcecode can be downloaded from here.

Please feel free to post your questions on the same.
In next few blogs, i would be sharing basic hibernate tutorial and its integration with REST Webservice application where it would use more HTTP methods like put/delete