Example usage for java.util.concurrent Semaphore release

List of usage examples for java.util.concurrent Semaphore release

Introduction

In this page you can find the example usage for java.util.concurrent Semaphore release.

Prototype

public void release() 

Source Link

Document

Releases a permit, returning it to the semaphore.

Usage

From source file:org.apache.servicemix.nmr.core.ChannelImpl.java

protected void handleFailure(InternalExchange exchange, RuntimeException e, boolean dispatch) {
    LOG.warn("Error processing exchange " + exchange, e);
    if (dispatch) {
        exchange.setError(e);/* ww w  . ja  va  2s . co m*/
        for (ExchangeListener l : nmr.getListenerRegistry().getListeners(ExchangeListener.class)) {
            l.exchangeFailed(exchange);
        }
        // Rethrow the exception so that sendSync are unblocked
        throw e;
    } else {
        // If the exchange is active, let's try to send an error on behalf of the endpoint
        if (exchange.getStatus() == Status.Active) {
            try {
                exchange.setError(e);
                send(exchange);
            } catch (RuntimeException e2) {
                for (ExchangeListener l : nmr.getListenerRegistry().getListeners(ExchangeListener.class)) {
                    l.exchangeFailed(exchange);
                }
            }
        } else {
            exchange.setError(e);
            Semaphore lock = exchange.getRole() == Role.Provider ? exchange.getConsumerLock(false)
                    : exchange.getProviderLock(false);
            if (lock != null) {
                lock.release();
            }
            for (ExchangeListener l : nmr.getListenerRegistry().getListeners(ExchangeListener.class)) {
                l.exchangeFailed(exchange);
            }
        }
    }
}

From source file:com.parse.ParseAnalyticsTest.java

@Test
public void testTrackEventInBackgroundNormalCallback() throws Exception {
    final Map<String, String> dimensions = new HashMap<>();
    dimensions.put("key", "value");
    final Semaphore done = new Semaphore(0);
    ParseAnalytics.trackEventInBackground("test", dimensions, new SaveCallback() {
        @Override//  w w w . j  a v a 2 s . c  om
        public void done(ParseException e) {
            assertNull(e);
            done.release();
        }
    });

    // Make sure the callback is called
    assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
    verify(controller, times(1)).trackEventInBackground(eq("test"), eq(dimensions), isNull(String.class));

    final Semaphore doneAgain = new Semaphore(0);
    ParseAnalytics.trackEventInBackground("test", new SaveCallback() {
        @Override
        public void done(ParseException e) {
            assertNull(e);
            doneAgain.release();
        }
    });

    // Make sure the callback is called
    assertTrue(doneAgain.tryAcquire(1, 10, TimeUnit.SECONDS));
    verify(controller, times(1)).trackEventInBackground(eq("test"), Matchers.<Map<String, String>>eq(null),
            isNull(String.class));
}

From source file:ch.admin.suis.msghandler.sender.SenderJob.java

@SuppressWarnings("unchecked")
@Override/*from  w w w .j  a v a  2 s. c  o m*/
public void execute(JobExecutionContext context) throws JobExecutionException {
    LOG.debug("sender job started");

    // get the objects that are necessary for the sender
    JobDataMap dataMap = context.getJobDetail().getJobDataMap();

    final MessageHandlerContext handlerContext = (MessageHandlerContext) dataMap
            .get(MessageHandlerContext.MESSAGE_HANDLER_CONTEXT_PARAM);
    final List<Outbox> outbox = (List<Outbox>) dataMap.get(MessageHandlerContext.OUTBOX_PARAM);

    // pre-conditions check
    Validate.notNull(handlerContext);
    Validate.notNull(outbox);

    Semaphore sequenceLock = handlerContext.getSequenceLock();

    try {
        sequenceLock.acquire();

        try {
            if (this instanceof TransparentSenderJob) {
                new Sender().execute(new TransparentSenderSessionImpl(handlerContext, outbox));
            } else {
                new Sender().execute(new SenderSessionImpl(handlerContext, outbox));
            }
        } finally {
            sequenceLock.release();
        }
    } catch (InterruptedException e) {
        LOG.info("sender terminated while waiting for other jobs to complete");
        Thread.currentThread().interrupt();
    }
}

From source file:com.netflix.curator.framework.recipes.leader.TestLeaderSelector.java

@Test
public void testServerDying() throws Exception {
    Timing timing = new Timing();
    LeaderSelector selector = null;// w w  w.j  a va 2 s  . c  o m
    CuratorFramework client = CuratorFrameworkFactory.builder().connectionTimeoutMs(timing.connection())
            .connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1))
            .sessionTimeoutMs(timing.session()).build();
    client.start();
    try {
        final Semaphore semaphore = new Semaphore(0);
        LeaderSelectorListener listener = new LeaderSelectorListener() {
            @Override
            public void takeLeadership(CuratorFramework client) throws Exception {
                semaphore.release();
                Thread.sleep(Integer.MAX_VALUE);
            }

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.LOST) {
                    semaphore.release();
                }
            }
        };
        selector = new LeaderSelector(client, "/leader", listener);
        selector.start();

        timing.acquireSemaphore(semaphore);

        server.close();

        timing.acquireSemaphore(semaphore);
    } finally {
        IOUtils.closeQuietly(selector);
        IOUtils.closeQuietly(client);
    }
}

From source file:com.netflix.curator.framework.recipes.leader.TestLeaderSelectorCluster.java

@Test
public void testRestart() throws Exception {
    final Timing timing = new Timing();

    CuratorFramework client = null;//w  w  w .j  av  a 2s.c o m
    TestingCluster cluster = new TestingCluster(3);
    cluster.start();
    try {
        client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(),
                timing.connection(), new RetryOneTime(1));
        client.start();

        final Semaphore semaphore = new Semaphore(0);
        LeaderSelectorListener listener = new LeaderSelectorListener() {
            @Override
            public void takeLeadership(CuratorFramework client) throws Exception {
                List<String> names = client.getChildren().forPath("/leader");
                Assert.assertTrue(names.size() > 0);
                semaphore.release();
            }

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
            }
        };
        LeaderSelector selector = new LeaderSelector(client, "/leader", listener);
        selector.autoRequeue();
        selector.start();
        Assert.assertTrue(timing.acquireSemaphore(semaphore));

        InstanceSpec connectionInstance = cluster
                .findConnectionInstance(client.getZookeeperClient().getZooKeeper());
        cluster.killServer(connectionInstance);

        Assert.assertTrue(timing.multiple(4).acquireSemaphore(semaphore));
    } finally {
        IOUtils.closeQuietly(client);
        IOUtils.closeQuietly(cluster);
    }
}

From source file:gobblin.runtime.job_catalog.FSJobCatalogHelperTest.java

@Test(dependsOnMethods = { "testloadGenericJobConfig" })
public void testPathAlterationObserver() throws Exception {
    PathAlterationObserverScheduler detector = new PathAlterationObserverScheduler(1000);
    final Set<Path> fileAltered = Sets.newHashSet();
    final Semaphore semaphore = new Semaphore(0);
    PathAlterationListener listener = new PathAlterationListenerAdaptor() {

        @Override/*from   w  w w.j av  a 2s. co m*/
        public void onFileCreate(Path path) {
            fileAltered.add(path);
            semaphore.release();
        }

        @Override
        public void onFileChange(Path path) {
            fileAltered.add(path);
            semaphore.release();
        }
    };

    detector.addPathAlterationObserver(listener, Optional.<PathAlterationObserver>absent(),
            new Path(this.jobConfigDir.getPath()));
    try {
        detector.start();
        // Give the monitor some time to start
        Thread.sleep(1000);

        File jobConfigFile = new File(this.subDir11, "test111.pull");
        Files.touch(jobConfigFile);

        File newJobConfigFile = new File(this.subDir11, "test112.pull");
        Files.append("k1=v1", newJobConfigFile, ConfigurationKeys.DEFAULT_CHARSET_ENCODING);

        semaphore.acquire(2);
        Assert.assertEquals(fileAltered.size(), 2);

        Assert.assertTrue(fileAltered.contains(new Path("file:" + jobConfigFile)));
        Assert.assertTrue(fileAltered.contains(new Path("file:" + newJobConfigFile)));
    } finally {
        detector.stop();
    }
}

From source file:org.apache.gobblin.runtime.job_catalog.FSJobCatalogHelperTest.java

@Test(enabled = false, dependsOnMethods = { "testloadGenericJobConfig" })
public void testPathAlterationObserver() throws Exception {
    PathAlterationObserverScheduler detector = new PathAlterationObserverScheduler(1000);
    final Set<Path> fileAltered = Sets.newHashSet();
    final Semaphore semaphore = new Semaphore(0);
    PathAlterationListener listener = new PathAlterationListenerAdaptor() {

        @Override//  ww w  . j  a v  a  2  s  .c  o  m
        public void onFileCreate(Path path) {
            fileAltered.add(path);
            semaphore.release();
        }

        @Override
        public void onFileChange(Path path) {
            fileAltered.add(path);
            semaphore.release();
        }
    };

    detector.addPathAlterationObserver(listener, Optional.<PathAlterationObserver>absent(),
            new Path(this.jobConfigDir.getPath()));
    try {
        detector.start();
        // Give the monitor some time to start
        Thread.sleep(1000);

        File jobConfigFile = new File(this.subDir11, "test111.pull");
        Files.touch(jobConfigFile);

        File newJobConfigFile = new File(this.subDir11, "test112.pull");
        Files.append("k1=v1", newJobConfigFile, ConfigurationKeys.DEFAULT_CHARSET_ENCODING);

        semaphore.acquire(2);
        Assert.assertEquals(fileAltered.size(), 2);

        Assert.assertTrue(fileAltered.contains(new Path("file:" + jobConfigFile)));
        Assert.assertTrue(fileAltered.contains(new Path("file:" + newJobConfigFile)));
    } finally {
        detector.stop();
    }
}

From source file:org.commoncrawl.service.listcrawler.DataTransferAgent.java

static int uploadSingeFile(CCBridgeServerMapping mapping, FileSystem fs, Configuration conf, Path hdfsFilePath,
        String uploadName, EventLoop eventLoop) throws IOException {

    final FileStatus fileStatus = fs.getFileStatus(hdfsFilePath);
    LOG.info("Uploading:" + uploadName + " size:" + fileStatus.getLen() + " to:" + mapping._internalName);

    {// www  . j a va 2s .c o m
        // construct url 
        URL fePostURL = new URL("http://" + mapping._externalName + ":8090/");
        LOG.info("POST URL IS:" + fePostURL.toString());

        // open input stream 
        final FSDataInputStream is = fs.open(hdfsFilePath);
        final Semaphore blockingSemaphore = new Semaphore(0);
        NIOHttpConnection connection = null;
        try {
            // create connection 
            connection = new NIOHttpConnection(fePostURL, eventLoop.getSelector(), eventLoop.getResolver(),
                    null);
            // set listener 
            connection.setListener(new Listener() {

                @Override
                public void HttpConnectionStateChanged(NIOHttpConnection theConnection, State oldState,
                        State state) {
                    LOG.info("Connection State Changed to:" + state.toString());
                    if (state == State.DONE || state == State.ERROR) {
                        //LOG.info("Connection Transition to Done or Error");
                        //LOG.info("Response Headers:" + theConnection.getResponseHeaders().toString());
                        blockingSemaphore.release();
                    }
                }

                @Override
                public void HttpContentAvailable(NIOHttpConnection theConnection, NIOBufferList contentBuffer) {
                    // TODO Auto-generated method stub

                }
            });
            // set headers 
            connection.getRequestHeaders().reset();
            connection.getRequestHeaders().prepend("PUT /put?src=" + uploadName + " HTTP/1.1", null);
            connection.getRequestHeaders().set("Host", mapping._internalName + ":8090");
            connection.getRequestHeaders().set("Content-Length", Long.toString(fileStatus.getLen()));
            connection.getRequestHeaders().set("Connection", "keep-alive");
            connection.setPopulateDefaultHeaderItems(false);

            final LinkedBlockingDeque<BufferStruct> _loaderQueue = new LinkedBlockingDeque<BufferStruct>(20);
            final AtomicBoolean eof = new AtomicBoolean();
            final ByteBuffer sentinel = ByteBuffer.allocate(4096);
            sentinel.position(sentinel.position());
            final Thread loaderThread = new Thread(new Runnable() {

                int _id = 0;

                @Override
                public void run() {
                    int bytesRead;
                    byte incomingBuffer[] = new byte[4096 * 10];
                    try {
                        while ((bytesRead = is.read(incomingBuffer)) != -1) {
                            ByteBuffer buffer = ByteBuffer.wrap(incomingBuffer, 0, bytesRead);
                            buffer.position(bytesRead);

                            //LOG.info("Loader Thread Read:"+ bytesRead + " Buffer:" + ++_id);
                            try {
                                _loaderQueue.put(new BufferStruct(buffer, _id));
                            } catch (InterruptedException e) {
                                LOG.error(CCStringUtils.stringifyException(e));
                                break;
                            }
                            incomingBuffer = new byte[4096 * 10];
                        }
                        try {
                            _loaderQueue.put(new BufferStruct(sentinel, ++_id));
                        } catch (InterruptedException e) {
                        }
                    } catch (IOException e) {
                        LOG.error(CCStringUtils.stringifyException(e));
                        return;
                    }
                }

            });

            loaderThread.start();

            // set data source ... 
            connection.setDataSource(new DataSource() {

                int bytesTransferred = 0;

                @Override
                public boolean read(NIOBufferList dataBuffer) throws IOException {
                    if (eof.get())
                        return true;
                    //LOG.info("Connect read callback triggered");
                    BufferStruct buffer = _loaderQueue.poll();
                    if (buffer != null) {
                        if (buffer._buffer != sentinel) {
                            //LOG.info("Got Buffer:"+ buffer._id);
                            if (buffer._id == 1) {
                                //LOG.info("Inital Buffer Bytes:" + new String(buffer._buffer.array(),0,10).toString());
                            }
                            dataBuffer.write(buffer._buffer);
                            bytesTransferred += buffer._buffer.limit();
                            //LOG.info("Read:" + buffer._buffer.limit() + " Transfered:" + bytesTransferred);
                            return false;
                        } else {
                            //LOG.info("EOF Condition");
                            dataBuffer.write(sentinel);
                            eof.set(true);
                            return true;
                        }
                    }
                    return false;
                }
            });

            // open connection 
            connection.open();
            // wait for connection to complete ... 
            blockingSemaphore.acquireUninterruptibly();
            // kill loader thread 
            loaderThread.interrupt();
            try {
                LOG.info("Waiting for Loader Thread");
                loaderThread.join();
                LOG.info("Done Waiting for Loader Thread");
            } catch (InterruptedException e) {
            }
        } finally {
            is.close();
            if (connection != null) {
                connection.close();
                LOG.info("Response Code for File:" + uploadName + "to Host: " + mapping._internalName + " is:"
                        + connection.getResponseHeaders().getHttpResponseCode());
                return connection.getResponseHeaders().getHttpResponseCode();
                /*
                if (connection.getResponseHeaders().getHttpResponseCode() != 200) { 
                  throw new IOException("Failed to upload file:" + dataFile.getName() + " responseCode:" + connection.getResponseHeaders().getHttpResponseCode());
                }
                */
            }
        }
    }
    // something went wrong ??? 
    LOG.error("Failed to upload file:" + uploadName + " unknown response code");
    return 500;
}

From source file:gobblin.util.SchedulerUtilsTest.java

@Test(dependsOnMethods = { "testLoadJobConfigsForCommonPropsFile", "testloadGenericJobConfig" })
public void testPathAlterationObserver() throws Exception {
    PathAlterationObserverScheduler monitor = new PathAlterationObserverScheduler(1000);
    final Set<Path> fileAltered = Sets.newHashSet();
    final Semaphore semaphore = new Semaphore(0);
    PathAlterationListener listener = new PathAlterationListenerAdaptor() {

        @Override//from  w w w. j av  a2s. c o m
        public void onFileCreate(Path path) {
            fileAltered.add(path);
            semaphore.release();
        }

        @Override
        public void onFileChange(Path path) {
            fileAltered.add(path);
            semaphore.release();
        }
    };

    SchedulerUtils.addPathAlterationObserver(monitor, listener, new Path(this.jobConfigDir.getPath()));
    try {
        monitor.start();
        // Give the monitor some time to start
        Thread.sleep(1000);

        File jobConfigFile = new File(this.subDir11, "test111.pull");
        Files.touch(jobConfigFile);

        File commonPropsFile = new File(this.subDir1, "test.properties");
        Files.touch(commonPropsFile);

        File newJobConfigFile = new File(this.subDir11, "test112.pull");
        Files.append("k1=v1", newJobConfigFile, ConfigurationKeys.DEFAULT_CHARSET_ENCODING);

        semaphore.acquire(3);
        Assert.assertEquals(fileAltered.size(), 3);

        Assert.assertTrue(fileAltered.contains(new Path("file:" + jobConfigFile)));
        Assert.assertTrue(fileAltered.contains(new Path("file:" + commonPropsFile)));
        Assert.assertTrue(fileAltered.contains(new Path("file:" + newJobConfigFile)));
    } finally {
        monitor.stop();
    }
}

From source file:org.apache.storm.daemon.DrpcServer.java

@Override
public void result(String id, String result) throws AuthorizationException, TException {
    meterResultCalls.mark();//  w  w  w.j  a v a2 s.c  o  m
    InternalRequest internalRequest = this.outstandingRequests.get(id);
    if (internalRequest != null) {
        Map<String, String> map = ImmutableMap.of(DRPCAuthorizerBase.FUNCTION_NAME, internalRequest.function);
        checkAuthorization(authorizer, map, "result");
        Semaphore sem = internalRequest.sem;
        LOG.debug("Received result {} for {} at {}", result, id, System.currentTimeMillis());
        if (sem != null) {
            internalRequest.result = result;
            sem.release();
        }
    }
}