Thursday, January 27, 2011

Begning with Simple WebService

WebService Link






Steps for WebService:In Netbeans create a new project as new webapplication

  1. New Web Application set source level to 1.6JDK.
  2. RightClick to project to new->webservice.
  3. Give webservice Name and package.
  4. Open webservice folder right click in the webservice (public class Test {) ->click add operation
  5. define webmethod add and web variables i and j.
  6. Properties -> Run->relative url “/CalculatorWS?Tester” for Tomcat
  7. Deploy webservice.



HelloWebService

           

package org.me.calculator;



import javax.jws.WebMethod;

import javax.jws.WebParam;

import javax.jws.WebService;



/**

 *

 * @author harshals

 */

@WebService()

public class CalculatorWS {

    /**

     * Web service operation

     */

    @WebMethod

    public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) {

        // TODO implement operation

        int k=i+j;

        return k;

    }

   

Steps to create the WebServiceClient.

1.       New Java Client

2.       Right JavaClient -> WebService Client

3.       Project browse to Webservice or provide the wsdl location “ http://localhost:8084/HelloWebservice/CalculatorWS?wsdl

4.       Right click in main ->Webservice Client resources.

5.       Run Project

6.       Voila webservice is ready.



Main.java

package hellojavaclient;



/**

 *

 * @author harshals

 */

public class Main {

   

    /** Creates a new instance of Main */

    public Main() {

    }

   

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        try { // Call Web Service Operation

            org.me.calculator.client.CalculatorWSService service = new org.me.calculator.client.CalculatorWSService();

            org.me.calculator.client.CalculatorWS port = service.getCalculatorWSPort();

            // TODO initialize WS operation arguments here

            int i = 0;

            int j = 0;

            // TODO process result here

            int result = port.add(i, j);

            System.out.println("Result = "+result);

        } catch (Exception ex) {

            // TODO handle custom exceptions here

        }

        // TODO code application logic here

    }

   

}

Pratical Example Logging using logback with example

Steps to implement logging in core Java

  1. Eclipse – include libraries ie logback-classic,core,access,slf4j-api.
  2. Sample program
package testlogger;

import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.PatternLayout;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.FileAppender;
import ch.qos.logback.core.util.StatusPrinter;


public class LoggerMain {

      private final Logger logger = LoggerFactory.getLogger(getClass());

      public static void main(String[] args) {
            //  PropertyConfigurator.configure("log4j.properties");
              new ConfigureLogBack();
            LoggerMain lm = new LoggerMain();
           
         
            lm.putLogger();


      }

      public void putLogger() {
            LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
          StatusPrinter.print(lc);
            logger.info("Hello");
      }
}
 
  1. In the classpath user librabries set the location of logback.xml
<configuration>
  <substitutionProperty name="log.dir" value="D:\\logging\\" />
  <substitutionProperty name="log.file" value="${logback.file.name}" />
 
  <appender name="STDOUT"
    class="ch.qos.logback.core.ConsoleAppender">     
     <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%d{yyyyMMdd-HH:mm:ss.SSSZ} [%thread] %-5level %logger.%method - %msg%n</Pattern>
      </layout>
  </appender>
 
  <appender name="STDOUT_inforeach" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%date{HH:mm:ss.SSS} %level %msg%n</Pattern>
        </layout>
   </appender>
 
  <appender name="FILE_DATE_ROLL"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${log.dir}/${log.file}.log</file>
    <Append>true</Append>
    <Encoding>UTF-8</Encoding>
    <BufferedIO>false</BufferedIO>
    <BufferSize>8192</BufferSize>
    <ImmediateFlush>true</ImmediateFlush>

    <rollingPolicy
      class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <FileNamePattern>
          ${log.dir}/bak/${log.file}-%d{yyyy-MM-dd}.log.zip
      </FileNamePattern>
    </rollingPolicy>

    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%d{yyyyMMdd-HH:mm:ss.SSSZ} [%thread] %-5level %logger.%method - %msg%n</Pattern>
      </layout>
   </appender>
  
   <appender name="FILE_SIZE_ROLL" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${log.dir}/${log.file}.log</file>
    <Append>true</Append>
    <Encoding>UTF-8</Encoding>
    <BufferedIO>false</BufferedIO>
    <BufferSize>8192</BufferSize>
    <ImmediateFlush>true</ImmediateFlush>
   
     <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
      <FileNamePattern>
          ${log.dir}/bak/${log.file}-%i.log.zip
      </FileNamePattern>
       <MinIndex>0</MinIndex>
       <MaxIndex>9</MaxIndex>
     </rollingPolicy>

     <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
      <MaxFileSize>50MB</MaxFileSize>
     </triggeringPolicy>
   
     <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%d{yyyyMMdd-HH:mm:ss.SSSZ} [%thread] %-5level %logger.%method - %msg%n</Pattern>
       </layout>
  </appender>

  <root>
    <level value="ALL" />
    <appender-ref ref="STDOUT" />
    <appender-ref ref="FILE_SIZE_ROLL" />
  </root>
 
  <logger name="com.inforeach.util.log.Slf4jLogger">
    <level value="DEBUG" />
  </logger>
 
  <logger name="org.logicalcobwebs.proxool">
    <level value="DEBUG" />
  </logger>
 
  <logger name="testlogger.LoggerMain">
    <level value="ALL" />
  </logger>
 
</configuration>
  1. In Eclipse Run Configuration ->Arguments ->VM set

   5 Run the application it should out put the resut in the out folder mentioned at the top


Include the below jar files in eclipse project build path
logback-classic-0.9.8.jar   
logback-core-0.9.8.jar   
slf4j-api-1.4.3.jar   
jcl104-over-slf4j-1.4.3.jar  
logback-access-0.9.8.jar   

Install MySql on Windows

Download
mysql-5.0.22-win32.exe and Install as standard.
mysql-administrator-1.1.9-win

http://dev.mysql.com/downloads/mysql/
http://techtracer.com/2008/06/09/3-easy-steps-to-install-mysql-on-windows-xp/
start mysql client from program files
mysql>
 create database employees;
 show databases;
 USE employees;
CREATE TABLE employee_data
(
emp_id int unsigned not null auto_increment primary key,

f_name varchar(20),
l_name varchar(20),
title varchar(30),
age int,
yos int,
salary int,
perks int,
email varchar(60)
);

Also can use mysql Administrator to check the creation of table and database.