Mule : Mail Servlet ActiveMQ Example
This page last changed on Aug 19, 2005 by jdirksen.
This page contains a simple example that explains how to integrate the following technologies using Mule:
We're going to implement a very simple MVC pattern. This scenario uses the following components:
First we look at the java part of the components we require. First the ReceiverServlet. They interesting parts here are the init method and the doPost method. In the init method we create a muleclient that is used later on to receive the messages. /** * Servlet implementation class for Servlet: ReceiverServlet */ public class ReceiverServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { private static final long serialVersionUID = 2963863938299247976L; private MuleClient m_client = null; /** * Setups the mule client */ public void init() throws ServletException { try { m_client = new MuleClient(); } catch (UMOException e) { throw new ServletException(e); } super.init(); } /** * Handles the GET HTTP requests. * * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, * HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } /** * Handles the POST HTTP Requests * * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, * HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BufferedWriter writer = null; try { writer = new BufferedWriter(new OutputStreamWriter(response.getOutputStream())); UMOMessage message = m_client.receive("jms://to.servlet2",1000); if (message != null) { writer.write(message.getPayloadAsString()); } else { writer.write("--no message waiting in queue--"); } } catch (Exception e) { throw new ServletException(e); } finally { if (writer != null) writer.close(); } } } Now on to the ServletSender. This again uses the MuleClient to communicate with the ESB. Again in the init method the MuleClient is created. And this is used in the doPost method. /** * Simple servlet, which when invoked sends a string to a certain queue. * This String is then received when the receiver servlet is called. */ public class SenderServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { private static final long serialVersionUID = -7557758560225183285L; private MuleClient m_client = null; public void init() throws ServletException { try { m_client = new MuleClient(); } catch (UMOException e) { throw new ServletException(e); } super.init(); } /** * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } /** * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { m_client.send("jms://from.servlet1","This is a message @" + System.currentTimeMillis(),null); } catch (UMOException e) { throw new ServletException(e); } } } Now how do these servlets determine where to send there data to? This is defined in the following two lines: ReceiverServlet: UMOMessage message = m_client.receive("jms://to.servlet2",1000); SenderServlet: m_client.send("jms://from.servlet1","This is a message @" + System.currentTimeMillis(),null); The RecieverServlet tries to recieve data on the jms://to.servlet2 endpoint, while the SenderServlet sends data to the jms://from.servlet1 endpoint. The 1000 indicates that we only want to wait 1 second, if in that time no message is available on the queue, we just continue. The null on the sender part, indicates that we don't send any properties/headers withe message. The component we didn't discuss yet is the MailerComponent. Like already mentioned, this is a very simple class called SimpleLogger: public class SimpleLogger { public void logMessage(String message) { System.out.println("Message '"+ message + "'passed through here @ " + new Date(System.currentTimeMillis())); } public void logMessage(MimeMessage message) { System.out.println("Message '"+ message + "'passed through here @ " + new Date(System.currentTimeMillis())); } } The first method shows logging information for String messages, the other for {{MimeMessage}}s. Now that we've created all our components, we can wire them together using Mule and Spring. The first configuration file I want to discuss is the configuration file that defines the flow of the data. <?xml version="1.0"?> <!DOCTYPE mule-configuration PUBLIC "http://mule.codehaus.org/mule-configuration.dtd" "mule-configuration.dtd"> <mule-configuration version="1.0" id="flows"> <transformers> <transformer name="strictMailTransformer" className="mule.examples.mailintegration.transformers.StrictObjectToJmsTransformer"/> </transformers> <mule-descriptor name="Logger Servlet to Queue" inboundEndpoint="jms://from.servlet1" outboundEndpoint="jms://to.servlet2" implementation="logger"/> <mule-descriptor name="Logger Mail to Queue" inboundEndpoint="pop3://jdirksen:secret@mail.xs4all.nl" outboundEndpoint="jms://to.servlet2?transformers=strictMailTransformer" implementation="logger"/> </mule-configuration> I'll get back to the transformer part, later in this document, first let's explore the two mule-descriptors shown here: Servlet to Queue:
|
![]() |
Document generated by Confluence on Oct 03, 2006 09:23 |