Sunday, January 8, 2012

Web Service Part 1. JAX-WS 2.1

Web Service Part 1. JAX-WS 2.1

O'Reilly ကထုတ္တဲ႔ Java Web Services Up and Running ကိုဖတ္ၾကည္႕ေနပါတယ္။ ဖတ္ရင္းနဲ႕နည္းနည္းေရးလုိက္ပါတယ္။ လုိက္လုပ္ခ်င္ရင္လုပ္လုိ႕ရပါတယ္။ စုစုေပါင္းအခ်ိန္ ဆယ္႔ငါးမိနစ္ေလာက္ပဲလုိပါတယ္။

Java 6 ကိုသံုးပါမယ္။ သံုးရတာကလည္းအေၾကာင္းရိွပါတယ္။ Java 6 မွာပါတဲ႔ JAX-WS ေၾကာင္႔ျဖစ္ပါတယ္။ JAX-WS ရဲ႕အရွည္ကေတာ႔ Java API for XML-WEB Services ျဖစ္ပါတယ္။ SOAP-based ေရာ REST-style အတြက္ပါ JAX-WS က support လုပ္ပါတယ္။

Web Service က ဘာအတြက္ေကာင္းလဲဆိုတာကို အင္တာဗ်ဴးမွာေမးခဲ႔ရင္ေျဖဖုိ႕ကေတာ႔ အလြယ္ဆံုးနဲ႕အတိုဆံုးအေနနဲ႕ Legacy Systems ေတြမွာ တျခား systems ေတြနဲ႕ communicate လုပ္ရလြယ္ဖုိ႕အတြက္အဓိကသံုးပါတယ္။ ဟိုဘက္က .net ဒီဘက္က java ေရးထားလည္းရတယ္။ တစ္ဖက္ဖက္က web service provide လုပ္လုိက္ရင္ ေခၚသံုးလို႕ရျပီ။ .net နဲ႕ေရးထားတဲ႔ဘက္က program ရဲ႕ result ကို webservice ထုတ္ထားေပးရင္ အဲဒီ program ကို java နဲ႕ေနာက္တစ္ခါ ထပ္ေရးရတဲ႔ေသာကကင္းေ၀းတယ္ေပါ႔ဗ်ာ။ program ဆိုတာနဲ႕ hello world ေလာက္လုိ႕မျမင္လုိက္ပါနဲ႕။ အင္မတန္ခက္ခဲတဲ႔ business logic အခုတစ္ေထာင္ပါ၀င္တဲ႔ ပရိုဂရမ္ၾကီးလို႕ေတြးၾကည္႕ပါ။ ျပန္ေရးေနရင္ ပ်င္းစရာေကာင္းသလို မွားစရာလည္းေကာင္းတယ္။ Web Service သံုးလုိက္ေတာ႔ ဒါကိုေရွာင္ရွားႏိုင္တယ္။ မေကာင္းတာကေတာ႔ ပိုၾကာတာေပါ႔ဗ်ာ။ HTTP ကသြားရင္ အင္တာနက္ေႏွးတဲ႔ကိစၥေတြရိွမယ္။ timeout issue ရိွမယ္။ connection error ရိွမယ္။ ဟိုဘက္ system down ေနရင္ ဒီဘက္က ငုတ္တုတ္ေမ႔မယ္။ အဲဒါေတြပဲေပါ႔။ အေျခခံအားျဖင္႔ ဒီေလာက္ပဲသိရင္ရေလာက္ျပီထင္ပါတယ္။

ေရးၾကမယ္ေဟ႔ေကာင္းေကာင္း။

SOAP-based web service ေတြကို single class နဲ႕ေရးလုိ႕ရတယ္။ ဒါေပမယ္႔ ပိုျပီးေတာ႔ စနစ္တက်ျဖစ္ေအာင္ (Best practices လို႕ေခၚတယ္။ ဒါမ်ိဳးမေရးရင္လည္း ဘာမွေတာ႔မျဖစ္ဘူး။ ေရးရင္ေတာ႔ပိုေကာင္းတယ္။) ႏွစ္ပိုင္းခြဲေရးတယ္။ interface နဲ႕ implementation ေပါ႔။ အဲဒါေတြကိုဘာေၾကာင္႔သံုးသလဲဆိုတာေတြကိုလည္း Design Pattern ဖတ္ၾကည္႕ရင္သိတယ္။ အားတဲ႔အခါမွဖတ္ၾကည္႕လုိက္ေတာ႔။

SEI ဆိုတာကေတာ႔ Service Endpoint Interface.
SIB ဆိုတာကေတာ႔ Service Implementation Bean

SIB က POJO လည္းျဖစ္ႏုိင္သလို၊ Stateless Session EJB လည္းျဖစ္ႏုိင္တယ္။

TimeServer.java interface ျဖစ္ပါတယ္။
SEI လည္းျဖစ္ပါတယ္။


package timeserver;  // time server
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

/**
 *  The annotation @WebService signals that this is the
 *  SEI (Service Endpoint Interface). @WebMethod signals
 *  that each method is a service operation.
 *
 *  The @SOAPBinding annotation impacts the under-the-hood
 *  construction of the service contract, the WSDL
 *  (Web Services Definition Language) document. Style.RPC
 *  simplifies the contract and makes deployment easier.
*/
@WebService
@SOAPBinding(style = Style.RPC) // more on this later
public interface TimeServer {
    @WebMethod String getTimeAsString();
    @WebMethod long getTimeAsElapsed();
}

@ ေတြက annotation ေတြျဖစ္ပါတယ္။ ဒါေတြကိုေတြ႕ရင္ အေနာက္ဘက္မွာ လုပ္စရာရိွတာေတြ လုပ္ဖုိ႕ ၾကိတ္မွာလုိက္တာလုိ႕ယူဆပါ။ အခ်ိန္ပိုေနရင္ေတာ႔ annotation အေၾကာင္းဖတ္ပါ။ အခ်ိန္သိပ္ေျပာျပေလာက္ေအာင္မပိုရင္ေတာ႔ ကြ်န္ေတာ္အားလို႕ေရးရင္ျပန္လာဖတ္ပါ။

TimeServerImpl.java ျဖစ္ပါတယ္။
SIB လည္းျဖစ္ပါတယ္။ Implementation ပီပီ interface တစ္ခုခုကို implements လုပ္ထားတာကိုေတြ႕ပါလိိမ္႔မယ္။ မေတြ႕မွာစိုးလို႕ အ၀ါေရာင္နဲ႕တားထားပါတယ္။ implements TimeServer လုပ္ထားတာကိုေတြ႕ပါလိမ္႔မယ္။

package timeserver;

import java.util.Date;
import javax.jws.WebService;

/**
 *  The @WebService property endpointInterface links the
 *  SIB (this class) to the SEI (ch01.ts.TimeServer).
 *  Note that the method implementations are not annotated
 *  as @WebMethods.
*/
@WebService(endpointInterface = "timeserver.TimeServer")
public class TimeServerImpl implements TimeServer {
    public String getTimeAsString() { return new Date().toString(); }
    public long getTimeAsElapsed() { return new Date().getTime(); }
}


ဒီ implementation က ခက္ခက္ခဲခဲဘာတစ္ခုမွမလုပ္ထားပါဘူး။ အခ်ိန္ကို return ျပန္တာေလးေရးထားတာပါ။ အခုကလည္း web service အေၾကာင္းေျပာခ်င္တာျဖစ္လုိ႕ အပိုေတြထည္႕မေရးေသးတာလည္းျဖစ္ပါတယ္။

ၾကံဳလို႕ေလရွည္ရင္ ဒီ implementation class ဟာ သူ implements လုပ္တဲ႔ interface မွာ declare လုပ္ထားတဲ႔ methods ေတြ ပါရပါတယ္။ မပါရင္ error တက္မွာပါ။ တကယ္မတတ္လည္း survive ျဖစ္ႏိုင္တဲ႔ စင္ကာပူ I.T ေလာကမွာေတာ႔ မ်ားမ်ားသိစရာမလိုပါဘူး။ ရွာလို႕ရတာေတြေရးတတ္ရံုနဲ႕ရပါတယ္။ အခ်ိန္ပိုေနရင္သာ ငါတုိ႕ အျမဲ ဘာေၾကာင္႔ဒီလိုေရးတာလည္းဆိုတာေလးကို ျပန္ေလ႔လာေပးပါ။

တတိယ class တစ္ခုကေတာ႔ TimeServerPublisher.java ျဖစ္ပါတယ္။ ေကာ္မန္႕ေတြကို မဖ်က္ဘဲ ကူးထည္႕လိုက္ရတာက မူရင္းေရးသူကို ရိုေသသမႈပါ။ တကယ္တမ္း class က လုိင္းနည္းနည္းေလးပါပဲ။


package timeserver;

import javax.xml.ws.Endpoint;

/**
 * This application publishes the Web service whose
 * SIB is ch01.ts.TimeServerImpl. For now, the service
 * service is published at network address 127.0.0.1.,
 * which is localhost, and at port number 9876, as this
 * port is likely available on any desktop machine. The
 * publication path is /ts, an arbitrary name.
 *
 * The Endpoint class has an overloaded publish method.
 * In this two-argument version, the first argument is the
 * publication URL as a string and the second argument is
 * an instance of the service SIB, in this case
 * ch01.ts.TimeServerImpl.
 *
 * The application runs indefinitely, awaiting service requests.
 * It needs to be terminated at the command prompt with control-C
 * or the equivalent.
 *
 * Once the applicatation is started, open a browser to the URL
 *
 *     http://127.0.0.1:9876/ts?wsdl
 *
 * to view the service contract, the WSDL document. This is an
 * easy test to determine whether the service has deployed
 * successfully. If the test succeeds, a client then can be
 * executed against the service.
*/
public class TimeServerPublisher {
    public static void main(String[ ] args) {
      // 1st argument is the publication URL
      // 2nd argument is an SIB instance
      Endpoint.publish("http://127.0.0.1:9876/ts", new TimeServerImpl());
    }
}

နည္းတာမွ ေတာ္ရုံနည္းတာမဟုတ္ပါဘူး။ တစ္ေၾကာင္းတည္းပဲရိွပါတယ္။

      Endpoint.publish("http://127.0.0.1:9876/ts", new TimeServerImpl());

အဲဒါဘာလုပ္တာလဲဆိုရင္ေတာ႔ service ကို launch လုပ္လုိက္တာပါပဲ။ 127.0.0.1 ဆိုတာ ကြ်န္ေတာ္တုိ႕ရဲ႕ ခ်စ္လွစြာေသာ Localhost ၾကီးရဲ႕ IP ပါပဲ။ ေနာက္က ၀စၥႏွစ္လံုးေပါက္ကေတာ႔ port ကိုေရးခါနီး ျခားထားတာပါ 9876 ကေတာ႔ 4D နံပါတ္မဟုတ္ပါဘူး။ port နံပါတ္ျဖစ္ပါတယ္။ts ကေတာ႔ service name ျဖစ္ပါတယ္။ က်န္တာေတြေပးၾကည္႕ျပီးစမ္းၾကည္႕လုိ႕ရပါတယ္။ ေနာက္မွာ နရူး (ႏြားရူး၊ new) ပါပါတယ္။ Service Implementation Class ကို ေခၚထားလိုက္ပါတယ္။

Java 6 ရဲ႕ေကာင္းျခင္းေတြထဲကတစ္ခုကေတာ႔ ေနာက္ဆံုး publisher ကို run ၾကည္႕လုိ႕ရပါတယ္။ ဘာ web server မွ မလိုပါဘူး။ Eclipse ကေန run လုိက္ပါ။ Eclipse ကေန run လုိက္ပါလုိ႕ေရးတာက ေရွ႕က class ႏွစ္ခုကို compiled မလုပ္ဘဲ ေနာက္ဆံုးတစ္ခုတည္း run ေနမွာစိုးလုိ႕ပါ။ Eclipse မွာေတာ႔ သူ႕ဘာသူ အလုိက္တသိ compile စရာရိွတာ pile ရွာပါတယ္။ Eclipse ကိုလည္း JDK 1.6 နဲ႕ compile ပါ run ပါ။  အဆင္ေျပပါလိမ္႔မယ္။

Error ေတြ ဘုရားသိၾကားမလို႕ မေပၚလာဘူးဆိုရင္ ဆိုင္ရာ ဆိုင္ရာ ဘုရားေတြကို ေက်းဇူးတင္ပါ။
ျပီးရင္ web browser ကေန
အဲဒါေလးေခၚၾကည္႕ပါ။ xml ၾကီး ဘြားကနဲေပၚလာရင္ ၾကည္ႏူးေနလုိက္ပါ။
ဒါပဲလားဆိုရင္ေတာ႔ တစ္ခုက်န္ေသးတယ္လုိ႕ေျပာရမွာပါ။ Java မွာ client တစ္ခုေလာက္ေရးျပီးစမ္းၾကည္႕ပါ။ ေရးေနၾကလူေတြကေတာ႔ URL ေတြနဲ႕ပတ္သက္ရင္ java.net.URL ကိုသံုးရမွန္းရိပ္မိေလာက္ပါတယ္။ မရိပ္မိရင္လည္း ရိပ္မိသလိုလုပ္ပါ။ ဟန္ကိုယ္ဖုိ႕မဟုတ္လား။

package client;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import timeserver.TimeServer;

import java.net.URL;
class TimeClient {
    public static void main(String args[ ]) throws Exception {
        URL url = new URL("http://localhost:9876/ts?wsdl");

        // Qualified name of the service:
        //   1st arg is the service URI
        //   2nd is the service name published in the WSDL
        QName qname = new QName("http://timeserver/", "TimeServerImplService");

        // Create, in effect, a factory for the service.
        Service service = Service.create(url, qname);

        // Extract the endpoint interface, the service "port".
        TimeServer eif = service.getPort(TimeServer.class);

        System.out.println(eif.getTimeAsString());
        System.out.println(eif.getTimeAsElapsed());
   }
}
 comment ေတြဖတ္ေစခ်င္လုိ႕ မဖ်က္ဘဲကူးလာေပးတာကိုသေဘာေပါက္ပါ။ အဲဒါကို run လုိက္ရင္ေတာ႔ ခင္ဗ်ားေမွ်ာ္လင္႔ေစာင္႔စားတမ္းတ ရင္ခုန္ေနရတဲ႔ web service က အခ်ိန္ေလးကို ျမင္ေတြ႕ရမွာပါ။

္package ေတြကို ဘယ္လိုထားထားလဲဆိုတာကို ေရးေနၾကလူဆိုရင္ေတာ႔ package keyword ကိုရွာၾကည္႕ျပီးသိႏိုင္ပါတယ္။ novice နဲ႕ newbie ေတြအတြက္ေတာ႔ ေအာက္မွာ package structure ေလး screen shot လုပ္ထားပါတယ္။



ေက်းဇူးတင္ပါတယ္။

Regards,
Zero

Ref:
Java Web Services Up and Running
Oreilly
Martin Kalin
ISBN 978-0-596-52112-7
Price SGD 55

2 b continued:
ဒီ example မွက concurrent သံုးလို႕မရေသးပါဘူး။
အဲဒါကို အပိုင္းႏွစ္မွာဆက္ပါမယ္။

အပိုင္းသံုးကေန ေျခာက္ေလာက္အထိက JAX-B
ျဖစ္ပါမယ္။

က်န္တာေတြကိုလည္းျဖည္းျဖည္းဆက္ပါဦးမယ္။

No comments: