Example usage for java.util.concurrent Executors newSingleThreadExecutor

List of usage examples for java.util.concurrent Executors newSingleThreadExecutor

Introduction

In this page you can find the example usage for java.util.concurrent Executors newSingleThreadExecutor.

Prototype

public static ExecutorService newSingleThreadExecutor() 

Source Link

Document

Creates an Executor that uses a single worker thread operating off an unbounded queue.

Usage

From source file:br.com.caelum.vraptor.ioc.spring.SpringProviderRegisteringComponentsTest.java

@Override
protected <T> T executeInsideRequest(final WhatToDo<T> execution) {
    Callable<T> task = new Callable<T>() {
        public T call() throws Exception {
            T result = null;//www .ja va2  s . c  om
            HttpSessionMock session = new HttpSessionMock(context, "session" + ++counter);
            HttpServletRequestMock httpRequest = new HttpServletRequestMock(session,
                    mock(MutableRequest.class, "request" + counter));
            MutableResponse response = mock(MutableResponse.class, "response" + counter);

            RequestInfo request = new RequestInfo(context, null, httpRequest, response);
            VRaptorRequestHolder.setRequestForCurrentThread(request);

            RequestContextListener contextListener = new RequestContextListener();
            contextListener.requestInitialized(new ServletRequestEvent(context, httpRequest));
            result = execution.execute(request, counter);
            contextListener.requestDestroyed(new ServletRequestEvent(context, httpRequest));

            VRaptorRequestHolder.resetRequestForCurrentThread();
            return result;
        }
    };

    Future<T> future = Executors.newSingleThreadExecutor().submit(task);

    try {
        return future.get(60, TimeUnit.SECONDS);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.github.c77.base_driver.HuskyBaseDevice.java

public HuskyBaseDevice(UsbSerialDriver driver) {
    serialDriver = driver;//from   w  w  w . j a v a  2 s .  c o  m
    try {
        serialDriver.open();
        serialDriver.setParameters(115200, UsbSerialDriver.DATABITS_8, UsbSerialDriver.STOPBITS_1,
                UsbSerialDriver.PARITY_NONE);
    } catch (IOException e) {
        log.info("Error setting up device: " + e.getMessage(), e);
        e.printStackTrace();
        try {
            serialDriver.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        serialDriver = null;
    }

    final ExecutorService executorService = Executors.newSingleThreadExecutor();

    SerialInputOutputManager serialInputOutputManager;

    final SerialInputOutputManager.Listener listener = new SerialInputOutputManager.Listener() {
        @Override
        public void onRunError(Exception e) {
        }

        @Override
        public void onNewData(final byte[] data) {
        }
    };

    serialInputOutputManager = new SerialInputOutputManager(serialDriver, listener);
    executorService.submit(serialInputOutputManager);
}

From source file:com.github.c77.base_driver.KobukiBaseDevice.java

public KobukiBaseDevice(UsbSerialDriver driver) {
    serialDriver = driver;//w w w . j  a  v a  2  s .  co  m
    try {
        serialDriver.open();
        serialDriver.setParameters(115200, UsbSerialDriver.DATABITS_8, UsbSerialDriver.STOPBITS_1,
                UsbSerialDriver.PARITY_NONE);
    } catch (IOException e) {
        log.info("Error setting up device: " + e.getMessage(), e);
        e.printStackTrace();
        try {
            serialDriver.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        serialDriver = null;
    }

    final ExecutorService executorService = Executors.newSingleThreadExecutor();

    SerialInputOutputManager serialInputOutputManager;

    final SerialInputOutputManager.Listener listener = new SerialInputOutputManager.Listener() {
        @Override
        public void onRunError(Exception e) {
        }

        @Override
        public void onNewData(final byte[] data) {
            KobukiBaseDevice.this.updateReceivedData(data);
        }
    };

    serialInputOutputManager = new SerialInputOutputManager(serialDriver, listener);
    executorService.submit(serialInputOutputManager);
}

From source file:com.jillesvangurp.httpclientfuture.HttpClientWithFutureTest.java

@BeforeClass
public void beforeClass() {
    webServerExecutor = Executors.newSingleThreadExecutor();
    webServer = new WebServer();
    webServerExecutor.execute(webServer);
    clientThreadPool = Executors.newFixedThreadPool(threads);
    PoolingClientConnectionManager conman = new PoolingClientConnectionManager();
    conman.setDefaultMaxPerRoute(threads);
    conman.setMaxTotal(threads);/*from www  . java  2  s .co m*/
    DefaultHttpClient httpclient = new DefaultHttpClient(conman);
    client = new HttpClientWithFuture<Boolean>(httpclient, clientThreadPool, new ResponseHandler<Boolean>() {
        @Override
        public Boolean handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            return response.getStatusLine().getStatusCode() == 200;
        }
    });
}

From source file:io.syndesis.controllers.integration.IntegrationController.java

@PostConstruct
public void start() {
    executor = Executors.newSingleThreadExecutor();
    scheduler = Executors.newScheduledThreadPool(1);
    scanIntegrationsForWork();/*from www.j av a  2s. c  om*/

    eventBus.subscribe("integration-controller", getChangeEventSubscription());
}

From source file:com.android.talkback.labeling.CustomLabelMigrationManager.java

public CustomLabelMigrationManager(Context context) {
    mExecutor = Executors.newSingleThreadExecutor();
    mHandler = new Handler();
    TalkBackService service = TalkBackService.getInstance();
    if (service != null) {
        mManager = service.getLabelManager();
    } else {/*from ww w. j a  v  a2  s  .  c  o m*/
        mManager = new CustomLabelManager(context);
    }
    mContext = context;
}

From source file:biz.fstechnology.micro.common.jms.JmsServiceConnection.java

/**
 * @see biz.fstechnology.micro.common.DefaultServiceConnection#callAsync(biz.fstechnology.micro.common.Request,
 * java.util.function.Consumer)/*from   w  ww  . j  a va 2 s  . c om*/
 */
@Override
public <T, U> void callAsync(Request<T> request, Consumer<Result<U>> callback) {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<Result<U>> rawResponse = executor.submit(() -> call(request));
    executor.submit(() -> {
        try {
            callback.accept(rawResponse.get());
        } catch (Exception e) {
            e.printStackTrace();
            Result<U> result = new Result<>(e);
            callback.accept(result);
        }
    });
    executor.shutdown();
}

From source file:mina.UnsyncClientSupport.java

/**
 * FTS?// ww  w . j a  v  a2s .  c o  m
 * 
 * @return
 * @throws CommunicationException
 * @throws InterruptedException
 */
public IoSession connect() throws InterruptedException {
    timer = new Timer();
    if (null != this.session && !session.isConnected()) {
        //??
        return this.session;
    }
    if (connector != null) {
        connector.connect();
        return this.session;
    }
    connector = new NioSocketConnector();
    connector.setConnectTimeoutMillis(CONNECT_TIMEOUT);
    connector.getFilterChain().addLast("threadPool", new ExecutorFilter(Executors.newSingleThreadExecutor()));
    connector.getFilterChain().addLast("logger", new LoggingFilter());
    connector.setHandler(new ClientMsgReceiveHandle());// ???
    try {
        connector.setDefaultRemoteAddress(
                new InetSocketAddress(EPSConstant.UNSYNC_CLIENT_IP, EPSConstant.UNSYNC_CLIENT_PORT));
        ConnectFuture future = connector.connect(new InetSocketAddress(this.hostname, this.port));
        future.awaitUninterruptibly();
        IoSession session = future.getSession();
        this.setSession(session);
        //???????
        timer = new Timer();
        timer.schedule(new HoldConnectionTask(), 0, 60000);
    } catch (RuntimeIoException e) {
        e.printStackTrace();
    }
    return session;
}

From source file:com.blackducksoftware.integration.jira.task.issue.event.IssueEventListener.java

public ExecutorService createExecutorService() {
    return Executors.newSingleThreadExecutor();
}

From source file:com.appdynamics.extensions.collectd.CollectdMonitor.java

public TaskOutput execute(Map<String, String> args, TaskExecutionContext taskExecutionContext)
        throws TaskExecutionException {

    LOGGER.info("Starting Collectd Monitoring Task");

    configure(args);/*from w  ww.j  a  va  2 s  .  c  om*/

    if (executor == null) {
        executor = Executors.newSingleThreadExecutor();
        executor.submit(new MachineAgentUdpReceiver(metricData));
    }

    LOGGER.info(String.format("Publishing %d metrics", metricData.size()));

    for (Map.Entry<String, Long> metric : metricData.entrySet()) {
        String key = metricPrefix + metric.getKey().replace('/', '|');
        getMetricWriter(key).printMetric(Long.toString(metric.getValue()));
    }

    if (sendOnlyChanges) {
        LOGGER.info("Clearing metric cache");
        metricData.clear();
    }

    return new TaskOutput("Success");
}