KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > net > axis > transport > mailto > client > AsyncMailSender


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  *
7  * Created on Mar 12, 2004
8  */

9 package org.jboss.net.axis.transport.mailto.client;
10
11 import org.apache.axis.AxisFault;
12 import org.apache.axis.MessageContext;
13 import org.apache.axis.message.addressing.AddressingHeaders;
14 import org.apache.axis.message.addressing.Constants;
15 import org.apache.axis.message.addressing.MessageID;
16
17 /**
18  * <dl>
19  * <dt><b>Title: </b><dd>Client Side Email Transport</dd>
20  * <p>
21  * <dt><b>Description: </b><dd>This transport uses the asynchronous capabilities of axis to send a request. This
22  * transport works in cooperation with the AsyncMailClientService. The call is created in the normal way, but rather than
23  * issuing a call.invoke() the call is passed to the AsyncMailClinetService along with a callback, and its arguments.
24  * the service starts a new thread that invokes the call. The call progresses normally until it gets to the transport
25  * (here) where the request is sent, but then the thread is put to sleep. The AsyncMailClientService keeps a reference
26  * to the MessageContext of every message it has sent. Every so often, the service checks the inbox for messages that
27  * correlate to ones it has sent. When it finds one of these response messages, it adds it to the corresponding
28  * message context, and notifies the thread. If the thread wakes up on it's own, it assumes that no response has been
29  * received and faults.</dd>
30  * <p>
31  * </dl>
32  * @author <a HREF="mailto:jasone@greenrivercomputing.com">Jason Essington</a>
33  * @version $Revision: 1.1 $
34  */

35 public class AsyncMailSender extends BaseMailSender
36 {
37
38    public static final String JavaDoc TIMEOUT = "Timeout";
39    public static final long DEFAULT_TIMEOUT = 1800000; // 30 minutes
40

41    protected long getTimeout()
42    {
43       long timeout = DEFAULT_TIMEOUT;
44       String JavaDoc to = (String JavaDoc) getOption(TIMEOUT);
45       if (to != null)
46       {
47          try
48          {
49             timeout = Long.parseLong(to) * 60 * 1000;
50          } catch (NumberFormatException JavaDoc e)
51          {
52             // this isn't really a big deal, we have a default timeout
53
log.warn(to + "is not a valid number we will use 30 minutes instead.", e);
54          }
55       }
56       return timeout;
57    }
58
59    private String JavaDoc getMessageID(MessageContext ctx)
60    {
61       String JavaDoc id = "no message-id";
62       
63       if (ctx.containsProperty(Constants.ENV_ADDRESSING_REQUEST_HEADERS))
64       {
65          AddressingHeaders headers = (AddressingHeaders) ctx.getProperty(Constants.ENV_ADDRESSING_REQUEST_HEADERS);
66          MessageID msgid = headers.getMessageID();
67          if (msgid != null)
68          {
69             id = msgid.toString();
70          }
71       }
72       return id;
73    }
74
75    /**
76     * Instead of checking mail, we'll just go to sleep, and when we wake up, hopefully a response message will have
77     * appeared in our message context.
78     * Really the response handling is done in the AsyncMailClientService
79     */

80    protected void checkResponse(MessageContext ctx) throws AxisFault
81    {
82       String JavaDoc id = getMessageID(ctx);
83       
84       if (log.isDebugEnabled())
85          log.debug("Entering: checkResponse [" + id + "]");
86
87       try
88       {
89          // nap time . . .
90
Thread.sleep(getTimeout());
91
92          // Whoa, nobody woke us up! Better pitch a fit.
93
if (log.isDebugEnabled())
94             log.debug("This request " + id + " timed out.");
95          throw new AxisFault("Request Timed Out");
96       }
97       catch (InterruptedException JavaDoc e)
98       {
99          // O.K. I'm awake, *YAWN*
100
if (log.isDebugEnabled())
101             log.debug("The request " + id + " received a response.");
102          
103          //TODO Now we should be in State.Success. Maybe we should check for that?
104
}
105       if (log.isDebugEnabled())
106          log.debug("Leaving: checkResponse [" + id + "]");
107    }
108 }
109
Popular Tags