Wednesday, July 25, 2012

BeanFace WebService - Dynamic Class Invocation Example

1.BeanFace Web Service Server Side
In this example, we will use BeanFace Object for the Web Service called BeanFaceWebServiceTransform.java
1.1 Open com.wwlee.beanface.webservice.server.BeanFaceWebServiceServer.java file.
Modify the codes to point BeanFaceWebServiceDynamicTransformationjava.

1.2 Review BeanFaceWebServiceDynamicTransformation.java code.
- This java class contains a couple of important features such as properties file, dynamic class invocation based on soapAction information.

- When initialize() method will be called, it will only once load properties file information to memory.

private void loadPropertyFile(boolean isInitialized) throws BeanFaceException {
        beanFaceWebServiceServerRouter = new BeanFaceWebServiceServerRouter(propertyFileName);
        if (isInitialized) {
            System.out.println("++++ Property File Was Intialized. " + beanFaceWebServiceServerRouter.getPropertyFileNameFullPath());
        } else {
            System.out.println("++++ Property File Was Re-Intialized. " + beanFaceWebServiceServerRouter.getPropertyFileNameFullPath());
        }
    }

- Once processRequest() method is called, it will invoke handleRequest() method which dynamically invoke the actual transformation class.

private void handleRequest(HttpServletRequest request, HttpServletResponse response) throws BeanFaceException, IOException {
        beanFaceWebServiceInfo = new BeanFaceWebServiceInfo(request, response);
        if (reloadProperties(request)) {
            int length = defaultSOAPEnvelope.createResponseForReload(response.getOutputStream());
            sendResponseCommon(response, HttpServletResponse.SC_OK, length);
            return;
        }
        System.out.println("+++++ SOAP Action : " + beanFaceWebServiceInfo.getSOAPAction() + " +++++");
        String routing = beanFaceWebServiceInfo.getSOAPRoutingInfo();
        System.out.println("+++++ SOAP Routing Info : " + routing + " +++++");
        /* Depending on getSOAPRoutingInfo(), you can dynamically call the implementation Class */
        WebServiceTransformation transformation = beanFaceWebServiceServerRouter.getInstance(routing);
        System.out.println("+++++ Dynamically Found Transformation Object : " + beanFaceWebServiceServerRouter.getDynamicInstanceName() + " +++++");
        transformation.setBeanFaceWebServiceInfo(beanFaceWebServiceInfo);
        transformation.receive();
        transformation.transform();
        int length = transformation.reply();
        sendResponseCommon(response, HttpServletResponse.SC_OK, length);
    }

When client will send SOAP with soapAction value '"http://bfws.wwlee.com/DynamicTransformation", this routing value will be 'DynamicTransformation'. When  beanFaceWebServiceServerRouter.getInstance(routing) is called, it will lookup the properties value and return 'com.wwlee.beanface.webservice.transform.example.DynamicTransformation' object instance which is actual WebServiceTransformation implementation class.


1.3. com.wwlee.beanface.webservice.server.impl.example.DynamicTransformation Class
- It extends abstract WebServiceTransformation class, which needs to implement receive(), reply(), and transform() method. 
- This class generates the exactly same results as we have seen in the previous example with  BeanFaceWebServiceTransform class. 

Build and Deploy the Server Program.


2.BeanFace Web Client Server Side called com.wwlee.beanface.webservice.examples.BeanFaceWebServiceClientDynamicTransformation
- This example are the exactly same as BeanFaceWebServiceClientTransformation which we used in the previous example. Only the different one is soapAction value, which is "http://bfws.wwlee.com/DynamicTransformation". 'DynamicTransformation' in soapAction will be used for dynamic class invocation in the server side.

Let's execute this program with countLooping = 10. If you have an issue, do troubleshoot. I think most of cases are related with properties information.

In the second try, let's use countLooping=1500000 (1.5 Million records) and execute it.
My glassfish heap size configuration is '-Xms512m -Xmx512m'. If you have 'OutOfMemoryError' in client side, you need to increase jvm heap size.
Here is my result.

SOAPAction,http://bfws.wwlee.com/DynamicTransformation
Invoking Time : 39172
Response : <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Envelope xmlns:ns2="http://schemas.xmlsoap.org/soap/envelope/">
    <ns2:Header>
        <ns3:bfwsHeader xmlns:ns3="http://beanface.wwlee.com/bfws/bfwsHeader">Response Header</ns3:bfwsHeader>
    </ns2:Header>
    <ns2:Body>
        <ns3:bfwsResponse xmlns:ns3="http://beanface.wwlee.com/bfws/bfwsResponse">ReceivedTotalCount[1500000],BodySize[60388896 bytes],Process Time[38624], LastEmployeeName[Emp1500000]</ns3:bfwsResponse>
    </ns2:Body>
</ns2:Envelope>

The Server side will calculate how many records are there, what the soap body size is, and how long it take to process. See the fantastic performance which just took '42 seconds' with soap body size 60M bytes. If you convert this message to xml format, it will be about 10 times bigger.

Combined with other technlogies such as Message Driven Bean, EJB, SOA, this BeanFace API will give huge benefits for your application integration. I am in the middle of JCAPS migration processes to other SOA tool. If you use Oracle SOA, the BeanFace API will be used in Spring Component. 


My next plan for BeanFace extension will be including SAAJ(SOAP with Attachments API) in the near features. If you register my future blog, you will get the new BeanFace API features.
If you have any question or feedback, you can post this blog or send email to me with wwlee0825@gmail.com.

Wonwoo

To fully use BeanFace and its Web Service, you need to download the followings.
1. BeanFace.jar - BeanFace Core API
2. BeanFaceExample.zip - Include all of the example source codes including Web Service Clients
3. BeanFaceWebService.zip - Include all of the sample java source for Web Service Server sides.  

No comments:

Post a Comment