Often in our business processes/integrations we might have a situation where in we would need to trigger our message flow/process with an Email.
With OSB’s Email transport listening to a mailbox and triggering a message flow is very simple and comes out of the box.
The following article will explain as a tutorial how can this be achieved.
The prerequisites for this tutorial are as under :
Setting Up Apache James
The first thing to get started would be to download the Apache James binary from here. Apache James would act as the email server through which we can send/receive emails.
Copy the James binary at any location on your computer. The binaries would come with a bin folder. Navigate to the bin folder to locate the run.bat file.
Apache James need JDK 1.5 and above so make sure JAVA_HOME is added with the JDK directory in the list of environment variables before running James.
Open a command prompt and run the run.bat file.
We can now see that Apache James server is up and running. Pretty simple.
Next we can verify by doing a telnet to the James Management port i.e. 4555. Login to James using the default Login Id and Password.
Login Id: root
We can add user inboxes by the following command
|adduser <username> <password>|
After you add users the next thing you can do to verify this is use the command listusers that will give you all users in the server.
Having done this we are good with the email server part. By default James runs both the SMTP server to send mail and a POP3 server that can be used to retrieve mails.
Creating the OSB Message Flow
The next thing would be to create a Message flow in OSB that can be triggered with the Email event.
Make sure that Oracle Service Bus domain is created and a server is up and running.
Open http:host:port/sbconsole and login with the username and password configured for the OSB domain.
The first step would be to create a SMTP server configuration. This is required as we would create a dummy Business Service to post a message to the configured inbox.
Once you have logged in to the OSB console go to System Administration –> SMTP Servers –> Add and create a SMTP server as under
Click on Save to save the configuration.
Now go to project explorer and create a project and name it EmailSub. Create an XML Schema resource from the Create Resource Icon based on the following XSD.
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" targetNamespace="http://www.beatechnologies.com/xsd/salesapp" xmlns:sales="http://www.beatechnologies.com/xsd/salesapp"> <xsd:complexType name="ContentType"> <xsd:sequence> <xsd:element name="MimeType" type="xsd:string" default="text/plain" minOccurs="0"/> <xsd:element name="ContentBody" type="sales:OrderIndicatorType"/> <xsd:element name="ContentEncoding" type="xsd:string" nillable="true"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="EmailPayloadType"> <xsd:sequence> <xsd:element name="FromAccountName" type="xsd:string" minOccurs="0"/> <xsd:element name="To" type="xsd:string" minOccurs="0"/> <xsd:element name="ReplyToAddress" type="xsd:string" minOccurs="0"/> <xsd:element name="Subject" type="xsd:string"/> <xsd:element name="Content" type="sales:ContentType"/> <xsd:element name="Cc" type="xsd:string" minOccurs="0"/> <xsd:element name="Bcc" type="xsd:string" minOccurs="0"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="OrderIndicatorType"> <xsd:sequence> <xsd:element name="OrderID" type="xsd:string" minOccurs="0"/> <xsd:element name="OrderAmount" type="xsd:string" minOccurs="0"/> <xsd:element name="CustomerID" type="xsd:string" minOccurs="0"/> <xsd:element name="QuoteID" type="xsd:string"/> <xsd:element name="BusinessUnitID" type="xsd:string"/> </xsd:sequence> </xsd:complexType> <xsd:element name="EmailPayload" type="sales:EmailPayloadType"/> </xsd:schema>
Create a Business Service based on Messaging Service Type called SendEmailBS
Configure rest of the service as under
Save the service and activate the session. Test the business service that we just created. Also configure an email client to see whether you get this email.
Here is a test payload
<sal:EmailPayload xmlns:sal="http://www.beatechnologies.com/xsd/salesapp"> <sal:Content> <sal:ContentBody> <sal:OrderID>2003138758424</sal:OrderID> <sal:OrderAmount>452617</sal:OrderAmount> <sal:CustomerID>2000001120268</sal:CustomerID> <sal:QuoteID>2002795866809</sal:QuoteID> <sal:BusinessUnitID>4747</sal:BusinessUnitID> </sal:ContentBody> </sal:Content> </sal:EmailPayload> <pre>
Off course this is a very basic way of testing. You can also go on and create a front ending proxy service to pass the content payload, subject and other email headers dynamically.
For the second part we need to configure a Proxy Service that can poll this mail box and instantiate its message flow whenever an email arrives.
The first step for this would be to configure a Service Account of Type ‘Static’ that will basically have the user name and password for the mail box.
We would need to attach this Service Account while configuring the Proxy Service required to poll this mail box.
Next create a Messaging type Proxy Service based on the same Email.xsd as under
Lastly go and edit the Proxy Service Message Flow. Add a Pipeline Pair and in the Request Pipeline add a Stage.
Add some simple action to extract the payload from the body variable and log the body to validate whether we receive an email.
Well we are now almost done.
Use the dummy Business Service that we created and push one more message to the user Inbox. This time you will notice that the Proxy Service picks up the message and logs it to the server log.
Open the domain AdminServer.log file to see the message getting logged.
That is pretty much it. Now you can create your own message flow that can be executed with the trigger of an Email.