Note: This post was first published before Oracle merged with BEA when the Oracle Service Bus product was known as AquaLogic Service Bus, hence the occasional reference to BEA and AquaLogic Service Bus.
In my previous post I showed how easy it was to create a business service in Oracle Service Bus which could be used to send email. If you completed the steps and created this service in your own Oracle Service Bus environment you may have noticed a few limitations:
- It only ever sends email to one address
- The email sent doesn't have the subject field populated
As pointed out in a comment on my first post - one way of addressing the first limitation is to create a separate business service for each email address - not exactly a scalable approach if you have 3000 different email addresses to send email to! Thankfully there is a better way to address both limitations - the Transport Headers action which allows you to modify the transport specific headers of the outbound request and/or inbound response from within a proxy service. In our case, we are interested in setting the email transport specific headers in the outbound request.
In order to demonstrate this, lets make our existing email business service more useful by fronting it with a proxy service which sets the email subject, content and destination email address dynamically based on information in an incoming request of the form:
<email>
<to>The destination email address</to>
<subject>The subject of the email</subject>
<content>The content of the email</content>
</email>
Lets start by creating a simple XML schema to represent our incoming request:
- In the service bus console, click Create in the Change Center to start a new session
- Navigate to the project you created your email business service in
- In the field Enter New Folder Name box type XML Schema and click Add Folder
- Click on the newly created XML Schema folder
- From the Create Resource drop down select XML Schema and complete the screen as follows:
Make sure you enter the XML schema which is:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"targetNamespace="http://www.example.org/EmailRequest"xmlns:tns="http://www.example.org/EmailRequest" elementFormDefault="qualified">
<schema xmlns="http://www.w3.org/2001/XMLSchema"targetNamespace="http://www.example.org/EmailRequest"xmlns:tns="http://www.example.org/EmailRequest" elementFormDefault="qualified">
<element name="Email" type="tns:EmailType"></element>
<complexType name="EmailType">
<sequence>
<element name="to" type="string"></element>
<element name="subject" type="string"></element>
<element name="content" type="string"></element>
</sequence>
</complexType>
</schema>
<complexType name="EmailType">
<sequence>
<element name="to" type="string"></element>
<element name="subject" type="string"></element>
<element name="content" type="string"></element>
</sequence>
</complexType>
</schema>
- Click Save
The next step is to create the proxy service which accepts an XML message conforming to the schema you have just created:
- Add a new folder to your project called Proxy Services and navigate to this folder
- From the Create Resource drop down choose to create a new Proxy Service
- Complete the first screen as follows:
- Click Next
- Select XML as the Request Message Type and select Browse
- Select the EmailRequest schema you have just created
- Select the Email element and click Submit
- Click Last as we do not need to modify any other settings for this service
- Check the summary screen looks like the one below:
Note: By clicking the Last button we have accepted the defaults and created an XML over HTTP service (although we could equally well have chosen to create a WSDL based service too).
- Click Save
The next step is to create the message flow for the proxy service:
- Click on the message flow icon in the Actions column and you should see a very simple message flow:
- Click on the EmailService node and select Add Route
- Click on the route node created and choose Edit Name and Description. Give the route node a sensible name and description and click Save
- Click on the route node again and select Edit Route
- Click on Add an Action and select Communication>Routing
- In the Route to action just created, click on Service, select the email business service you created previously, and click Submit
- Back in the Route to action add a request action by clicking on Add an Action under Request actions and select Communication>Transport Headers
- Click on Add Header
- From the drop down select email>Subject
- Set the Set Header to Expression to $body/ema:Email/ema:subject/text() (you can use the Variable Structures section on the left hand side to select the correct structure)
- Click on the document icon to the left of the Subject radio button and select Add Header
- Repeat the Add Header steps to set the To field to $body/ema:Email/ema:to/text()
Next we have to replace the main body of the message with just the content part to ensure our email contains the content rather than the original XML request sent to the proxy service:
- Click on the icon to the left of Set Transport Headers and select Add an Action>Message Processing>Replace
- Ignore the XPath field (by default it selects everything which is what we want in this case)
- In the variable field enter body
- Set the Expression field to be $body/ema:Email/ema:content/text()
- Ensure the Replace node contents radio button is selected
- The completed Route to part of the flow should look like:
- Click Save All
- Click Activate, enter a description and click Submit to enforce your changes.
The final step is to test our new proxy service. Click on the bug icon to launch the Test Console and modify the contents of the to, subject and content XML fields and click Execute. Check your email to make sure it has been sent and is formatted correctly.
In summary, in this post we have taken a simple email business service and fronted it with a proxy service. This has turned our limited email service into a much more flexible service which can easily be reused by other services. We have also showed how to expose a service using a different transport protocol (in this case we have exposed our email business service as an XML/HTTP proxy service) and ensured our service consumers are not tightly coupled to our email business service making it easier for us to make changes.
No comments:
Post a Comment