Java Code Examples for org.eclipse.jetty.util.thread.Sweeper

Following code examples demonstrate how to use org.eclipse.jetty.util.thread.Sweeper from jetty. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.eclipse.jetty.util.thread.Sweeper and various code implementation of this class.

    @Override
    protected void doStart() throws Exception
    {
        HttpClient client = getHttpClient();
        this.connectionPool = newConnectionPool(client);
        addBean(connectionPool);
        super.doStart();
        Sweeper sweeper = client.getBean(Sweeper.class);
        if (sweeper != null)
            sweeper.offer(connectionPool);
    } 


    @Override
    protected void doStop() throws Exception
    {
        HttpClient client = getHttpClient();
        Sweeper sweeper = client.getBean(Sweeper.class);
        if (sweeper != null)
            sweeper.remove(connectionPool);
        super.doStop();
        removeBean(connectionPool);
    } 

    @Override
    protected void doStart() throws Exception
    {
        this.connectionPool = newConnectionPool(client);
        addBean(connectionPool);
        super.doStart();
        Sweeper sweeper = client.getBean(Sweeper.class);
        if (sweeper != null && connectionPool instanceof Sweeper.Sweepable)
            sweeper.offer((Sweeper.Sweepable)connectionPool);
    } 

    @Override
    protected void doStop() throws Exception
    {
        Sweeper sweeper = client.getBean(Sweeper.class);
        if (sweeper != null && connectionPool instanceof Sweeper.Sweepable)
            sweeper.remove((Sweeper.Sweepable)connectionPool);
        super.doStop();
        removeBean(connectionPool);
    } 

    @Override
    public boolean sweep()
    {
        List<Connection> toSweep;
        lock();
        try
        {
            toSweep = activeConnections.stream()
                    .filter(connection -> connection instanceof Sweeper.Sweepable)
                    .collect(Collectors.toList());
        }
        finally
        {
            unlock();
        }

        for (Connection connection : toSweep)
        {
            if (((Sweeper.Sweepable)connection).sweep())
            {
                boolean removed = remove(connection, true);
                LOG.warn("Connection swept: {}{}{} from active connections{}{}",
                        connection,
                        System.lineSeparator(),
                        removed ? "Removed" : "Not removed",
                        System.lineSeparator(),
                        dump());
            }
        }

        return false;
    } 

    @Override
    public boolean sweep()
    {
        List<Connection> toSweep = new ArrayList<>();
        lock();
        try
        {
            busyConnections.values().stream()
                    .map(holder -> holder.connection)
                    .filter(connection -> connection instanceof Sweeper.Sweepable)
                    .collect(Collectors.toCollection(() -> toSweep));
            muxedConnections.values().stream()
                    .map(holder -> holder.connection)
                    .filter(connection -> connection instanceof Sweeper.Sweepable)
                    .collect(Collectors.toCollection(() -> toSweep));
        }
        finally
        {
            unlock();
        }

        for (Connection connection : toSweep)
        {
            if (((Sweeper.Sweepable)connection).sweep())
            {
                boolean removed = remove(connection, true);
                LOG.warn("Connection swept: {}{}{} from active connections{}{}",
                        connection,
                        System.lineSeparator(),
                        removed ? "Removed" : "Not removed",
                        System.lineSeparator(),
                        dump());
            }
        }

        return false;
    } 

    @Override
    public boolean sweep()
    {
        List<Sweeper.Sweepable> toSweep = new ArrayList<>();
        final ReentrantLock lock = this.lock;
        lock.lock();
        try
        {
            for (Connection connection : getActiveConnections())
            {
                if (connection instanceof Sweeper.Sweepable)
                    toSweep.add(((Sweeper.Sweepable)connection));
            }
        }
        finally
        {
            lock.unlock();
        }

        for (Sweeper.Sweepable candidate : toSweep)
        {
            if (candidate.sweep())
            {
                boolean removed = getActiveConnections().remove(candidate);
                LOG.warn("Connection swept: {}{}{} from active connections{}{}",
                        candidate,
                        System.lineSeparator(),
                        removed ? "Removed" : "Not removed",
                        System.lineSeparator(),
                        dump());
            }
        }

        return false;
    } 

Advertisement
Javadoc


A utility class to perform periodic sweeping of resources.

Sweepable resources

may be added to or removed from a Sweeper and the resource implementation decides whether it should be swept or not.

If a Sweepable resources is itself a container of other sweepable resources, it will forward the sweep operation to children resources, and so on recursively.

Typical usage is to add Sweeper as a bean to an existing container: Server server = new Server(); server.addBean(new Sweeper(), true); server.start(); Code that knows it has sweepable resources can then lookup the Sweeper and offer the sweepable resources to it: class MyComponent implements Sweeper.Sweepable { private final long creation; private volatile destroyed; MyComponent(Server server) { this.creation = System.nanoTime(); Sweeper sweeper = server.getBean(Sweeper.class); sweeper.offer(this); void destroy() { destroyed = true; @Override public boolean sweep() { return destroyed;

Read More
Advertisement