Search in sources :

Example 96 with ArrayBlockingQueue

use of java.util.concurrent.ArrayBlockingQueue in project ff4j by ff4j.

the class ExceptionsTest method testExceptionHandler.

@Test
public void testExceptionHandler() {
    EventRejectedExecutionHandler ereh = new EventRejectedExecutionHandler();
    final BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(2);
    // Executor with worker to process threads
    RejectedExecutionHandler rej = new EventRejectedExecutionHandler();
    ThreadFactory tFactorty = new PublisherThreadFactory();
    ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, queue, tFactorty, rej);
    ereh.rejectedExecution(new Thread(), executor);
}
Also used : PublisherThreadFactory(org.ff4j.audit.PublisherThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) PublisherThreadFactory(org.ff4j.audit.PublisherThreadFactory) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) EventRejectedExecutionHandler(org.ff4j.audit.EventRejectedExecutionHandler) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) EventRejectedExecutionHandler(org.ff4j.audit.EventRejectedExecutionHandler) Test(org.junit.Test)

Example 97 with ArrayBlockingQueue

use of java.util.concurrent.ArrayBlockingQueue in project xian by happyyangyuan.

the class TestFrameworkEdges method internalTestPathsFromProtectingInBackground.

private void internalTestPathsFromProtectingInBackground(CreateMode mode) throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), 1, new RetryOneTime(1));
    try {
        client.start();
        client.create().creatingParentsIfNeeded().forPath("/a/b/c");
        final BlockingQueue<String> paths = new ArrayBlockingQueue<String>(2);
        BackgroundCallback callback = new BackgroundCallback() {

            @Override
            public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                paths.put(event.getName());
                paths.put(event.getPath());
            }
        };
        final String TEST_PATH = "/a/b/c/test-";
        client.create().withMode(mode).inBackground(callback).forPath(TEST_PATH);
        String name1 = paths.take();
        String path1 = paths.take();
        client.close();
        client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), 1, new RetryOneTime(1));
        client.start();
        CreateBuilderImpl createBuilder = (CreateBuilderImpl) client.create().withProtection();
        client.create().forPath(createBuilder.adjustPath(TEST_PATH));
        createBuilder.debugForceFindProtectedNode = true;
        createBuilder.withMode(mode).inBackground(callback).forPath(TEST_PATH);
        String name2 = paths.take();
        String path2 = paths.take();
        Assert.assertEquals(ZKPaths.getPathAndNode(name1).getPath(), ZKPaths.getPathAndNode(TEST_PATH).getPath());
        Assert.assertEquals(ZKPaths.getPathAndNode(name2).getPath(), ZKPaths.getPathAndNode(TEST_PATH).getPath());
        Assert.assertEquals(ZKPaths.getPathAndNode(path1).getPath(), ZKPaths.getPathAndNode(TEST_PATH).getPath());
        Assert.assertEquals(ZKPaths.getPathAndNode(path2).getPath(), ZKPaths.getPathAndNode(TEST_PATH).getPath());
        client.delete().deletingChildrenIfNeeded().forPath("/a/b/c");
        client.delete().forPath("/a/b");
        client.delete().forPath("/a");
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent)

Example 98 with ArrayBlockingQueue

use of java.util.concurrent.ArrayBlockingQueue in project xian by happyyangyuan.

the class TestLeaderSelector method testLeaderNodeDeleteOnInterrupt.

@Test
public void testLeaderNodeDeleteOnInterrupt() throws Exception {
    Timing timing = new Timing();
    LeaderSelector selector = null;
    CuratorFramework client = null;
    try {
        client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
        final CountDownLatch reconnectedLatch = new CountDownLatch(1);
        ConnectionStateListener connectionStateListener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.RECONNECTED) {
                    reconnectedLatch.countDown();
                }
            }
        };
        client.getConnectionStateListenable().addListener(connectionStateListener);
        client.start();
        final BlockingQueue<Thread> queue = new ArrayBlockingQueue<Thread>(1);
        LeaderSelectorListener listener = new LeaderSelectorListener() {

            @Override
            public void takeLeadership(CuratorFramework client) throws Exception {
                queue.add(Thread.currentThread());
                try {
                    Thread.currentThread().join();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
            }
        };
        selector = new LeaderSelector(client, "/leader", listener);
        selector.start();
        Thread leaderThread = queue.take();
        server.stop();
        leaderThread.interrupt();
        server.restart();
        Assert.assertTrue(timing.awaitLatch(reconnectedLatch));
        timing.sleepABit();
        Assert.assertEquals(client.getChildren().forPath("/leader").size(), 0);
    } finally {
        CloseableUtils.closeQuietly(selector);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Example 99 with ArrayBlockingQueue

use of java.util.concurrent.ArrayBlockingQueue in project divolte-collector by divolte.

the class GoogleCloudPubSubFlusherTest method testMessageBatchSentToPublisher.

@Test
public void testMessageBatchSentToPublisher() {
    final Publisher publisher = mockPublisher.orElseThrow(IllegalStateException::new);
    // Process a bunch of messages.
    final DivolteSchema schema = new DivolteSchema(MINIMAL_SCHEMA, Optional.empty());
    final GoogleCloudPubSubFlusher flusher = new GoogleCloudPubSubFlusher(publisher, schema);
    final Queue<Item<AvroRecordBuffer>> items = Stream.generate(this::generateMessage).limit(10).map(this::itemFromAvroRecordBuffer).collect(Collectors.toCollection(() -> new ArrayBlockingQueue<>(10)));
    flusher.process(items);
    // Check the messages were all forwarded to the publisher.
    verify(publisher, times(10)).publish(any(PubsubMessage.class));
    verifyNoMoreInteractions(publisher);
}
Also used : Item(io.divolte.server.processing.Item) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Publisher(com.google.cloud.pubsub.v1.Publisher) PubsubMessage(com.google.pubsub.v1.PubsubMessage) DivolteSchema(io.divolte.server.DivolteSchema) Test(org.junit.Test)

Example 100 with ArrayBlockingQueue

use of java.util.concurrent.ArrayBlockingQueue in project AJSC by att.

the class Runner method main.

/**
 * @param args
 * @throws Exception
 */
/**
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    try {
        System.out.println("Initializing the AJSC . . . ");
        // cleaning AJSC_HOME. During a Soa Cloud Install to a node with an
        // InstallRoot of "/",
        // AJSC_HOME is prefixed with "//" and causes subsequent program
        // failure. This code
        // 'cleans' AJSC_HOME and replaces "//" with "/" for those
        // particular Installations
        String ajscHome = System.getProperty("AJSC_HOME").replace("//", "/");
        System.setProperty("AJSC_HOME", ajscHome);
        System.out.println("AJSC_HOME has been set to: " + System.getProperty("AJSC_HOME"));
        String ajscConfHome = System.getProperty("AJSC_CONF_HOME");
        if (ajscConfHome == null || ajscConfHome.trim().length() <= 0) {
            System.setProperty("AJSC_CONF_HOME", ajscHome);
        } else {
            System.setProperty("AJSC_CONF_HOME", ajscConfHome.replace("//", "/"));
        }
        System.out.println("AJSC_CONF_HOME has been set to: " + System.getProperty("AJSC_CONF_HOME"));
        // Scan the sysprops directory and set the properties to System
        Properties prop = new Properties();
        String dirName = System.getProperty("AJSC_CONF_HOME") + "/etc/sysprops";
        System.out.println("System properties scanning directory has been set to: " + dirName);
        scanSystemPropsDir(dirName, prop);
        for (String key : prop.stringPropertyNames()) {
            System.setProperty(key, (String) prop.get(key));
        }
        String ajscSharedConfigLoc = System.getProperty("AJSC_SHARED_CONFIG");
        if (ajscSharedConfigLoc == null || ajscSharedConfigLoc.trim().length() <= 0) {
            ajscSharedConfigLoc = System.getProperty("AJSC_CONF_HOME");
        }
        String AJSC_JETTY_CONFIG_XML_LOC = System.getProperty("AJSC_HOME") + "/etc/ajsc-jetty.xml";
        // Setting up Temporary Directory for jetty to deploy webapps from
        File dir = new File(System.getProperty("AJSC_HOME") + File.separator + "jetty" + File.separator + "webapps");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        // Setting up log directory for Java Util (JUL) Framework to work as intended
        File logDir = new File(System.getProperty("AJSC_HOME") + File.separator + "log");
        if (!logDir.exists()) {
            logDir.mkdirs();
        }
        Path basedir = FileSystems.getDefault().getPath(System.getProperty("AJSC_HOME") + File.separator + "jetty" + File.separator + "webapps");
        // Path p = Files.createTempDirectory("ajsc-temp.dir");
        // creates the temp directory to store ajsc.war - incorrect.  this is setting up the temp dir to explode ajsc-war
        Path p = Files.createTempDirectory(basedir, "ajsc-temp.dir");
        // setting the ajsc-temp-dir system property to point to ajsc.war
        // still need to set the ajsc-temp-dir, i believe to account for multiple instances.  this also sets the dir
        // underneath jetty/webapps which is what we still want
        System.setProperty("AJSC_TEMP_DIR", p.toString());
        System.out.println("AJSC_TEMP_DIR has been set to: " + System.getProperty("AJSC_TEMP_DIR"));
        // adding a shutdownHook to delete the temp dir created by each ajsc instance
        Runtime.getRuntime().addShutdownHook(new Thread() {

            @Override
            public void run() {
                File dir = new File(System.getProperty("AJSC_TEMP_DIR"));
                try {
                    FileUtils.deleteDirectory(dir);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        File ajscWarDir = new File(System.getProperty("AJSC_HOME") + File.separator + "lib");
        if (!ajscWarDir.exists()) {
            ajscWarDir.mkdirs();
        }
        // Setting URL Pattern for CamelServlet to register properly with
        // dme2
        setURLPatterns();
        // Retrieving the ajsc Context Path from program args and setting
        // the AJSC_CONTEXT_PATH to be used by ajsc-jetty.xml
        String ajscWarPath = getAjscWarCtxPath(args);
        System.setProperty("AJSC_CONTEXT_PATH", ajscWarPath);
        System.out.println("AJSC_CONTEXT_PATH has been set to: " + System.getProperty("AJSC_CONTEXT_PATH"));
        // I am also setting APP_CONTEXT_PATH here as well as
        // AJSC_CONTEXT_PATH as I believe the program later uses
        // APP_CONTEXT_PATH for some other logic. I would like to convert
        // that to AJSC_CONTEXT_PATH when I get a chance
        System.setProperty("APP_CONTEXT_PATH", ajscWarPath);
        // Retrieving the location of the ajsc war and setting this path to
        // be used by ajsc-jetty.xml
        String runTheMethod = getAjscWarWarPath(args).toString();
        String ajscWarWarPath = System.getProperty("AJSC_HOME") + File.separator + "lib" + File.separator + "ajsc.war";
        // Changing above code to simply set the folder path to /ajscWar for the ajsc.war location
        // String ajscWarWarPath = System.getProperty("AJSC_HOME").toString() + "/ajscWar";
        System.setProperty("AJSC_WAR_PATH", ajscWarWarPath);
        System.out.println("AJSC_WAR_PATH has been set to: " + System.getProperty("AJSC_WAR_PATH"));
        // Searching for a port to listen to and register with dme2 as an
        // ephemeral port
        int listenerPort = getListenerPort(args);
        if (listenerPort == 0) {
            ServerSocket s = new ServerSocket(0);
            listenerPort = s.getLocalPort();
            s.close();
        }
        System.setProperty("AJSC_HTTP_PORT", Integer.toString(listenerPort));
        System.out.println("AJSC_HTTP_PORT has been set to: " + System.getProperty("AJSC_HTTP_PORT"));
        System.setProperty("server.port", Integer.toString(listenerPort));
        // Setting up https port for ssl. Port will be passed to
        // ajsc-jetty.xml where actual configuration will take place
        String enableSSL = System.getProperty("enableSSL");
        if (enableSSL != null && enableSSL.equals("true")) {
            int httpsListenerPort = getHttpsListenerPort(args);
            if (httpsListenerPort == 0) {
                ServerSocket s = new ServerSocket(0);
                httpsListenerPort = s.getLocalPort();
                s.close();
            }
            System.setProperty("AJSC_HTTPS_PORT", Integer.toString(httpsListenerPort));
            System.out.println("AJSC_HTTPS_PORT has been set to: " + System.getProperty("AJSC_HTTPS_PORT"));
        }
        // Setting up the jetty Server by reading the ajsc-jetty.xml
        // configuration file
        XmlConfiguration ajscJettyConfigXml = new XmlConfiguration(getAjscJettyConfigXml(AJSC_JETTY_CONFIG_XML_LOC));
        int blockingQueueSize = 10;
        int corePoolSize = 100;
        int maxPoolSize = 100;
        int keepAliveTime = 3000;
        if (System.getProperty("AJSC_JETTY_BLOCKING_QUEUE_SIZE") != null) {
            blockingQueueSize = Integer.parseInt(System.getProperty("AJSC_JETTY_BLOCKING_QUEUE_SIZE"));
        }
        if (System.getProperty("AJSC_JETTY_ThreadCount_MIN") != null) {
            corePoolSize = Integer.parseInt(System.getProperty("AJSC_JETTY_ThreadCount_MIN"));
        }
        if (System.getProperty("AJSC_JETTY_ThreadCount_MAX") != null) {
            maxPoolSize = Integer.parseInt(System.getProperty("AJSC_JETTY_ThreadCount_MAX"));
        }
        if (System.getProperty("AJSC_JETTY_IDLETIME_MAX") != null) {
            keepAliveTime = Integer.parseInt(System.getProperty("AJSC_JETTY_IDLETIME_MAX"));
        }
        ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(blockingQueueSize);
        QueuedThreadPool pool = new QueuedThreadPool(maxPoolSize, corePoolSize, keepAliveTime, queue);
        // ExecutorThreadPool pool = new ExecutorThreadPool(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.SECONDS, queue);
        Server server = new Server(pool);
        ajscJettyConfigXml.configure(server);
        try {
            server.start();
            server.join();
        } finally {
            if (server.isFailed()) {
                try {
                    server.stop();
                    System.exit(0);
                } catch (Exception stopJettyForException) {
                    server.destroy();
                }
            }
        }
    // server.start();
    // server.join();
    } catch (Exception ex) {
        System.out.println("ERROR:" + ex.getMessage());
    }
}
Also used : Path(java.nio.file.Path) Server(org.eclipse.jetty.server.Server) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) Properties(java.util.Properties) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) File(java.io.File)

Aggregations

ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)440 Test (org.junit.Test)158 ArrayList (java.util.ArrayList)75 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)74 IOException (java.io.IOException)66 CountDownLatch (java.util.concurrent.CountDownLatch)58 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)41 BlockingQueue (java.util.concurrent.BlockingQueue)34 ExecutorService (java.util.concurrent.ExecutorService)34 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)31 List (java.util.List)29 LocalAddress (io.netty.channel.local.LocalAddress)27 HashMap (java.util.HashMap)25 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)25 Subscription (rx.Subscription)25 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)24 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)23 File (java.io.File)22 CompletableFuture (java.util.concurrent.CompletableFuture)22 LinkedList (java.util.LinkedList)21