View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.servlet.jetty;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleException;
11  import org.mule.api.MuleMessage;
12  import org.mule.api.construct.FlowConstruct;
13  import org.mule.api.endpoint.InboundEndpoint;
14  import org.mule.api.lifecycle.CreateException;
15  import org.mule.api.transport.Connector;
16  import org.mule.transport.AbstractMessageReceiver;
17  
18  import javax.resource.spi.work.Work;
19  import javax.resource.spi.work.WorkException;
20  
21  /**
22   * <code>JettyHttpMessageReceiver</code> is a simple http server that can be used to
23   * listen for http requests on a particular port
24   */
25  public class JettyHttpMessageReceiver extends AbstractMessageReceiver
26  {
27      public static final String JETTY_SERVLET_CONNECTOR_NAME = "_jettyConnector";
28  
29      public JettyHttpMessageReceiver(Connector connector, FlowConstruct flowConstruct, InboundEndpoint endpoint)
30          throws CreateException
31      {
32          super(connector, flowConstruct, endpoint);
33      }
34      
35      public void routeMessageAsync(final MuleMessage message, final ContinuationsReplyTo continuationsReplyTo)
36      {
37          try
38          {
39              getWorkManager().scheduleWork(new Work() {
40  
41                  public void run()
42                  {
43                      try
44                      {
45                          MuleMessage threadSafeMessage = new DefaultMuleMessage(message);
46                          routeMessage(threadSafeMessage);
47                      }
48                      catch (MuleException e)
49                      {
50                          continuationsReplyTo.setAndResume(e);
51                      }
52                  }
53  
54                  public void release()
55                  {
56                      // nothing to clean up
57                  }
58              });
59          }
60          catch (WorkException e)
61          {
62              getConnector().getMuleContext().getExceptionListener().handleException(e);
63          }
64      }
65  }