Monday, February 21, 2011

Day 3 — JAX-WS Fetching Data

Day 3 — JAX-WS Fetching Data: "

According to the previous day tutorial, we have all set the server publish it service. And now we can digg up our client side application by using web service power.


Again, i have to remain that we had no change to use the Netbeans IDE on this tutorial. Besides, we have to make sure again that database client have remain like in Day – 1 tutorial. It was postgreSQL with table defined as the student table.




How To







  1. Make new project, Web Project.

  2. Generating wsdl. It is can be done by choosing web service client from right click open dialog and choosing web service category. Once you get there, you’ll be ask the wsdl type you may insert. This stage allow you choose three type of wsdl deployment; From project, Selecting local file or by WSDL URL. What i’d know in common usage is the URL. then lets give the server WSDL URL and get it generated.

    http://localhost:8080/class.data.server/ClassServiceService?wsdl



  3. After you generate the WSDL, it will cause generated package inside the project. With this, i’d love to move the generated package to sources package. It is due to issue that some communication between package ain’t works so well if we let it in generated package. And this is the example of mine.


  4. import metro library to support the rpc, for the moment :D

  5. Create java class in same package in cl.app package and add this code,
    package cl.app;

    import java.lang.reflect.Proxy;
    import java.rmi.RemoteException;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Vector;
    import javax.mail.MessageContext;
    import javax.xml.rpc.*;
    import javax.xml.namespace.QName;
    import javax.xml.ws.BindingProvider;
    import java.lang.reflect.Proxy;
    /**
    * @author Arif
    */
    public class fetchData {
    private static String endpoint = "http://localhost:8080/class.data.service/ClassService";

    public List<Core> getData() {
    ClassServiceService service = new ClassServiceService();
    ClassService port = service.getClassServicePort();
    List<Core> coreClass = null;
    coreClass = port.collectAuto();
    return (List<Core>) coreClass;
    }

    public static void main(String[] args) throws ServiceException, RemoteException {

    ServiceFactory sf = ServiceFactory.newInstance();
    Service service = sf.createService(null);

    Call call = service.createCall();
    call.setTargetEndpointAddress(endpoint);
    call.setOperationName(new QName("http://localhost:8080/class.data.service/ClassService", "collectAuto"));

    long time = System.currentTimeMillis();
    fetchData fetch = new fetchData();
    List<Core> coreClass = fetch.getData();
    for (Core cb : coreClass) {
    String errorMessage = cb.getErrMsg();
    if (errorMessage.equalsIgnoreCase("")) {
    System.out.println(
    cb.getSid() + " : "
    + cb.getFname() + " : "
    + cb.getLname()+ " : "
    + cb.getAddress());
    System.out.println("---------");

    } else {
    System.out.println(errorMessage);
    }
    }
    System.out.println(time);
    System.out.println(System.currentTimeMillis()-time);
    }
    }



And you will get this like result when you running the project,



"

No comments:

Post a Comment

Comments