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.
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:
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>
<service name="myGroovyService" engine="groovy" location="component://myComponent/script/myGroovyService.groovy" invoke=""> </service>
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>
testGroovyService
as shown in step 3, you might write something like the following:<a href="<@ofbizUrl>testGroovyService</@ofbizUrl>"> test groovy Service </a>
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.
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.
18.219.134.198