List of usage examples for java.util.concurrent LinkedBlockingQueue LinkedBlockingQueue
public LinkedBlockingQueue()
From source file:com.projectsexception.myapplist.iconloader.IconManager.java
/** * Constructs the work queues and thread pools used to download and decode images. *//*from w w w . j a va 2 s .co m*/ private IconManager() { /* * Creates a work queue for the pool of Thread objects used for downloading, using a linked * list queue that blocks when the queue is empty. */ mDownloadWorkQueue = new LinkedBlockingQueue<Runnable>(); /* * Creates a work queue for the set of of task objects that control downloading and * decoding, using a linked list queue that blocks when the queue is empty. */ mIconTaskWorkQueue = new LinkedBlockingQueue<IconTask>(); /* * Creates a new pool of Thread objects for the download work queue */ mDownloadThreadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT, mDownloadWorkQueue); // Instantiates a new cache based on the cache size estimate mIconCache = new LruCache<String, Drawable>(IMAGE_CACHE_SIZE); /* * Instantiates a new anonymous Handler object and defines its * handleMessage() method. The Handler *must* run on the UI thread, because it moves * Drawables from the IconTask object to the View object. * To force the Handler to run on the UI thread, it's defined as part of the IconManager * constructor. The constructor is invoked when the class is first referenced, and that * happens when the View invokes startDownload. Since the View runs on the UI Thread, so * does the constructor and the Handler. */ mHandler = new Handler(Looper.getMainLooper()) { /* * handleMessage() defines the operations to perform when the * Handler receives a new Message to process. */ @Override public void handleMessage(Message inputMessage) { // Gets the image task from the incoming Message object. IconTask iconTask = (IconTask) inputMessage.obj; // Sets an IconView that's a weak reference to the // input ImageView IconView localView = iconTask.getIconView(); // If this input view isn't null if (localView != null) { /* * Gets the package name of the *weak reference* to the input * ImageView. The weak reference won't have changed, even if * the input ImageView has. */ String packageName = localView.getPackageName(); /* * Compares the URL of the input ImageView to the URL of the * weak reference. Only updates the drawable in the ImageView * if this particular Thread is supposed to be serving the * ImageView. */ if (iconTask.getPackageName() != null && iconTask.getPackageName().equals(packageName)) { /* * Chooses the action to take, based on the incoming message */ switch (inputMessage.what) { // If the download has started, sets background color to dark green case LOAD_STARTED: localView.setStatusResource(R.drawable.ic_default_launcher); break; /* * The decoding is done, so this sets the * ImageView's bitmap to the bitmap in the * incoming message */ case TASK_COMPLETE: localView.setImageDrawable(iconTask.getDrawable()); recycleTask(iconTask); break; // The download failed, sets the background color to dark red case LOAD_FAILED: localView.setStatusResource(R.drawable.ic_default_launcher); // Attempts to re-use the Task object recycleTask(iconTask); break; default: // Otherwise, calls the super method super.handleMessage(inputMessage); } } } } }; }
From source file:info.raack.appliancedetection.common.email.SMTPEmailSender.java
public SMTPEmailSender() { emailProperties = new Properties(); emailProperties.put("mail.smtp.host", "smtp.gmail.com"); emailProperties.put("mail.smtp.socketFactory.port", "465"); emailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); emailProperties.put("mail.smtp.auth", "true"); emailProperties.put("mail.smtp.port", "465"); emailQueue = new LinkedBlockingQueue<Message>(); emailThread = new StoppableThread(new StoppableThreadAction() { public void doAction() throws InterruptedException { sendGeneralEmail(emailQueue.take()); }/* w ww . j av a 2 s . c o m*/ private void sendGeneralEmail(Message message) { try { logger.debug("Sending email to " + message.getRecipients(Message.RecipientType.TO)[0]); Transport.send(message); } catch (Exception e) { throw new RuntimeException("Could not send email", e); } } public String getName() { return "email sender"; } }); emailThread.start(); }
From source file:com.smartmarmot.common.db.DBJob.java
@Override public void run() { SmartLogger.logThis(Level.DEBUG, "Starting dbJob on database " + _dbname + " " + _queriesGroup); final long start = System.currentTimeMillis(); try {/*from w w w . j a v a2 s . co m*/ Connection dbConn = this._spds.getConnection(); /* if (dbConn.isClosed()){ dbConn = this._spds.getConnection(); }*/ if (Alive(dbConn)) { _queue.offer(new ZabbixItem("alive", "1", this._dbname)); _queue.offer( new ZabbixItem(Constants.PROJECT_NAME + "." + "Version", Constants.BANNER, this._dbname)); ZabbixItem[] zitems = DBEnquiry.execute(this._queries, dbConn, this._dbname); if (zitems != null && zitems.length > 0) { SmartLogger.logThis(Level.DEBUG, "Item retrieved " + zitems.length + " on database " + this._dbname); for (int cnt = 0; cnt < zitems.length; cnt++) { String zItemName = zitems[cnt].getKey(); if (this._dgNum > 0) { zItemName = zItemName + "_" + _dgNum; } SmartLogger.logThis(Level.DEBUG, "dbname " + this._dbname + " sending item " + zitems[cnt].getKey() + " value " + zitems[cnt].getValue()); _queue.offer(new ZabbixItem(zItemName, zitems[cnt].getValue(), _dbname)); } } dbConn.close(); Sender sender = new Sender(_queue, _zabbixServers, this._dbname); sender.run(); } else { BlockingQueue<ZabbixItem> _queue = new LinkedBlockingQueue<ZabbixItem>(); _queue.offer(new ZabbixItem("alive", "0", this._dbname)); _queue.offer( new ZabbixItem(Constants.PROJECT_NAME + "." + "Version", Constants.BANNER, this._dbname)); for (int cnt = 0; cnt < this._queries.length; cnt++) { _queue.offer(new ZabbixItem(_queries[cnt].getName(), _queries[cnt].getNoData(), _dbname)); } Sender sender = new Sender(_queue, _zabbixServers, _dbname); sender.run(); } } catch (Exception e) { SmartLogger.logThis(Level.ERROR, "Error on dbJob for database " + _dbname + " " + _queriesGroup + " error: " + e); } finally { if (_queries != null) _queries = null; } SmartLogger.logThis(Level.INFO, "Done with dbJob on database " + _dbname + " " + _queriesGroup + " elapsed time " + (System.currentTimeMillis() - start) + " ms"); }
From source file:org.ulyssis.ipp.processor.Processor.java
public Processor(final ProcessorOptions options) { Database.setDatabaseURI(options.getDatabaseUri()); try (Connection connection = Database.createConnection(EnumSet.of(READ_WRITE))) { if (options.shouldClearDb()) { Database.clearDb(connection); }// w w w. ja v a 2 s .c o m Database.initDb(connection); connection.commit(); } catch (SQLException e) { LOG.fatal("Error initializing database!", e); } URI uri = options.getRedisUri(); this.eventQueue = new LinkedBlockingQueue<>(); this.eventCallbacks = new ConcurrentHashMap<>(); this.onStartedCallbacks = new CopyOnWriteArrayList<>(); this.readerListeners = new ArrayList<>(); this.threads = new ArrayList<>(); // TODO: Move status reporting and processing of commands to ZeroMQ? // Also: post some stuff to a log in the db? this.statusReporter = new StatusReporter(uri, Config.getCurrentConfig().getStatusChannel()); this.commandProcessor = new CommandProcessor(uri, Config.getCurrentConfig().getControlChannel(), statusReporter); initCommandProcessor(); snapshot = new Snapshot(Instant.EPOCH); if (!restoreFromDb()) { registerInitialTags(); } }
From source file:com.linkedin.drelephant.ElephantRunner.java
@Override public void run() { logger.info("Dr.elephant has started"); try {/* w w w. j ava 2 s. co m*/ _hadoopSecurity = new HadoopSecurity(); _hadoopSecurity.doAs(new PrivilegedAction<Void>() { @Override public Void run() { HDFSContext.load(); loadGeneralConfiguration(); loadAnalyticJobGenerator(); ElephantContext.init(); // Initialize the metrics registries. MetricsController.init(); logger.info("executor num is " + _executorNum); if (_executorNum < 1) { throw new RuntimeException("Must have at least 1 worker thread."); } ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("dr-el-executor-thread-%d") .build(); _threadPoolExecutor = new ThreadPoolExecutor(_executorNum, _executorNum, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), factory); while (_running.get() && !Thread.currentThread().isInterrupted()) { _analyticJobGenerator.updateResourceManagerAddresses(); lastRun = System.currentTimeMillis(); logger.info("Fetching analytic job list..."); try { _hadoopSecurity.checkLogin(); } catch (IOException e) { logger.info("Error with hadoop kerberos login", e); //Wait for a while before retry waitInterval(_retryInterval); continue; } List<AnalyticJob> todos; try { todos = _analyticJobGenerator.fetchAnalyticJobs(); } catch (Exception e) { logger.error("Error fetching job list. Try again later...", e); //Wait for a while before retry waitInterval(_retryInterval); continue; } for (AnalyticJob analyticJob : todos) { _threadPoolExecutor.submit(new ExecutorJob(analyticJob)); } int queueSize = _threadPoolExecutor.getQueue().size(); MetricsController.setQueueSize(queueSize); logger.info("Job queue size is " + queueSize); //Wait for a while before next fetch waitInterval(_fetchInterval); } logger.info("Main thread is terminated."); return null; } }); } catch (Exception e) { logger.error(e.getMessage()); logger.error(ExceptionUtils.getStackTrace(e)); } }
From source file:flexflux.analyses.randomConditions.RandomConditions.java
@Override public RandomConditionsResult runAnalysis() { if (inputs == null) { return null; }/*from w w w . j av a 2 s .co m*/ double startTime = System.currentTimeMillis(); RandomConditionsResult result = new RandomConditionsResult(inputs); Queue<Integer> tasks = new LinkedBlockingQueue<Integer>(); for (int i = 0; i < numberSimulations; i++) { tasks.add(i); } for (int i = 0; i < Vars.maxThread; i++) { ThreadRandomConditions thread = new ThreadRandomConditions(tasks, gaussianMean, gaussianStd, minInputs, maxInputs, inputRandomParameters, type, result); threads.add(thread); } if (Vars.verbose) { System.err.println("Progress : "); System.err.print("["); for (int i = 0; i < 50; i++) { System.err.print(" "); } System.err.print("]\n"); System.err.print("["); } for (ThreadRandomConditions thread : threads) { thread.start(); } for (ThreadRandomConditions thread : threads) { // permits to wait for the threads to end try { thread.join(); } catch (InterruptedException e) { // e.printStackTrace(); } } if (Vars.verbose) { System.err.print("]\n"); } // we remove the threads to permit another analysis while (threads.size() > 0) { threads.remove(0); } if (Vars.verbose) { System.err.println("Random conditions over " + ((System.currentTimeMillis() - startTime) / 1000) + "s " + Vars.maxThread + " threads"); } return result; }
From source file:com.momock.binder.container.ViewPagerBinder.java
public void onBind(final ViewPager view, final IDataList<?> dataSource) { if (view != null) { if (refIndicator != null && refIndicator.get() != null) { refIndicator.get().setCount(dataSource.getItemCount()); }/*from w w w .ja v a 2 s . c om*/ view.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int position) { position = adapter instanceof IRoundAdapter ? ((IRoundAdapter) adapter).getRealPosition(position) : position; Object item = dataSource.getItem(position); ItemEventArgs args = new ItemEventArgs(view, position, item); itemSelectedEvent.fireEvent(view, args); if (refIndicator != null && refIndicator.get() != null) { refIndicator.get().setCurrentIndex(position); } } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageScrollStateChanged(int state) { viewPagerState = state; } }); view.setOnTouchListener(new View.OnTouchListener() { GestureDetector detector = null; @Override public boolean onTouch(View v, MotionEvent event) { view.getParent().requestDisallowInterceptTouchEvent(true); if (detector == null) { detector = new GestureDetector(v.getContext(), new GestureDetector.SimpleOnGestureListener() { @Override public boolean onSingleTapConfirmed(MotionEvent e) { Object item = dataSource.getItem(adapter instanceof IRoundAdapter ? ((IRoundAdapter) adapter).getRealPosition(view.getCurrentItem()) : view.getCurrentItem()); ItemEventArgs args = new ItemEventArgs(view, view.getCurrentItem(), item); itemClickedEvent.fireEvent(view, args); return true; } }); } return detector.onTouchEvent(event); } }); adapter = new PagerAdapter() { BlockingQueue<View> savedViews = new LinkedBlockingQueue<View>(); @Override public int getCount() { return dataSource.getItemCount(); } @Override public boolean isViewFromObject(View view, Object object) { return view == ((View) object); } @Override public Object instantiateItem(ViewGroup container, int position) { View convertView = null; if (savedViews.size() > 0) { convertView = savedViews.poll(); } Object item = dataSource.getItem(position); if (convertView != null) convertView.setTag(null); convertView = itemBinder.onCreateItemView(convertView, item, ViewPagerBinder.this); convertView.setTag(item); container.addView(convertView, 0); return convertView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { savedViews.add((View) object); container.removeView((View) object); } long lastDataSetChangedTick = 0; @Override public void notifyDataSetChanged() { lastDataSetChangedTick = System.nanoTime(); getDataChangedEvent().fireEvent(this, new EventArgs()); if (refIndicator != null && refIndicator.get() != null) { refIndicator.get().setCount(dataSource.getItemCount()); } super.notifyDataSetChanged(); Logger.debug("ViewPagerBinder.PagerAdapter.notifyDataSetChanged"); } long updateTick = 0; @Override public void startUpdate(ViewGroup container) { updateTick = System.nanoTime(); super.startUpdate(container); } @Override public void setPrimaryItem(ViewGroup container, int position, Object object) { super.setPrimaryItem(container, position, object); } @Override public void finishUpdate(ViewGroup container) { super.finishUpdate(container); if (lastDataSetChangedTick < updateTick) lastDataSetChangedTick = 0; } @Override public int getItemPosition(Object object) { return lastDataSetChangedTick == 0 ? PagerAdapter.POSITION_UNCHANGED : PagerAdapter.POSITION_NONE; } }; if (round) adapter = new RoundPagerAdapter(adapter); view.setAdapter(adapter); dataSource.addDataChangedHandler(new IEventHandler<DataChangedEventArgs>() { @Override public void process(Object sender, DataChangedEventArgs args) { adapter.notifyDataSetChanged(); } }); if (round) view.setCurrentItem(Math.max(100, dataSource.getItemCount() * 100), false); } }
From source file:au.org.ala.layers.dao.LayerIntersectDAOImpl.java
void init() { if (intersectConfig == null) { synchronized (initLock) { if (intersectConfig != null) { return; }/*w ww . j ava 2 s . c o m*/ intersectConfig = new IntersectConfig(fieldDao, layerDao); gridReaders = new LinkedBlockingQueue<GridCacheReader>(); for (int i = 0; i < intersectConfig.getGridCacheReaderCount(); i++) { GridCacheReader gcr = fixGridCacheReaderNames( new GridCacheReader(intersectConfig.getGridCachePath())); try { gridReaders.put(gcr); } catch (InterruptedException ex) { logger.error("failed to add a GridCacheReader"); } gridGroupCount = gcr.getGroupCount(); } if (gridReaders.size() == 0) { gridReaders = null; } } } }
From source file:com.mattring.nifi.nats.bundle.GetNats.java
public GetNats() { this.natsLock = new Object(); // TODO: queue capacity should be a property this.inbox = new LinkedBlockingQueue<>(); this.msgHandler = new MsgHandler() { @Override//from w w w . j a v a 2 s .c o m public void execute(String msg, String reply, String subject) { final boolean enqueued = inbox.offer(msg); // TODO: if ! enqueued then do what? } }; }
From source file:ch.entwine.weblounge.maven.S3DeployMojo.java
/** * /* w ww . ja v a 2s. c o m*/ * {@inheritDoc} * * @see org.apache.maven.plugin.Mojo#execute() */ public void execute() throws MojoExecutionException, MojoFailureException { // Setup AWS S3 client AWSCredentials credentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey); AmazonS3Client uploadClient = new AmazonS3Client(credentials); TransferManager transfers = new TransferManager(credentials); // Make sure key prefix does not start with a slash but has one at the // end if (keyPrefix.startsWith("/")) keyPrefix = keyPrefix.substring(1); if (!keyPrefix.endsWith("/")) keyPrefix = keyPrefix + "/"; // Keep track of how much data has been transferred long totalBytesTransferred = 0L; int items = 0; Queue<Upload> uploads = new LinkedBlockingQueue<Upload>(); try { // Check if S3 bucket exists getLog().debug("Checking whether bucket " + bucket + " exists"); if (!uploadClient.doesBucketExist(bucket)) { getLog().error("Desired bucket '" + bucket + "' does not exist!"); return; } getLog().debug("Collecting files to transfer from " + resources.getDirectory()); List<File> res = getResources(); for (File file : res) { // Make path of resource relative to resources directory String filename = file.getName(); String extension = FilenameUtils.getExtension(filename); String path = file.getPath().substring(resources.getDirectory().length()); String key = concat("/", keyPrefix, path).substring(1); // Delete old file version in bucket getLog().debug("Removing existing object at " + key); uploadClient.deleteObject(bucket, key); // Setup meta data ObjectMetadata meta = new ObjectMetadata(); meta.setCacheControl("public, max-age=" + String.valueOf(valid * 3600)); FileInputStream fis = null; GZIPOutputStream gzipos = null; final File fileToUpload; if (gzip && ("js".equals(extension) || "css".equals(extension))) { try { fis = new FileInputStream(file); File gzFile = File.createTempFile(file.getName(), null); gzipos = new GZIPOutputStream(new FileOutputStream(gzFile)); IOUtils.copy(fis, gzipos); fileToUpload = gzFile; meta.setContentEncoding("gzip"); if ("js".equals(extension)) meta.setContentType("text/javascript"); if ("css".equals(extension)) meta.setContentType("text/css"); } catch (FileNotFoundException e) { getLog().error(e); continue; } catch (IOException e) { getLog().error(e); continue; } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(gzipos); } } else { fileToUpload = file; } // Do a random check for existing errors before starting the next upload if (erroneousUpload != null) break; // Create put object request long bytesToTransfer = fileToUpload.length(); totalBytesTransferred += bytesToTransfer; PutObjectRequest request = new PutObjectRequest(bucket, key, fileToUpload); request.setProgressListener(new UploadListener(credentials, bucket, key, bytesToTransfer)); request.setMetadata(meta); // Schedule put object request getLog().info( "Uploading " + key + " (" + FileUtils.byteCountToDisplaySize((int) bytesToTransfer) + ")"); Upload upload = transfers.upload(request); uploads.add(upload); items++; } } catch (AmazonServiceException e) { getLog().error("Uploading resources failed: " + e.getMessage()); } catch (AmazonClientException e) { getLog().error("Uploading resources failed: " + e.getMessage()); } // Wait for uploads to be finished String currentUpload = null; try { Thread.sleep(1000); getLog().info("Waiting for " + uploads.size() + " uploads to finish..."); while (!uploads.isEmpty()) { Upload upload = uploads.poll(); currentUpload = upload.getDescription().substring("Uploading to ".length()); if (TransferState.InProgress.equals(upload.getState())) getLog().debug("Waiting for upload " + currentUpload + " to finish"); upload.waitForUploadResult(); } } catch (AmazonServiceException e) { throw new MojoExecutionException("Error while uploading " + currentUpload); } catch (AmazonClientException e) { throw new MojoExecutionException("Error while uploading " + currentUpload); } catch (InterruptedException e) { getLog().debug("Interrupted while waiting for upload to finish"); } // Check for errors that happened outside of the actual uploading if (erroneousUpload != null) { throw new MojoExecutionException("Error while uploading " + erroneousUpload); } getLog().info("Deployed " + items + " files (" + FileUtils.byteCountToDisplaySize((int) totalBytesTransferred) + ") to s3://" + bucket); }