[AMQ-7063] Be able to configure ActiveMQ http transport connector by providing a jetty.xml

This commit is contained in:
Jean-Baptiste Onofré 2018-10-11 09:18:32 +02:00
parent a311139bfe
commit ba2447e39a
4 changed files with 132 additions and 8 deletions

View File

@ -16,6 +16,7 @@
*/
package org.apache.activemq.transport;
import java.io.File;
import java.net.InetAddress;
import java.net.URI;
import java.util.Map;
@ -27,15 +28,21 @@ import org.eclipse.jetty.security.ConstraintSecurityHandler;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.security.Constraint;
import org.eclipse.jetty.xml.XmlConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
abstract public class WebTransportServerSupport extends TransportServerSupport {
private final static Logger LOG = LoggerFactory.getLogger(WebTransportServerSupport.class);
protected URI bindAddress;
protected Server server;
protected Connector connector;
protected SocketConnectorFactory socketConnectorFactory;
protected String host;
protected final HttpOptions httpOptions = new HttpOptions();
protected final JettyOptions jettyOptions = new JettyOptions();
public WebTransportServerSupport(URI location) {
super(location);
@ -46,7 +53,22 @@ abstract public class WebTransportServerSupport extends TransportServerSupport {
}
protected void createServer() {
server = new Server();
LOG.info("Starting Jetty server");
if (jettyOptions.getConfig() != null) {
try {
LOG.info("Configuring Jetty server using {}", jettyOptions.getConfig());
File file = new File(jettyOptions.getConfig());
if (!file.exists()) {
throw new IllegalArgumentException("Jetty XML not found: " + file.getAbsolutePath());
}
XmlConfiguration xmlConfiguration = new XmlConfiguration(file.toURI().toURL());
server = (Server) xmlConfiguration.configure();
} catch (Throwable t) {
throw new IllegalStateException("Jetty configuration can't be loaded", t);
}
} else {
server = new Server();
}
try {
server.getClass().getMethod("setStopTimeout", Long.TYPE).invoke(server, 500l);
} catch (Throwable t) {
@ -55,21 +77,31 @@ abstract public class WebTransportServerSupport extends TransportServerSupport {
}
public URI bind() throws Exception {
URI bind = getBindLocation();
String bindHost = bind.getHost();
bindHost = (bindHost == null || bindHost.length() == 0) ? "localhost" : bindHost;
InetAddress addr = InetAddress.getByName(bindHost);
host = addr.getCanonicalHostName();
setConnectorProperty("Host", String.class, host);
setConnectorProperty("Port", Integer.TYPE, bindAddress.getPort());
server.addConnector(connector);
if (server.getConnectors().length == 0) {
LOG.info("Creating Jetty connector");
setConnectorProperty("Host", String.class, host);
setConnectorProperty("Port", Integer.TYPE, bindAddress.getPort());
server.addConnector(connector);
} else {
LOG.info("Using Jetty configured connector");
connector = server.getConnectors()[0];
for (Connector c : server.getConnectors()) {
if (c.getName() != null && c.getName().equalsIgnoreCase("activemq")) {
connector = c;
}
}
setConnectorProperty("Host", String.class, host);
setConnectorProperty("Port", Integer.TYPE, bindAddress.getPort());
server.addConnector(connector);
}
if (addr.isAnyLocalAddress()) {
host = InetAddressUtil.getLocalHostName();
}
URI boundUri = new URI(bind.getScheme(), bind.getUserInfo(), host, bindAddress.getPort(), bind.getPath(), bind.getQuery(), bind.getFragment());
setConnectURI(boundUri);
return boundUri;
@ -94,6 +126,12 @@ abstract public class WebTransportServerSupport extends TransportServerSupport {
}
}
public void setJettyOptions(Map<String, Object> options) {
if (options != null) {
IntrospectionSupport.setProperties(this.jettyOptions, options);
}
}
protected static class HttpOptions {
private boolean enableTrace = false;
@ -105,4 +143,17 @@ abstract public class WebTransportServerSupport extends TransportServerSupport {
this.enableTrace = enableTrace;
}
}
protected static class JettyOptions {
private String config;
public String getConfig() {
return config;
}
public void setConfig(String config) {
this.config = config;
}
}
}

View File

@ -45,8 +45,10 @@ public class HttpTransportFactory extends TransportFactory {
try {
Map<String, String> options = new HashMap<String, String>(URISupport.parseParameters(location));
HttpTransportServer result = new HttpTransportServer(location, this);
Map<String, Object> jettyOptions = IntrospectionSupport.extractProperties(options, "jetty.");
Map<String, Object> httpOptions = IntrospectionSupport.extractProperties(options, "http.");
Map<String, Object> transportOptions = IntrospectionSupport.extractProperties(options, "transport.");
result.setJettyOptions(jettyOptions);
result.setTransportOption(transportOptions);
result.setHttpOptions(httpOptions);
return result;

View File

@ -0,0 +1,49 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.activemq.transport.http;
import org.apache.activemq.broker.BrokerService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class HttpJettyConfigurationTest {
private BrokerService brokerService;
@Before
public void setUp() throws Exception {
brokerService = new BrokerService();
brokerService.setPersistent(false);
brokerService.setUseJmx(false);
brokerService.deleteAllMessages();
brokerService.addConnector("http://0.0.0.0:0?jetty.config=src/test/resources/jetty.xml");
brokerService.start();
}
@After
public void tearDown() throws Exception {
brokerService.stop();
}
@Test
public void test() throws Exception {
// nothing to do
}
}

View File

@ -0,0 +1,22 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="server" class="org.eclipse.jetty.server.Server">
<Arg>
<New class="org.eclipse.jetty.util.thread.QueuedThreadPool">
<Set name="minThreads">10</Set>
<Set name="maxThreads">1000</Set>
</New>
</Arg>
<Call name="addConnector">
<Arg>
<New id="activemq" class="org.eclipse.jetty.server.ServerConnector">
<Arg name="server"><Ref refid="server"/></Arg>
<Arg name="acceptors" type="int"><Property name="jetty.http.acceptors" default="-1"/></Arg>
<Arg name="selectors" type="int"><Property name="jetty.http.selectors" default="-1"/></Arg>
</New>
</Arg>
</Call>
</Configure>