KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > http > HttpEmbeddedTunnelServlet


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.transport.http;
19
20 import org.apache.activemq.broker.BrokerService;
21 import org.apache.activemq.transport.TransportAcceptListener;
22
23 import javax.servlet.ServletException JavaDoc;
24
25 import java.net.URI JavaDoc;
26
27 /**
28  * This servlet embeds an ActiveMQ broker inside a servlet engine which is ideal
29  * for deploying ActiveMQ inside a WAR and using this servlet as a HTTP tunnel.
30  *
31  * @version $Revision$
32  */

33 public class HttpEmbeddedTunnelServlet extends HttpTunnelServlet {
34     private static final long serialVersionUID = -3705734740251302361L;
35     
36     protected BrokerService broker;
37     protected HttpTransportServer transportConnector;
38
39     public synchronized void init() throws ServletException JavaDoc {
40         // lets initialize the ActiveMQ broker
41
try {
42             if (broker == null) {
43                 broker = createBroker();
44
45                 // Add the servlet connector
46
String JavaDoc url = getConnectorURL();
47                 transportConnector = new HttpTransportServer(new URI JavaDoc(url));
48                 broker.addConnector(transportConnector);
49
50                 String JavaDoc brokerURL = getServletContext().getInitParameter("org.apache.activemq.brokerURL");
51                 if (brokerURL != null) {
52                     log("Listening for internal communication on: " + brokerURL);
53                 }
54             }
55             broker.start();
56         }
57         catch (Exception JavaDoc e) {
58             throw new ServletException JavaDoc("Failed to start embedded broker: " + e, e);
59         }
60         // now lets register the listener
61
TransportAcceptListener listener = transportConnector.getAcceptListener();
62         getServletContext().setAttribute("transportChannelListener", listener);
63         super.init();
64     }
65
66     /**
67      * Factory method to create a new broker
68      *
69      * @throws Exception
70      */

71     protected BrokerService createBroker() throws Exception JavaDoc {
72         BrokerService answer = new BrokerService();
73         return answer;
74     }
75
76     protected String JavaDoc getConnectorURL() {
77         return "http://localhost/" + getServletContext().getServletContextName();
78     }
79 }
80
Popular Tags