List of usage examples for java.util.concurrent LinkedBlockingQueue LinkedBlockingQueue
public LinkedBlockingQueue()
From source file:com.cloud.agent.Agent.java
public Agent(final IAgentShell shell, final int localAgentId, final ServerResource resource) throws ConfigurationException { _shell = shell;/*w w w . j av a2 s . c om*/ _resource = resource; _link = null; resource.setAgentControl(this); final String value = _shell.getPersistentProperty(getResourceName(), "id"); _id = value != null ? Long.parseLong(value) : null; s_logger.info("id is " + (_id != null ? _id : "")); final Map<String, Object> params = PropertiesUtil.toMap(_shell.getProperties()); // merge with properties from command line to let resource access command line parameters for (final Map.Entry<String, Object> cmdLineProp : _shell.getCmdLineProperties().entrySet()) { params.put(cmdLineProp.getKey(), cmdLineProp.getValue()); } if (!_resource.configure(getResourceName(), params)) { throw new ConfigurationException("Unable to configure " + _resource.getName()); } final String host = _shell.getHost(); _connection = new NioClient("Agent", host, _shell.getPort(), _shell.getWorkers(), this); // ((NioClient)_connection).setBindAddress(_shell.getPrivateIp()); s_logger.debug("Adding shutdown hook"); Runtime.getRuntime().addShutdownHook(new ShutdownThread(this)); _ugentTaskPool = new ThreadPoolExecutor(shell.getPingRetries(), 2 * shell.getPingRetries(), 10, TimeUnit.MINUTES, new SynchronousQueue<Runnable>(), new NamedThreadFactory("UgentTask")); _executor = new ThreadPoolExecutor(_shell.getWorkers(), 5 * _shell.getWorkers(), 1, TimeUnit.DAYS, new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory("agentRequest-Handler")); s_logger.info("Agent [id = " + (_id != null ? _id : "new") + " : type = " + getResourceName() + " : zone = " + _shell.getZone() + " : pod = " + _shell.getPod() + " : workers = " + _shell.getWorkers() + " : host = " + host + " : port = " + _shell.getPort()); }
From source file:com.wordnik.swaggersocket.server.SwaggerSocketProtocolInterceptor.java
@Override public Action inspect(final AtmosphereResource r) { final AtmosphereRequest request = r.getRequest(); r.addEventListener(new AtmosphereResourceEventListenerAdapter() { /**/*from ww w. j a v a 2s . c o m*/ * {@inheritDoc} */ @Override public void onSuspend(AtmosphereResourceEvent event) { AsyncIOWriter writer = event.getResource().getResponse().getAsyncIOWriter(); if (writer == null) { writer = new AtmosphereInterceptorWriter(); r.getResponse().asyncIOWriter(writer); } if (AtmosphereInterceptorWriter.class.isAssignableFrom(writer.getClass())) { AtmosphereInterceptorWriter.class.cast(writer).interceptor(interceptor); } } }); boolean ok = false; if (request.getHeader("SwaggerSocket") != null) { ok = true; } if (ok && request.attributes().get(SWAGGER_SOCKET_DISPATCHED) == null) { AtmosphereResponse response = new WrappedAtmosphereResponse(r.getResponse(), request); logger.debug("Method {} Transport {}", request.getMethod(), r.transport()); // Suspend to keep the connection OPEN. if (request.getMethod() == "GET" && r.transport().equals(AtmosphereResource.TRANSPORT.LONG_POLLING)) { r.resumeOnBroadcast(true).suspend(); BlockingQueue<AtmosphereResource> queue = (BlockingQueue<AtmosphereResource>) getContextValue( request, SUSPENDED_RESPONSE); if (queue == null) { queue = new LinkedBlockingQueue<AtmosphereResource>(); request.getSession().setAttribute(SUSPENDED_RESPONSE, queue); } queue.offer(r); String identity = (String) getContextValue(request, IDENTITY); schedule(r, identity); return Action.SUSPEND; } AtmosphereFramework framework = r.getAtmosphereConfig().framework(); StringBuilder d = new StringBuilder(); try { InputStreamReader isr = new InputStreamReader(request.getInputStream()); BufferedReader bufReader = new BufferedReader(isr); char[] charBuffer = new char[8192]; for (int readCount = bufReader.read(charBuffer); readCount > -1; readCount = bufReader .read(charBuffer)) { d.append(charBuffer, 0, readCount); } String data = d.toString(); if (data.length() == 0) { return Action.CANCELLED; } String message = data.substring(0, 20).replaceAll(" ", ""); logger.debug(data); if (message.startsWith("{\"handshake\"")) { // This will fail if the message is not well formed. HandshakeMessage handshakeMessage = mapper.readValue(data, HandshakeMessage.class); // If we missed the CloseReason for whatever reason (IE is a good candidate), make sure we swap the previous session anyway. String identity = (String) getContextValue(request, IDENTITY); if (identity == null) { identity = UUID.randomUUID().toString(); } else { logger.debug("Client disconnected {}, cleaning session {}", identity); try { Enumeration<String> e = request.getSession().getAttributeNames(); while (e.hasMoreElements()) { request.getSession().removeAttribute(e.nextElement()); } } catch (Exception ex) { logger.warn("", ex); } } addContextValue(request, IDENTITY, identity); StatusMessage statusMessage = new StatusMessage.Builder() .status(new StatusMessage.Status(200, "OK")).identity(identity).build(); response.setContentType("application/json"); response.getOutputStream().write(mapper.writeValueAsBytes(statusMessage)); if (r.transport() == AtmosphereResource.TRANSPORT.WEBSOCKET) { schedule(r, identity); } } else if (message.startsWith("{\"close\"")) { CloseMessage c = mapper.readValue(data, CloseMessage.class); logger.debug("Client disconnected {} with reason {}", c.getClose().getIdentity(), c.getClose().getReason()); try { request.getSession().invalidate(); } catch (Exception ex) { logger.warn("", ex); } return Action.CANCELLED; } else { Message swaggerSocketMessage = mapper.readValue(data, Message.class); swaggerSocketMessage.transactionID(UUID.randomUUID().toString()); String identity = (String) getContextValue(request, IDENTITY); if (!swaggerSocketMessage.getIdentity().equals(identity)) { StatusMessage statusMessage = new StatusMessage.Builder() .status(new StatusMessage.Status(503, "Not Allowed")) .identity(swaggerSocketMessage.getIdentity()).build(); response.getOutputStream().write(mapper.writeValueAsBytes(statusMessage)); return Action.CANCELLED; } transactionIdentity.set(swaggerSocketMessage.transactionID()); List<Request> requests = swaggerSocketMessage.getRequests(); addContextValue(request, swaggerSocketMessage.transactionID() + RESPONSE_COUNTER, new AtomicInteger(requests.size())); AtmosphereRequest ar; for (Request req : requests) { ar = toAtmosphereRequest(request, req); try { ar.attributes().put(SWAGGER_SOCKET_DISPATCHED, "true"); // This is a new request, we must clean the Websocket AtmosphereResource. request.removeAttribute(INJECTED_ATMOSPHERE_RESOURCE); response.request(ar); attachWriter(r); ssRequest.set(req); request.setAttribute("swaggerSocketRequest", req); Action action = framework.doCometSupport(ar, response); if (action.type() == Action.TYPE.SUSPEND) { ar.destroyable(false); response.destroyable(false); } } catch (Exception e) { logger.warn("", e); //REVISIT might want to optionally return the body entity? response.setStatus(500, "Server Error"); ResponseMessage responseMessage = new ResponseMessage(identity, createResponseBuilder(response, null).build()); response.getOutputStream().write(mapper.writeValueAsBytes(responseMessage)); } } } return Action.CANCELLED; } catch (IOException e) { logger.warn("", e); return Action.CONTINUE; } } else { if (!ok) { request.setAttribute(TrackMessageSizeInterceptor.SKIP_INTERCEPTOR, "true"); } } return Action.CONTINUE; }
From source file:com.nextgis.maplibui.service.TileDownloadService.java
private void download(DownloadTask task) { MapBase map = MapBase.getInstance(); if (null == map) return;//from ww w .j a v a 2 s. co m ILayer layer = map.getLayerByPathName(task.getLayerPathName()); if (null != layer && layer instanceof RemoteTMSLayer) { // process only tms layers final RemoteTMSLayer tmsLayer = (RemoteTMSLayer) layer; String notifyTitle = getString(R.string.download_tiles); mBuilder.setWhen(System.currentTimeMillis()).setContentTitle(notifyTitle); mNotifyManager.notify(TILE_DOWNLOAD_NOTIFICATION_ID, mBuilder.build()); final List<TileItem> tiles = new LinkedList<>(); int zoomCount = task.getZoomTo() + 1 - task.getZoomFrom(); for (int zoom = task.getZoomFrom(); zoom < task.getZoomTo() + 1; zoom++) { tiles.addAll(MapUtil.getTileItems(task.getEnvelope(), zoom, tmsLayer.getTMSType())); if (mCanceled) break; mBuilder.setProgress(zoomCount, zoom, false).setContentText(getString(R.string.form_tiles_list)); mNotifyManager.notify(TILE_DOWNLOAD_NOTIFICATION_ID, mBuilder.build()); if (tiles.size() > Constants.MAX_TILES_COUNT) break; } int threadCount = DRAWING_SEPARATE_THREADS; int coreCount = Runtime.getRuntime().availableProcessors(); // FIXME more than 1 pool size causing strange behaviour on 6.0 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) coreCount = 1; mThreadPool = new ThreadPoolExecutor(coreCount, threadCount, KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT, new LinkedBlockingQueue<Runnable>(), new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { try { executor.getQueue().put(r); } catch (InterruptedException e) { e.printStackTrace(); //throw new RuntimeException("Interrupted while submitting task", e); } } }); int tilesSize = tiles.size(); List<Future> futures = new ArrayList<>(tilesSize); for (int i = 0; i < tilesSize; ++i) { if (mCanceled) break; final TileItem tile = tiles.get(i); futures.add(mThreadPool.submit(new Runnable() { @Override public void run() { android.os.Process.setThreadPriority(Constants.DEFAULT_DRAW_THREAD_PRIORITY); tmsLayer.downloadTile(tile); } })); } //in separate thread // wait for download ending int nStep = futures.size() / Constants.DRAW_NOTIFY_STEP_PERCENT; if (nStep == 0) nStep = 1; for (int i = 0, futuresSize = futures.size(); i < futuresSize; i++) { if (mCanceled) break; try { Future future = futures.get(i); future.get(); // wait for task ending if (i % nStep == 0) { mBuilder.setProgress(futuresSize, i, false) .setContentText(getString(R.string.processing) + " " + tmsLayer.getName()); // Displays the progress bar for the first time. mNotifyManager.notify(TILE_DOWNLOAD_NOTIFICATION_ID, mBuilder.build()); } } catch (CancellationException | InterruptedException e) { //e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } } }
From source file:ca.sqlpower.architect.enterprise.ArchitectClientSideSession.java
/** * This constructor is only used for testing. This constructor allows users * to specify an executor to use as the foreground thread instead of using * the normal EDT. This is handy for ensuring all of the events occur on the * correct thread and updates do not conflict with persists. If the executor * is null then the foreground thread will just execute the runnables on the * current thread.//from w ww.j a v a2 s . c o m */ public ArchitectClientSideSession(ArchitectSessionContext context, String name, ProjectLocation projectLocation, boolean useThreadPool) throws SQLObjectException { super(context, name, new ArchitectSwingProject()); this.projectLocation = projectLocation; this.useThreadPool = useThreadPool; this.foregroundThreadExecutor = new ThreadPoolExecutor(1, 1, 5, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread newThread = new Thread(r); foregroundExecutorThread.add(newThread); return newThread; } }); foregroundThreadExecutor.allowCoreThreadTimeOut(false); dataSourceCollectionUpdater = new ArchitectDataSourceCollectionUpdater(projectLocation); this.isEnterpriseSession = true; setupSnapshots(); String ddlgClass = prefs.get(this.projectLocation.getUUID() + ".ddlg", null); if (ddlgClass != null) { try { DDLGenerator ddlg = (DDLGenerator) Class .forName(ddlgClass, true, ArchitectClientSideSession.class.getClassLoader()).newInstance(); setDDLGenerator(ddlg); ddlg.setTargetCatalog(prefs.get(this.projectLocation.getUUID() + ".targetCatalog", null)); ddlg.setTargetSchema(prefs.get(this.projectLocation.getUUID() + ".targetSchema", null)); } catch (Exception e) { createUserPrompter("Cannot load DDL settings due to missing class " + ddlgClass, UserPromptType.MESSAGE, UserPromptOptions.OK, UserPromptResponse.OK, null, "OK"); logger.error("Cannot find DDL Generator for class " + ddlgClass + ", ddl generator properties are not loaded."); } } outboundHttpClient = ClientSideSessionUtils.createHttpClient(projectLocation.getServiceInfo(), cookieStore); dataSourceCollection = getDataSources(); sessionPersister = new ArchitectSessionPersister("inbound-" + projectLocation.getUUID(), getWorkspace(), new ArchitectPersisterSuperConverter(dataSourceCollection, getWorkspace())); sessionPersister.setWorkspaceContainer(this); jsonMessageDecoder = new SPJSONMessageDecoder(sessionPersister); updater = new ArchitectNetworkConflictResolver(projectLocation, jsonMessageDecoder, ClientSideSessionUtils.createHttpClient(projectLocation.getServiceInfo(), cookieStore), outboundHttpClient, this); jsonPersister = new SPJSONPersister(updater); verifyServerLicense(projectLocation); }
From source file:gov.nasa.ensemble.resources.TestProjectProperties.java
@Test public void writing() throws CoreException, IOException, InterruptedException { final String key = "key", val1 = "val1", val2 = "val2"; final IFile propFile = ProjectProperties.propFile(file, key); final ProjectProperties props = projProps(file); assertTrue(props.get(key).isNone()); assertFalse(propFile.exists());/* w w w. java2 s . c o m*/ final BlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>(); ResourcesPlugin.getWorkspace().addResourceChangeListener(new IResourceChangeListener() { @Override public void resourceChanged(IResourceChangeEvent event) { if (event.getDelta() != null) { try { event.getDelta().accept(new IResourceDeltaVisitor() { @Override public boolean visit(IResourceDelta delta) { if (propFile.equals(delta.getResource())) try { queue.put(delta.getKind()); } catch (InterruptedException e) { LogUtil.error(e); } return true; } }); } catch (CoreException e) { throw new RuntimeException(e); } } } }); final long timeout = 1000; // create props.set(key, val1); assertEquals(ADDED, (int) queue.poll(timeout, MILLISECONDS)); assertTrue("Property file does not exist after setting property in memory", propFile.exists()); assertEquals(val1, getStringContents(propFile)); // update props.set(key, val2); assertEquals(CHANGED, (int) queue.poll(timeout, MILLISECONDS)); assertEquals("Property file does not exist after updating property in memory", val2, getStringContents(propFile)); // delete props.unset(key); assertEquals(REMOVED, (int) queue.poll(timeout, MILLISECONDS)); assertFalse("Property file still exists after unsetting property in memory", propFile.exists()); }
From source file:beans.BL.java
public void processData() { if (loadDataOffline) { loadOfflineDataInList();/*from w w w. ja v a2 s . co m*/ } if (reconnecting) { do { try { emf = Persistence.createEntityManagerFactory("GPSPU"); em = emf.createEntityManager(); reconnecting = false; } catch (PersistenceException ex) { System.out.println("PersistenceEx +-+-+-+-+-+-+-+-+-+"); System.out.println(ex.toString()); reconnecting = true; } } while (reconnecting); System.out.println("EMF started****************************"); } if (reconnectWithoutEMF) { do { try { System.out.println("reconnect"); em = emf.createEntityManager(); reconnectWithoutEMF = false; } catch (PersistenceException ex) { System.out.println("PersistenceEx +-+-+-+-+-+-+-+-+-+"); System.out.println(ex.toString()); LOGGER.log(Level.WARNING, "Reconect Exc", ex); reconnectWithoutEMF = true; } } while (reconnectWithoutEMF); System.out.println("EMF started****************************"); } try { BlockingQueue<Track> removeTracks = new LinkedBlockingQueue<>(); removeTracks = data_manager.uploadTracks(em, tracks); for (Track removeTrack : removeTracks) { tracks.remove(removeTrack); } BlockingQueue<Point> removePoints = new LinkedBlockingQueue<>(); removePoints = data_manager.uploadPoints(em, points); for (Point removePoint : removePoints) { points.remove(removePoint); } } catch (Exception ex) { reconnectWithoutEMF = true; System.out.println("Starting to reconnect.........."); System.out.println(ex.toString()); LOGGER.log(Level.WARNING, "Exc Exc", ex); } }
From source file:com.facebook.LinkBench.LinkBenchDriver.java
void load() throws IOException, InterruptedException, Throwable { if (!doLoad) { logger.info("Skipping load data per the cmdline arg"); return;/* ww w . j av a2 s . c o m*/ } // load data int nLinkLoaders = ConfigUtil.getInt(props, Config.NUM_LOADERS); boolean bulkLoad = true; BlockingQueue<LoadChunk> chunk_q = new LinkedBlockingQueue<LoadChunk>(); // max id1 to generate long maxid1 = ConfigUtil.getLong(props, Config.MAX_ID); // id1 at which to start long startid1 = ConfigUtil.getLong(props, Config.MIN_ID); // Create loaders logger.info("Starting loaders " + nLinkLoaders); logger.debug("Bulk Load setting: " + bulkLoad); Random masterRandom = createMasterRNG(props, Config.LOAD_RANDOM_SEED); boolean genNodes = ConfigUtil.getBool(props, Config.GENERATE_NODES); int nTotalLoaders = genNodes ? nLinkLoaders + 1 : nLinkLoaders; LatencyStats latencyStats = new LatencyStats(nTotalLoaders); List<Runnable> loaders = new ArrayList<Runnable>(nTotalLoaders); LoadProgress loadTracker = LoadProgress.create(logger, props); // First load nodes (changed by aapo) if (genNodes) { logger.info("Will generate graph nodes during loading"); int loaderId = nTotalLoaders - 1; NodeStore nodeStore = createNodeStore(null); Random rng = new Random(masterRandom.nextLong()); loaders.add(new NodeLoader(props, logger, nodeStore, rng, latencyStats, csvStreamFile, loaderId)); } enqueueLoadWork(chunk_q, startid1, maxid1, nLinkLoaders, new Random(masterRandom.nextLong())); // run loaders loadTracker.startTimer(); System.out.println("Loading nodes first..."); // Changed by Aky long loadTime = concurrentExec(loaders); loaders.clear(); // Links for (int i = 0; i < nLinkLoaders; i++) { LinkStore linkStore = createLinkStore(); bulkLoad = bulkLoad && linkStore.bulkLoadBatchSize() > 0; LinkBenchLoad l = new LinkBenchLoad(linkStore, props, latencyStats, csvStreamFile, i, maxid1 == startid1 + 1, chunk_q, loadTracker); loaders.add(l); } loadTime += concurrentExec(loaders); long expectedNodes = maxid1 - startid1; long actualLinks = 0; long actualNodes = 0; for (final Runnable l : loaders) { if (l instanceof LinkBenchLoad) { actualLinks += ((LinkBenchLoad) l).getLinksLoaded(); } else { assert (l instanceof NodeLoader); actualNodes += ((NodeLoader) l).getNodesLoaded(); } } latencyStats.displayLatencyStats(); if (csvStatsFile != null) { latencyStats.printCSVStats(csvStatsFile, true); } double loadTime_s = (loadTime / 1000.0); logger.info(String.format( "LOAD PHASE COMPLETED. " + " Loaded %d nodes (Expected %d)." + " Loaded %d links (%.2f links per node). " + " Took %.1f seconds. Links/second = %d", actualNodes, expectedNodes, actualLinks, actualLinks / (double) actualNodes, loadTime_s, (long) Math.round(actualLinks / loadTime_s))); }
From source file:net.tomp2p.simgrid.SimGridTomP2P.java
public static void addQueue(Number160 senderID, SendingMessage sendingMessage) { BlockingQueue<SendingMessage> queue = pendingMessages.get(senderID); if (queue == null) { queue = new LinkedBlockingQueue<SendingMessage>(); pendingMessages.put(senderID, queue); }/*from w w w. ja v a2 s . co m*/ queue.offer(sendingMessage); notify(senderID); }
From source file:org.apache.axis2.transport.http.server.HttpFactory.java
/** * Create the executor used to launch the single requestConnectionListener *//*w w w. j a va 2s . c o m*/ public ExecutorService newListenerExecutor(int port) { return new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), new DefaultThreadFactory(new ThreadGroup("Listener thread group"), "HttpListener-" + this.port)); }
From source file:com.barchart.http.server.TestHttpServer.java
@Test public void testTooManyConnections() throws Exception { final Queue<Integer> status = new LinkedBlockingQueue<Integer>(); final Runnable r = new Runnable() { @Override// ww w . ja va 2 s . c o m public void run() { try { final HttpResponse response = client .execute(new HttpGet("http://localhost:" + port + "/client-disconnect")); status.add(response.getStatusLine().getStatusCode()); } catch (final Exception e) { e.printStackTrace(); } } }; final Thread t1 = new Thread(r); t1.start(); final Thread t2 = new Thread(r); t2.start(); t1.join(); t2.join(); assertEquals(2, status.size()); assertTrue(status.contains(200)); assertTrue(status.contains(503)); }