Search in sources :

Example 1 with LifeCycle

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);
}
Also used : LifeCycle(org.eclipse.jetty.util.component.LifeCycle) ContainerLifeCycle(org.eclipse.jetty.util.component.ContainerLifeCycle) ContainerLifeCycle(org.eclipse.jetty.util.component.ContainerLifeCycle) Test(org.junit.Test)

Example 2 with 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();
    }
}
Also used : LifeCycle(org.eclipse.jetty.util.component.LifeCycle) MetricRegistry(com.codahale.metrics.MetricRegistry) Environment(io.dropwizard.setup.Environment) Before(org.junit.Before)

Example 3 with LifeCycle

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");
    }
}
Also used : LifeCycle(org.eclipse.jetty.util.component.LifeCycle) Server(org.eclipse.jetty.server.Server) AtomicReference(java.util.concurrent.atomic.AtomicReference) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) ElasticsearchException(org.elasticsearch.ElasticsearchException) PortsRange(org.elasticsearch.common.transport.PortsRange) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) ElasticsearchException(org.elasticsearch.ElasticsearchException) BindTransportException(org.elasticsearch.transport.BindTransportException) BoundTransportAddress(org.elasticsearch.common.transport.BoundTransportAddress) BindTransportException(org.elasticsearch.transport.BindTransportException) Map(java.util.Map)

Example 4 with LifeCycle

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();
    }
}
Also used : LifeCycle(org.eclipse.jetty.util.component.LifeCycle) TimeZone(java.util.TimeZone) DataSourceFactory(io.dropwizard.db.DataSourceFactory) MetricRegistry(com.codahale.metrics.MetricRegistry) Environment(io.dropwizard.setup.Environment) DBIFactory(io.dropwizard.jdbi.DBIFactory)

Example 5 with LifeCycle

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);
        }
    }
}
Also used : LifeCycle(org.eclipse.jetty.util.component.LifeCycle) Destroyable(org.eclipse.jetty.util.component.Destroyable)

Aggregations

LifeCycle (org.eclipse.jetty.util.component.LifeCycle)9 MetricRegistry (com.codahale.metrics.MetricRegistry)2 Environment (io.dropwizard.setup.Environment)2 Map (java.util.Map)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 DataSourceFactory (io.dropwizard.db.DataSourceFactory)1 DBIFactory (io.dropwizard.jdbi.DBIFactory)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 File (java.io.File)1 IOException (java.io.IOException)1 URL (java.net.URL)1 FileSystems (java.nio.file.FileSystems)1 Path (java.nio.file.Path)1 StandardWatchEventKinds (java.nio.file.StandardWatchEventKinds)1 OVERFLOW (java.nio.file.StandardWatchEventKinds.OVERFLOW)1 WatchKey (java.nio.file.WatchKey)1 WatchService (java.nio.file.WatchService)1