/* | |
* Copyright 1999-2011 Alibaba Group. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.alibaba.dubbo.remoting.http.jetty; | |
import java.io.IOException; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.mortbay.jetty.Server; | |
import org.mortbay.jetty.handler.AbstractHandler; | |
import org.mortbay.jetty.nio.SelectChannelConnector; | |
import org.mortbay.thread.QueuedThreadPool; | |
import com.alibaba.dubbo.common.Constants; | |
import com.alibaba.dubbo.common.URL; | |
import com.alibaba.dubbo.common.logger.Logger; | |
import com.alibaba.dubbo.common.logger.LoggerFactory; | |
import com.alibaba.dubbo.common.utils.NetUtils; | |
import com.alibaba.dubbo.remoting.http.HttpHandler; | |
import com.alibaba.dubbo.remoting.http.support.AbstractHttpServer; | |
public class JettyHttpServer extends AbstractHttpServer { | |
private static final Logger logger = LoggerFactory.getLogger(JettyHttpServer.class); | |
private Server server; | |
public JettyHttpServer(URL url, final HttpHandler handler){ | |
super(url, handler); | |
int threads = url.getParameter(Constants.THREADS_KEY, Constants.DEFAULT_THREADS); | |
QueuedThreadPool threadPool = new QueuedThreadPool(); | |
threadPool.setDaemon(true); | |
threadPool.setMaxThreads(threads); | |
threadPool.setMinThreads(threads); | |
SelectChannelConnector connector = new SelectChannelConnector(); | |
if (NetUtils.isValidLocalHost(url.getHost())) { | |
connector.setHost(url.getHost()); | |
} | |
connector.setPort(url.getPort()); | |
server = new Server(); | |
server.setThreadPool(threadPool); | |
server.addConnector(connector); | |
server.addHandler(new AbstractHandler() { | |
public void handle(String target, HttpServletRequest request, | |
HttpServletResponse response, int dispatch) throws IOException, | |
ServletException { | |
handler.handle(request, response); | |
} | |
}); | |
try { | |
server.start(); | |
} catch (Exception e) { | |
throw new IllegalStateException("Failed to start jetty server on " + url.getAddress() + ", cause: " | |
+ e.getMessage(), e); | |
} | |
} | |
public void close() { | |
super.close(); | |
if (server != null) { | |
try { | |
server.stop(); | |
} catch (Exception e) { | |
logger.warn(e.getMessage(), e); | |
} | |
} | |
} | |
} |