Most of the web services that we
interact with are synchronous and request-response in nature. However,
we see that the synchronous request-response type of interaction is
only a part of the messaging scenarios we encounter in real life.
Asynchronous messaging is very important in constructing loosely
coupled systems. Take for an instance a chain of stores, at the end of
the day all the stores all over can send a mail to the central system
telling it what that days business activity was and in the morning when
the store opens there will be a reply to that mail with new
instructions and updates. It is a lot like the way the old business
worked but with a modern touch. Similarly Axis2 mail transport can be
used to implement SETI@HOME through mail.
Introduction
To get things started you will
first need to go through the Mail Transport configuration document. It will give you all
the information you need about how to configure Axis2 to get the mail
transport working.
Broadly speaking there are 3
ways of calling a service through mail.
1. Using the simple
mail server included in Axis2.
2. Using a generic mail server.
3. Using mailets.
Option number 1 and 2 are
fairly simple and easy to implement, whereas option 3 is somewhat
harder [unless of course you are one of those guys who see mails going
around in your dreams.]. However the mailet scenario provides a more robust and useful
solution in a production environment.
If you are not a guru in mail
related issues you should probably get started on the simple mail server
that has been provided with Axis2. Once you get the hang of the Axis2
related issues then you can move onto tackle the mail beast. Please do
keep in mind that the mail server we have implemented is ONLY FOR
DEMONSTRATION/TESTING PURPOSES.
1. Using the simple mail
server included in Axis2
Before we start I want to
reiterate the fact that the mail server included with Axis2 is ONLY FOR
DEMONSTRATION/TESTING PURPOSES.
The SMTP/POP server that we
have included has the ability to function as a standalone SMTP/POP
server and also has the ability to work as a mailet. All this is done
through a small filter that keeps watch for certain pre-configured
email addresses. These pre-configured email addresses can be changed by
doing a simple edit of the filter class
org.apache.axis2.transport.mail.server.Sorter.
Now that we have the
environment set up, let us start pumping out some code to get the mail
functionality off the ground. First we'll have a look at it from the
mail server side.
This code sets up your Axis2
server working through mail, with a single service. If you need to
have a look under the hood have a look at the MailServer and
UtilsMailServer classes.
Moving onto the client side
have a look at the code listing below. It will call the service that
was setup on the previous code listing.
This will call the service that
was setup on the server and will poll the mail server till the response
is received. Well that is all there is to it.
2. Using a generic mail server
First you need two email
accounts that works with POP/SMTP. One will act as a server and the
other will act as the client. For the time being we will use
server@somewhere.org and client@somewhere.org as the server and the
client email addresses. Now that we have the email addresses you will
have to set up the client and the server looking at the Mail Transport introduction
document.
When calling the generic mail
server the client side code will remain the same and there will be some
modification to the server-side code.
We have to create a separate ConfigurationContext and then use that. We are done; wasn't that simple?
3. Calling Axis through a
James mailet
This process will be a bit more
challenging than the other two methods but will provide a really
elegant way to use the mail transport. Before we get started you will
have to go though the James documents Writing
a Custom Matcher and Writing
a Custom Mailet.
Now that we know the James part of it lets dive into to the Axis2 part of the code. Once you have set up the James side of business we just need to use the same functionality that is used in the above code. Have a look at the code listing below for more details
If you don't happen to have a ConfigurationContext lying around to call this method you can use the following bit of code to get one. Once you create one you can store that on the mailet and keep using it.
Well that seems to be all from the wonderful world of mail for now. :)