Writing Groovy Services

While Groovy is normally used for data preparation, this and other integrated scripting languages may be used to implement OFBiz Services. Using a scripting language to implement a Service has the advantage that the Service does not need to be compiled nor the containing Component rebuilt prior to use.

Getting ready

Make sure you have a "local dispatcher" and "local delegator" (if you want to use database Services) defined in the web.xml file, then follow these steps:

  1. Check the web.xml file and make sure there is a local dispatcher and local delegator defined for the Component from which you will be calling the Service. For example:
    <context-param>
    <param-name>localDispatcherName</param-name>
    <param-value>example</param-value>
    <description>
    A unique name used to identify the local dispatcher for the
    Service Engine
    </description>
    </context-param>
    <context-param>
    <param-name>entityDelegatorName</param-name>
    <param-value>default</param-value>
    <description>
    The Name of the Entity Delegator to use,
    defined in entityengine.xml
    </description>
    </context-param>
    
  2. Create the Service definition within an existing Service definition file. Note, any changes to the Service definition such as adding or removing input and/or output parameters require an OFBiz restart to become effective:
    <service name="myGroovyService" engine="groovy"
    location="component://myComponent/script/myGroovyService.groovy"
    invoke="">
    </service>
    
  3. Restart OFBiz to ensure the Service's configuration (definition) is read and reloaded.
  4. To test this Service from a web page, create a controller.xml request-map to call and test this Service from a webapp web page. For example, to call the Service in step 2:
    <!-- ======= Testing a groovy Service ===== -->
    <request-map uri="testGroovyService">
    <security https="false" auth="false"/>
    <event type="service" invoke="myGroovyService"/>
    <response name="success" type="view" value="main"/>
    <response name="error" type="view" value="main"/>
    </request-map>
    
  5. To test this Service from a web page, create a HTML form or link. For example, to call the Service with a request-map set to testGroovyService as shown in step 3, you might write something like the following:
    <a href="<@ofbizUrl>testGroovyService</@ofbizUrl>">
    test groovy Service
    </a>
    

How to do it...

Implement your Service by writing the Groovy script and saving it in the location and file specified in the Service's definition location attribute value. Note, to tell OFBiz to execute the Groovy script from top to bottom, leave the invoke parameter empty.

Unlike Java-based Services, there is no need to compile or restart OFBiz to make a Groovy Service immediately effective. Remember, however, that Service definition changes do require an OFBiz restart.

How it works...

Use of OFBiz Service implementation engines such as the Groovy engine make OFBiz Services implementation agnostic. Callers do not need to know that the Service is written in Groovy, and Groovy programmers do not need to concern themselves with the details of calling programs.

To make OFBiz aware that a Service is available and written in Groovy, simply indicate that the Service is to use the Groovy Service Engine by setting the service element's engine attribute value to groovy. OFBiz takes care of the rest.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.134.198