Monday, March 21, 2016

Simple program with Spring + Maven

The below simple program developed with Spring and maven configuration.

Technologies used:

1.Eclipse Indigo
2.Jdk 1.6
3.Maven 3.0.4
4.Spring 3.2.3

Note: If any image is not able to see properly, please click on that image. It will show nice visible mode.

Step 1: Open eclipse, File àNew à Maven à Maven Project. Its look like below.

Maven
Step 2: Once you create maven project , select (tick) create a simple project. you will see the below in next screen. 

Simple Project
Step 3: Click Next. You will see below in next screen. Please provide Group Id and Artifact Id which is related to maven. Its look like below.

Here I provided,
Group Id : com.test
Artifact Id: helloworld

Group Id: It will identify your project uniquely across all your projects. It has to follow package name rules (Naming schema). 

Artifact Id: Project name as artifact id for the purpose of JAR. Always put frinedly name here.

POM xml
Step 4: Once you click finish on above screen , the next screen will appear like below. This is your project structure now.

Maven structure
Step 5: Please add the below in pom.xml. Here you can add all of your dependency, properties etc.,




  4.0.0
  com.test
  helloworld
  0.0.1-SNAPSHOT
  
  
  
   org.springframework
   spring-core
   ${spring.version}
  
  
   org.springframework
   spring-context
   ${spring.version}
  
 
 
 
  3.2.3.RELEASE
 
 


Step 6: Once you configured pom xml, next you should add application context xml for your project bean definition. So , create one xml like below under src/main/resources.

Spring context xml
Once xml file was created, add the below bean definitons.



 
  
 


Step7 : After adding application context xml, add your java class with the name of  HelloService. That java class looks like below.

package com.test.helloworld;

import org.springframework.stereotype.Service;
 
@Service("helloWorld")
public class HelloService {
 
 private String name;
 
 public void setName(String name) {
  this.name = name;
 }
 
 public String sayHello() {
  return "Hello ! Spring + Maven Test by " + name;
 }
}


Note: Here @Service annotation is not mandatory, without this also program will run. But in generally, its good to add before call will go from this class to any DAO class etc.,

Step 8: The above java class implemented at service layer level. So I used there @Service annotation. We can see it deeply later.

Now I need to test it, means should call above java class from HelloTest java class. The implemented class for HelloTest is below.

package com.test.helloworld;

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

public class HelloTest {
  
 
 public static void main(String[] args) {
 
  // loading bean definitions from applicationcontext.xml
  ApplicationContext context = new ClassPathXmlApplicationContext(
    "applicationContext.xml");
 
  HelloService service = (HelloService) context
    .getBean("helloWorld");
  String message = service.sayHello();
  System.out.println(message);
 
  //set your new name
  service.setName("Setting again new name : TestHelloWorld ");
  message = service.sayHello();
  System.out.println(message);
 }
}


Step 9 : Next we should and clean and build through maven. You can directly run this application from main method of HelloTest java. Since it is a maven project, it is advice to follow through clean and install maven option .

Right click on your pom xml or on project , select Run As --> Maven Clean.
Below images will explain clearly.

maven clean

Step 10 :  Once maven clean done, you should build the application.

Right click on your pom xml or on project , select Run As --> Maven Install. It will build your project. The below images will explain that.

install

Step 11: After build your project , you able to see below screen. In case if your project have any Test class(Junit) it will refelect in console tab.

console
Step 12: After finishing maven clean and install, Run your application .

Right click on HelloTest program, Run As --> Java application. Below output we can see.

Console Output
Please raise comment in case if you have any clarification.

Thats All. Happy learning!

Friday, March 18, 2016

WSDL checking through SOAP UI Tool

With the help of SOAP UI Tool, we could check our WSDL file. While providing request data , it will display response data, if your wsdl is fine. Else it may through any exception while submitting request to end point.

The below screenshot will explain , how to check wsdl.

Step 1: Create a New SOAP Project
            File -->New SOAP Project --> It will display pop up like below screenshot. Here, you have to provide project name and you must import your project wsdl file.




Step 2: Once you created project, just expand + symbol based on your project method. (In my sample project it is addValue). After expand that, you able to see request1. Click on that. Below pop up will open.


Step 3: The request asking , you should provide input parameter to request xml. After entering input details, click on left corner Green Arrow Icon(Submit End point). It will show the response XML in next screen like below.


Now your WSDL is working fine, if it is not throwing any exception. 

Thursday, March 17, 2016

Soap Web Service Example - Simple Calculator

Hi All, The below is simple SOAP calculator example. I have created this with below technologies.

1. Eclipse Indigo IDE
2. Apache Tomcat 6
3. Apache Axis

Step 1: Create one dynamic project which name is SimpleCalculator


Step 2: Cretae one package like com.ws.calculator and write one Calculator java class with four methods for addition, substration, multiplication and division.




Step 3: Once Calculator class is created, next you need to create web services through Bottom Java bean approach. I was forget to tick (Checkbox) Publish the web Service and Monitor the web service. Please tick it.




 Step 4:  Click Next button, Now you able to see which style you have to follow for soap web service. You can choose document / literal .
 Step 5: After that click Next, continuosuly it will ask click Next. From your Calculator java code Calculator wsdl will be generated, this concept is called Bottom Up. You can see below, generated wsdl file.


 Step 6: Click Launch button, Eclipse browser will open like below.


 Step 7: Now SimpleCalculatorClient code also generated. Click addition(int,int) method. It will ask you provide two inputs. Once you provide inputs , bottom you able to see correct result like below.

Step 8: We know soap will process all request and responses in the XML format only. Below I provided screenshot for requesting addtion method in the XML format.

Step 9: You can see below response result in the XML format for the above request XML.


Thats ALL.  Happy Learning! Any doubts please raise in comment.


Tuesday, March 15, 2016

JDK and Maven PATH Settings

JDK and Maven Path settings are very easy to configure in environment variables. The below process will explain.

1.Goto your environment variable and click on that button.

2. Under System variables, click on New button. It will give you an option to add the below two fileds.

Variable Name:     JAVA_HOME
Variable Value:     C:\Program Files\Java\jdk1.6.0_31

java path set
Java Path Setting

3. If you need you can configure maven , else leave maven configuration step.

For maven configuration, again click New button, it will ask you to enter variable name and variable value like step 2.

Variable name :    MAVEN_HOME
Variable value :    C:\Softwares\apache-maven-3.0.4-bin\apache-maven-3.0.4

maven path
Maven Path settings

Once you finished both JAVA_HOME and MAVEN_HOME, we should add it in User variable like below. Please carefully place ; and \bin; (copy and paste it).

Variable name : PATH
Variable value : %JAVA_HOME%;%MAVEN_HOME%\bin;


java and maven
Java and Maven path settings
Once you finish click Ok. Thats all.






Friday, March 11, 2016

Which one developer should use for writing file , FileOutputStream or FileWriter ?

Well, Good question. But all developer will get struggle to choose to write file. Developer should choose based on his/her requirements.

1. FileOutputStream -  Write the file in the streams of raw bytes like IMAGE.
2. FileWriter - Writting streams of characters (Text type of data)

software being installed m2e - slf4j over logback logging (optional) 1

Almost most of the developers are facing this issue and its getting irritate. But its simple to solve by below ways.

1.Go to help – Install new software
2.Add the below URL.
   2.1. http://download.eclipse.org/releases/indigo/
3.Add it. Once you after add, it will load all list.
4.After list loads, Under "General Purpose Tools" check "m2e - Maven Integration for Eclipse".
5.Continue with your installation to proceed further.
6.Once done, eclipse will ask you to restart eclipse.
7. Restart it. PROBLEM SOLVED.

Tuesday, March 8, 2016

Create Singleton with Thread-Safe

Many blog could teach you how to write singleton but somebody may interest to face singelton with thread safe. We can see the below example for create the singleton instance with thread safe.

The below code will also explain What is volatile keyword and its responsibilty.

private static volatile Test instance;

public static Test getInstance () {

if (instance == null) {

synchronized (Test.class) {

instance = new Test();

}

}

return instance;

}

}


From the above, We know static will execute at first as per java rules. Here we able to see volatile keyword, which will create that instance in Main memory. The reason behind for adding volatile here is, we need multi threading task should be happened with the help of that instance. A lot of parallel threads will load simultaneously and if they want to access the instance, which should be in Main memory. The below picture will represent clearly.

Volatile
Volatile with Thread Safe Explanation
Hope you understand volatile purpose here. Suppose if volatile is not used. some thread could access instance as NULL.