use of org.eclipse.jetty.util.component.LifeCycle in project dropwizard by dropwizard.
the class LifecycleEnvironmentTest method managesLifeCycleObjects.
@Test
public void managesLifeCycleObjects() throws Exception {
final LifeCycle lifeCycle = mock(LifeCycle.class);
environment.manage(lifeCycle);
final ContainerLifeCycle container = new ContainerLifeCycle();
environment.attach(container);
assertThat(container.getBeans()).contains(lifeCycle);
}
use of org.eclipse.jetty.util.component.LifeCycle in project dropwizard by dropwizard.
the class DropwizardApacheConnectorTest method setup.
@Before
public void setup() throws Exception {
JerseyClientConfiguration clientConfiguration = new JerseyClientConfiguration();
clientConfiguration.setConnectionTimeout(Duration.milliseconds(SLEEP_TIME_IN_MILLIS / 2));
clientConfiguration.setTimeout(Duration.milliseconds(DEFAULT_CONNECT_TIMEOUT_IN_MILLIS));
environment = new Environment("test-dropwizard-apache-connector", Jackson.newObjectMapper(), Validators.newValidator(), new MetricRegistry(), getClass().getClassLoader());
client = (JerseyClient) new JerseyClientBuilder(environment).using(clientConfiguration).build("test");
for (LifeCycle lifeCycle : environment.lifecycle().getManagedObjects()) {
lifeCycle.start();
}
}
use of org.eclipse.jetty.util.component.LifeCycle in project elasticsearch-jetty by sonian.
the class JettyHttpServerTransport method doStart.
@Override
protected void doStart() throws ElasticsearchException {
PortsRange portsRange = new PortsRange(port);
final AtomicReference<Exception> lastException = new AtomicReference<Exception>();
Log.setLog(loggerWrapper);
portsRange.iterate(new PortsRange.PortCallback() {
@Override
public boolean onPortNumber(int portNumber) {
try {
Server server = null;
XmlConfiguration lastXmlConfiguration = null;
Object[] objs = new Object[jettyConfig.length];
Map<String, String> esProperties = jettySettings(bindHost, portNumber);
for (int i = 0; i < jettyConfig.length; i++) {
String configFile = jettyConfig[i];
URL config = environment.resolveConfig(configFile);
XmlConfiguration xmlConfiguration = new XmlConfiguration(config);
// in the later configurations
if (lastXmlConfiguration != null) {
xmlConfiguration.getIdMap().putAll(lastXmlConfiguration.getIdMap());
} else {
xmlConfiguration.getIdMap().put("ESServerTransport", JettyHttpServerTransport.this);
xmlConfiguration.getIdMap().put("ESClient", client);
}
// Inject elasticsearch properties
xmlConfiguration.getProperties().putAll(esProperties);
objs[i] = xmlConfiguration.configure();
lastXmlConfiguration = xmlConfiguration;
}
// Find jetty Server with id jettyConfigServerId
Object serverObject = lastXmlConfiguration.getIdMap().get(jettyConfigServerId);
if (serverObject != null) {
if (serverObject instanceof Server) {
server = (Server) serverObject;
}
} else {
// For compatibility - if it's not available, find first available jetty Server
for (Object obj : objs) {
if (obj instanceof Server) {
server = (Server) obj;
break;
}
}
}
if (server == null) {
logger.error("Cannot find server with id [{}] in configuration files [{}]", jettyConfigServerId, jettyConfig);
lastException.set(new ElasticsearchException("Cannot find server with id " + jettyConfigServerId));
return true;
}
// Keep it for now for backward compatibility with previous versions of jetty.xml
server.setAttribute(TRANSPORT_ATTRIBUTE, JettyHttpServerTransport.this);
// Start all lifecycle objects configured by xml configurations
for (Object obj : objs) {
if (obj instanceof LifeCycle) {
LifeCycle lifeCycle = (LifeCycle) obj;
if (!lifeCycle.isRunning()) {
lifeCycle.start();
}
}
}
jettyServer = server;
lastException.set(null);
} catch (BindException e) {
lastException.set(e);
return false;
} catch (Exception e) {
logger.error("Jetty Startup Failed ", e);
lastException.set(e);
return true;
}
return true;
}
});
if (lastException.get() != null) {
throw new BindHttpException("Failed to bind to [" + port + "]", lastException.get());
}
InetSocketAddress jettyBoundAddress = findFirstInetConnector(jettyServer);
if (jettyBoundAddress != null) {
InetSocketAddress publishAddress;
try {
publishAddress = new InetSocketAddress(networkService.resolvePublishHostAddress(publishHost), jettyBoundAddress.getPort());
} catch (Exception e) {
throw new BindTransportException("Failed to resolve publish address", e);
}
this.boundAddress = new BoundTransportAddress(new InetSocketTransportAddress(jettyBoundAddress), new InetSocketTransportAddress(publishAddress));
} else {
throw new BindHttpException("Failed to find a jetty connector with Inet transport");
}
}
use of org.eclipse.jetty.util.component.LifeCycle in project dropwizard by dropwizard.
the class DBIClient method before.
@Override
protected void before() throws Throwable {
final Environment environment = new Environment("test", Jackson.newObjectMapper(), Validators.newValidator(), new MetricRegistry(), getClass().getClassLoader());
final DataSourceFactory dataSourceFactory = new DataSourceFactory();
dataSourceFactory.setDriverClass("org.h2.Driver");
dataSourceFactory.setUrl("jdbc:h2:tcp://localhost/fldb");
dataSourceFactory.setUser("sa");
dataSourceFactory.setPassword("");
// Set the time zone of the database
final DBIFactory dbiFactory = new DBIFactory() {
@Override
protected Optional<TimeZone> databaseTimeZone() {
return Optional.of(dbTimeZone);
}
};
dbi = dbiFactory.build(environment, dataSourceFactory, "test-jdbi-time-zones");
// Start the DB pool
managedObjects = environment.lifecycle().getManagedObjects();
for (LifeCycle managedObject : managedObjects) {
managedObject.start();
}
}
use of org.eclipse.jetty.util.component.LifeCycle in project jetty.project by eclipse.
the class ShutdownThread method run.
/* ------------------------------------------------------------ */
@Override
public void run() {
for (LifeCycle lifeCycle : _thread._lifeCycles) {
try {
if (lifeCycle.isStarted()) {
lifeCycle.stop();
LOG.debug("Stopped {}", lifeCycle);
}
if (lifeCycle instanceof Destroyable) {
((Destroyable) lifeCycle).destroy();
LOG.debug("Destroyed {}", lifeCycle);
}
} catch (Exception ex) {
LOG.debug(ex);
}
}
}
Aggregations