Mule : Echo Example
This page last changed on Oct 02, 2006 by lajos@mulesource.com.
The example demonstrates how to expose a Mule component over multiple transports, in this case as an Axis web sevice and via System.in (request) and System.out (response). Running the ApplicationPrerequisitesTo run you will need the following -
Optional
Running
*Shell refers to a command shell in unix or linux and command prompt in windows. The Echo ServiceThe echo Service is a POJO that implements an EchoService interface - public interface EchoService { public String echo(String echo); } The implementation (i.e the POJO to be managed by Mule) looks like - package org.mule.components.simple; public class EchoComponent extends LogComponent implements EchoService { public String echo(String echo) { return echo; } } Configuring the ServiceNow lets look at configuring the service. To do this you need to add a <mule-descriptor> config element to your Mule Xml configuration file and provide name and implementation attributes. <mule-descriptor name="EchoUMO" implementation="org.mule.components.simple.EchoComponent"> </mule-descriptor> The implementation attribute can be a class name or if you have an Object Container configured the implementationattribute can be a key name for an object in the container. This allows you to manage Spring Beans, EJB session beans or objects from any of the supported containers. The name attribute must be a unique name for the service. Invoking the ServiceNext we need to configure endpoints on the service so that it can be invoked. When you ran the example you would have been prompted to enter something in the shell (or command prompt). Lets have a look at the configuration for this service. <mule-descriptor name="EchoUMO" implementation="org.mule.components.simple.EchoComponent"> <inbound-router> <endpoint address="stream://System.in"/> </inbound-router> <outbound-router> <router className="org.mule.routing.outbound.FilteringOutboundRouter"> <endpoint address="stream://System.out"/> </router> </outbound-router> </mule-descriptor> The <inbound-router> element allows one or more inbound endpoint to be configured for a service. As you'll see in the next section it is really easy to expose your service over other transports such as SOAP. The <endpoint> element defines an address which is a URI on which this component will receive events. In this case its using the 'stream' connector on System.in. The <outbound-router> element allows one or more <router> elements to be configured that control how and where the response is sent. In this case all responses are sent via System.out. The only other bit of configuration needed is the stream connector configuration that tells it to prompt you for input. The connector configuration looks like this - <connector name="SystemStreamConnector" className="org.mule.providers.stream.SystemStreamConnector"> <properties> <property name="promptMessage" value="Please enter something: "/> <property name="messageDelayTime" value="1000"/> </properties> </connector> Thats all the configuration needed to get the component Running. You can see the whole configuration file for this example here.
Exposing EchoService as a Web ServiceTo expose the EchoService as a web service you need to tell Mule to expose the service as an Axis Service. This only requires a single line of configuration to the component descriptor - <mule-descriptor name="EchoUMO" implementation="org.mule.components.simple.EchoComponent"> <inbound-router> <endpoint address="stream://System.in"/> <endpoint address="axis:http://localhost:8081/services"/> </inbound-router> <outbound-router> <router className="org.mule.routing.outbound.FilteringOutboundRouter"> <endpoint address="stream://System.out"/> </router> </outbound-router> </mule-descriptor> This new <endpoint> element in the <inbound-router> tells Mule to use Axis to expose the component as a service on URL 'http://localhost:8081/services/EchoUMO'. Thats it, there is no additional configuration required! To invoke the component as a soap request, open a browser and type the following in the address bar - http://localhost:8081/services/EchoUMO?method=echo¶m=Is%20there%20an%20echo%20in%20here? You sould get a response back looking something like this - <soapenv:Envelope> <soapenv:Body> <echoResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <echoReturn xsi:type="xsd:string">Is there an echo in here?</echoReturn> </echoResponse> </soapenv:Body> </soapenv:Envelope> Great! you just made a request via the Axis transport provider. If you wanted to use WebMethods Glue instead of Axis you would just replace 'axis:' with 'glue:' on the endpoint URI. See Axis Web Services and Mule and Glue for more information. Mule Config Graph for the Echo Example
|
![]() |
Document generated by Confluence on Oct 03, 2006 09:23 |