List of usage examples for java.util.concurrent BlockingQueue iterator
Iterator<E> iterator();
From source file:Main.java
public static void main(String[] argv) throws Exception { int capacity = 10; BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity); for (int i = 0; i < 10; i++) { queue.add(i);/* w w w.j av a2s .c o m*/ } Iterator<Integer> it = queue.iterator(); while (it.hasNext()) { System.out.println(it.next()); } }
From source file:com.alibaba.wasp.fserver.SplitThread.java
public String dumpQueue() { StringBuffer queueLists = new StringBuffer(); queueLists.append("Split Queue dump:\n"); queueLists.append(" Split Queue:\n"); BlockingQueue<Runnable> lq = splits.getQueue(); Iterator<Runnable> it = lq.iterator(); while (it.hasNext()) { queueLists.append(" " + it.next().toString()); queueLists.append("\n"); }/* w ww .j a va2s . c o m*/ return queueLists.toString(); }
From source file:it.drwolf.ridire.session.async.JobMapperMonitor.java
@SuppressWarnings("unchecked") private void lookForNotMappedJob() { JobSizeComparator jobSizeComparator = new JobSizeComparator(); this.mappers.clear(); try {//from www . j av a 2s. co m this.mainUserTx = (UserTransaction) org.jboss.seam.Component .getInstance("org.jboss.seam.transaction.transaction"); this.mainUserTx.setTransactionTimeout(10 * 10 * 60); // set timeout // to // 10 mins do { this.mainUserTx.begin(); this.eventEntityManager.joinTransaction(); List<Job> jobs = this.eventEntityManager.createQuery( "from Job j where j.mappedResources=:mp and (j.jobStage=:aborted or j.jobStage=:finished)") .setParameter("aborted", CrawlStatus.ABORTED.toString()) .setParameter("finished", CrawlStatus.FINISHED.toString()).setParameter("mp", false) .getResultList(); this.eventEntityManager.flush(); this.mainUserTx.commit(); BlockingQueue<Runnable> q = this.threadPool.getQueue(); Collections.sort(jobs, jobSizeComparator); for (Job job : jobs) { // look if job is awaiting in the queue boolean skipThis = false; Iterator<Runnable> itOnQ = q.iterator(); while (itOnQ.hasNext()) { Mapper m = (Mapper) itOnQ.next(); if (job.getId() != null && job.getId().equals(m.getJobId())) { skipThis = true; break; } } if (skipThis) { continue; } // look if job is running or completed if (!this.mappers.containsKey(job.getId())) { Mapper mapper = new Mapper(job, this.flagBearer); this.threadPool.execute(mapper); this.mappers.put(job.getId(), mapper); } } try { Thread.sleep(JobMapperMonitor.DELAY); } catch (InterruptedException e) { continue; } } while (this.threadPool.getActiveCount() > 0); } catch (Exception e) { e.printStackTrace(); } finally { try { if (this.mainUserTx != null && this.mainUserTx.isActive()) { this.mainUserTx.rollback(); } } catch (IllegalStateException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (SecurityException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (SystemException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }
From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java
/** * Tries to remove from the work queue all {@link Future} tasks that have * been cancelled. This method can be useful as a storage reclamation * operation, that has no other impact on functionality. Cancelled tasks are * never executed, but may accumulate in work queues until worker threads * can actively remove them. Invoking this method instead tries to remove * them now. However, this method may fail to remove tasks in the presence * of interference by other threads./*from w w w. j av a 2 s. co m*/ */ public void purge() { final BlockingQueue<Runnable> q = workQueue; try { Iterator<Runnable> it = q.iterator(); while (it.hasNext()) { Runnable r = it.next(); if (r instanceof Future<?> && ((Future<?>) r).isCancelled()) it.remove(); } } catch (ConcurrentModificationException fallThrough) { // Take slow path if we encounter interference during traversal. // Make copy for traversal and call remove for cancelled entries. // The slow path is more likely to be O(N*N). for (Object r : q.toArray()) if (r instanceof Future<?> && ((Future<?>) r).isCancelled()) q.remove(r); } tryTerminate(); // In case SHUTDOWN and now empty }
From source file:org.apache.hadoop.hbase.regionserver.CompactSplit.java
public String dumpQueue() { StringBuffer queueLists = new StringBuffer(); queueLists.append("Compaction/Split Queue dump:\n"); queueLists.append(" LargeCompation Queue:\n"); BlockingQueue<Runnable> lq = longCompactions.getQueue(); Iterator<Runnable> it = lq.iterator(); while (it.hasNext()) { queueLists.append(" " + it.next().toString()); queueLists.append("\n"); }//ww w . j ava 2s . co m if (shortCompactions != null) { queueLists.append("\n"); queueLists.append(" SmallCompation Queue:\n"); lq = shortCompactions.getQueue(); it = lq.iterator(); while (it.hasNext()) { queueLists.append(" " + it.next().toString()); queueLists.append("\n"); } } queueLists.append("\n"); queueLists.append(" Split Queue:\n"); lq = splits.getQueue(); it = lq.iterator(); while (it.hasNext()) { queueLists.append(" " + it.next().toString()); queueLists.append("\n"); } return queueLists.toString(); }
From source file:org.apache.hadoop.hbase.regionserver.CompactSplitThread.java
public String dumpQueue() { StringBuffer queueLists = new StringBuffer(); queueLists.append("Compaction/Split Queue dump:\n"); queueLists.append(" LargeCompation Queue:\n"); BlockingQueue<Runnable> lq = longCompactions.getQueue(); Iterator it = lq.iterator(); while (it.hasNext()) { queueLists.append(" " + it.next().toString()); queueLists.append("\n"); }// ww w . j a v a 2 s .com if (shortCompactions != null) { queueLists.append("\n"); queueLists.append(" SmallCompation Queue:\n"); lq = shortCompactions.getQueue(); it = lq.iterator(); while (it.hasNext()) { queueLists.append(" " + it.next().toString()); queueLists.append("\n"); } } queueLists.append("\n"); queueLists.append(" Split Queue:\n"); lq = splits.getQueue(); it = lq.iterator(); while (it.hasNext()) { queueLists.append(" " + it.next().toString()); queueLists.append("\n"); } queueLists.append("\n"); queueLists.append(" Region Merge Queue:\n"); lq = mergePool.getQueue(); it = lq.iterator(); while (it.hasNext()) { queueLists.append(" " + it.next().toString()); queueLists.append("\n"); } return queueLists.toString(); }
From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java
@Test public void testWithConnectionFactoryCachedConnection() throws Exception { com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock( com.rabbitmq.client.ConnectionFactory.class); final List<com.rabbitmq.client.Connection> mockConnections = new ArrayList<com.rabbitmq.client.Connection>(); final List<Channel> mockChannels = new ArrayList<Channel>(); doAnswer(new Answer<com.rabbitmq.client.Connection>() { private int connectionNumber; @Override// w w w .j av a2 s . co m public com.rabbitmq.client.Connection answer(InvocationOnMock invocation) throws Throwable { com.rabbitmq.client.Connection connection = mock(com.rabbitmq.client.Connection.class); doAnswer(new Answer<Channel>() { private int channelNumber; @Override public Channel answer(InvocationOnMock invocation) throws Throwable { Channel channel = mock(Channel.class); when(channel.isOpen()).thenReturn(true); int channelNumnber = ++this.channelNumber; when(channel.toString()) .thenReturn("mockChannel" + connectionNumber + ":" + channelNumnber); mockChannels.add(channel); return channel; } }).when(connection).createChannel(); int connectionNumber = ++this.connectionNumber; when(connection.toString()).thenReturn("mockConnection" + connectionNumber); when(connection.isOpen()).thenReturn(true); mockConnections.add(connection); return connection; } }).when(mockConnectionFactory).newConnection(any(ExecutorService.class), anyString()); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); ccf.setCacheMode(CacheMode.CONNECTION); ccf.afterPropertiesSet(); Set<?> allocatedConnections = TestUtils.getPropertyValue(ccf, "allocatedConnections", Set.class); assertEquals(0, allocatedConnections.size()); BlockingQueue<?> idleConnections = TestUtils.getPropertyValue(ccf, "idleConnections", BlockingQueue.class); assertEquals(0, idleConnections.size()); final AtomicReference<com.rabbitmq.client.Connection> createNotification = new AtomicReference<com.rabbitmq.client.Connection>(); final AtomicReference<com.rabbitmq.client.Connection> closedNotification = new AtomicReference<com.rabbitmq.client.Connection>(); ccf.setConnectionListeners(Collections.singletonList(new ConnectionListener() { @Override public void onCreate(Connection connection) { assertNull(createNotification.get()); createNotification.set(targetDelegate(connection)); } @Override public void onClose(Connection connection) { assertNull(closedNotification.get()); closedNotification.set(targetDelegate(connection)); } })); Connection con1 = ccf.createConnection(); verifyConnectionIs(mockConnections.get(0), con1); assertEquals(1, allocatedConnections.size()); assertEquals(0, idleConnections.size()); assertNotNull(createNotification.get()); assertSame(mockConnections.get(0), createNotification.getAndSet(null)); Channel channel1 = con1.createChannel(false); verifyChannelIs(mockChannels.get(0), channel1); channel1.close(); //AMQP-358 verify(mockChannels.get(0), never()).close(); con1.close(); // should be ignored, and placed into connection cache. verify(mockConnections.get(0), never()).close(); assertEquals(1, allocatedConnections.size()); assertEquals(1, idleConnections.size()); assertNull(closedNotification.get()); /* * will retrieve same connection that was just put into cache, and reuse single channel from cache as well */ Connection con2 = ccf.createConnection(); verifyConnectionIs(mockConnections.get(0), con2); Channel channel2 = con2.createChannel(false); verifyChannelIs(mockChannels.get(0), channel2); channel2.close(); verify(mockChannels.get(0), never()).close(); con2.close(); verify(mockConnections.get(0), never()).close(); assertEquals(1, allocatedConnections.size()); assertEquals(1, idleConnections.size()); assertNull(createNotification.get()); /* * Now check for multiple connections/channels */ con1 = ccf.createConnection(); verifyConnectionIs(mockConnections.get(0), con1); con2 = ccf.createConnection(); verifyConnectionIs(mockConnections.get(1), con2); channel1 = con1.createChannel(false); verifyChannelIs(mockChannels.get(0), channel1); channel2 = con2.createChannel(false); verifyChannelIs(mockChannels.get(1), channel2); assertEquals(2, allocatedConnections.size()); assertEquals(0, idleConnections.size()); assertNotNull(createNotification.get()); assertSame(mockConnections.get(1), createNotification.getAndSet(null)); // put mock1 in cache channel1.close(); verify(mockChannels.get(1), never()).close(); con1.close(); verify(mockConnections.get(0), never()).close(); assertEquals(2, allocatedConnections.size()); assertEquals(1, idleConnections.size()); assertNull(closedNotification.get()); Connection con3 = ccf.createConnection(); assertNull(createNotification.get()); verifyConnectionIs(mockConnections.get(0), con3); Channel channel3 = con3.createChannel(false); verifyChannelIs(mockChannels.get(0), channel3); assertEquals(2, allocatedConnections.size()); assertEquals(0, idleConnections.size()); channel2.close(); con2.close(); assertEquals(2, allocatedConnections.size()); assertEquals(1, idleConnections.size()); channel3.close(); con3.close(); assertEquals(2, allocatedConnections.size()); assertEquals(2, idleConnections.size()); assertEquals("1", ccf.getCacheProperties().get("openConnections")); /* * Cache size is 1; con3 (mock1) should have been a real close. * con2 (mock2) should still be in the cache. */ verify(mockConnections.get(0)).close(30000); assertNotNull(closedNotification.get()); assertSame(mockConnections.get(0), closedNotification.getAndSet(null)); verify(mockChannels.get(1), never()).close(); verify(mockConnections.get(1), never()).close(30000); verify(mockChannels.get(1), never()).close(); verifyConnectionIs(mockConnections.get(1), idleConnections.iterator().next()); /* * Now a closed cached connection */ when(mockConnections.get(1).isOpen()).thenReturn(false); when(mockChannels.get(1).isOpen()).thenReturn(false); con3 = ccf.createConnection(); assertNotNull(closedNotification.get()); assertSame(mockConnections.get(1), closedNotification.getAndSet(null)); verifyConnectionIs(mockConnections.get(2), con3); assertNotNull(createNotification.get()); assertSame(mockConnections.get(2), createNotification.getAndSet(null)); assertEquals(2, allocatedConnections.size()); assertEquals(1, idleConnections.size()); assertEquals("1", ccf.getCacheProperties().get("openConnections")); channel3 = con3.createChannel(false); verifyChannelIs(mockChannels.get(2), channel3); channel3.close(); con3.close(); assertNull(closedNotification.get()); assertEquals(2, allocatedConnections.size()); assertEquals(2, idleConnections.size()); assertEquals("1", ccf.getCacheProperties().get("openConnections")); /* * Now a closed cached connection when creating a channel */ con3 = ccf.createConnection(); verifyConnectionIs(mockConnections.get(2), con3); assertNull(createNotification.get()); assertEquals(2, allocatedConnections.size()); assertEquals(1, idleConnections.size()); when(mockConnections.get(2).isOpen()).thenReturn(false); channel3 = con3.createChannel(false); assertNotNull(closedNotification.getAndSet(null)); assertNotNull(createNotification.getAndSet(null)); verifyChannelIs(mockChannels.get(3), channel3); channel3.close(); con3.close(); assertNull(closedNotification.get()); assertEquals(2, allocatedConnections.size()); assertEquals(2, idleConnections.size()); assertEquals("1", ccf.getCacheProperties().get("openConnections")); // destroy ccf.destroy(); assertNotNull(closedNotification.get()); verify(mockConnections.get(3)).close(30000); }
From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java
@Test public void testWithConnectionFactoryCachedConnectionAndChannels() throws Exception { com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock( com.rabbitmq.client.ConnectionFactory.class); final List<com.rabbitmq.client.Connection> mockConnections = new ArrayList<com.rabbitmq.client.Connection>(); final List<Channel> mockChannels = new ArrayList<Channel>(); doAnswer(new Answer<com.rabbitmq.client.Connection>() { private int connectionNumber; @Override// w ww . j a v a 2 s . co m public com.rabbitmq.client.Connection answer(InvocationOnMock invocation) throws Throwable { com.rabbitmq.client.Connection connection = mock(com.rabbitmq.client.Connection.class); doAnswer(new Answer<Channel>() { private int channelNumber; @Override public Channel answer(InvocationOnMock invocation) throws Throwable { Channel channel = mock(Channel.class); when(channel.isOpen()).thenReturn(true); int channelNumnber = ++this.channelNumber; when(channel.toString()) .thenReturn("mockChannel" + connectionNumber + ":" + channelNumnber); mockChannels.add(channel); return channel; } }).when(connection).createChannel(); int connectionNumber = ++this.connectionNumber; when(connection.toString()).thenReturn("mockConnection" + connectionNumber); when(connection.isOpen()).thenReturn(true); mockConnections.add(connection); return connection; } }).when(mockConnectionFactory).newConnection(any(ExecutorService.class), anyString()); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); ccf.setCacheMode(CacheMode.CONNECTION); ccf.setConnectionCacheSize(2); ccf.setChannelCacheSize(2); ccf.afterPropertiesSet(); Set<?> allocatedConnections = TestUtils.getPropertyValue(ccf, "allocatedConnections", Set.class); assertEquals(0, allocatedConnections.size()); BlockingQueue<?> idleConnections = TestUtils.getPropertyValue(ccf, "idleConnections", BlockingQueue.class); assertEquals(0, idleConnections.size()); @SuppressWarnings("unchecked") Map<?, List<?>> cachedChannels = TestUtils.getPropertyValue(ccf, "allocatedConnectionNonTransactionalChannels", Map.class); final AtomicReference<com.rabbitmq.client.Connection> createNotification = new AtomicReference<com.rabbitmq.client.Connection>(); final AtomicReference<com.rabbitmq.client.Connection> closedNotification = new AtomicReference<com.rabbitmq.client.Connection>(); ccf.setConnectionListeners(Collections.singletonList(new ConnectionListener() { @Override public void onCreate(Connection connection) { assertNull(createNotification.get()); createNotification.set(targetDelegate(connection)); } @Override public void onClose(Connection connection) { assertNull(closedNotification.get()); closedNotification.set(targetDelegate(connection)); } })); Connection con1 = ccf.createConnection(); verifyConnectionIs(mockConnections.get(0), con1); assertEquals(1, allocatedConnections.size()); assertEquals(0, idleConnections.size()); assertNotNull(createNotification.get()); assertSame(mockConnections.get(0), createNotification.getAndSet(null)); Channel channel1 = con1.createChannel(false); verifyChannelIs(mockChannels.get(0), channel1); channel1.close(); //AMQP-358 verify(mockChannels.get(0), never()).close(); con1.close(); // should be ignored, and placed into connection cache. verify(mockConnections.get(0), never()).close(); assertEquals(1, allocatedConnections.size()); assertEquals(1, idleConnections.size()); assertEquals(1, cachedChannels.get(con1).size()); assertNull(closedNotification.get()); /* * will retrieve same connection that was just put into cache, and reuse single channel from cache as well */ Connection con2 = ccf.createConnection(); verifyConnectionIs(mockConnections.get(0), con2); Channel channel2 = con2.createChannel(false); verifyChannelIs(mockChannels.get(0), channel2); channel2.close(); verify(mockChannels.get(0), never()).close(); con2.close(); verify(mockConnections.get(0), never()).close(); assertEquals(1, allocatedConnections.size()); assertEquals(1, idleConnections.size()); assertNull(createNotification.get()); /* * Now check for multiple connections/channels */ con1 = ccf.createConnection(); verifyConnectionIs(mockConnections.get(0), con1); con2 = ccf.createConnection(); verifyConnectionIs(mockConnections.get(1), con2); channel1 = con1.createChannel(false); verifyChannelIs(mockChannels.get(0), channel1); channel2 = con2.createChannel(false); verifyChannelIs(mockChannels.get(1), channel2); assertEquals(2, allocatedConnections.size()); assertEquals(0, idleConnections.size()); assertNotNull(createNotification.get()); assertSame(mockConnections.get(1), createNotification.getAndSet(null)); // put mock1 in cache channel1.close(); verify(mockChannels.get(1), never()).close(); con1.close(); verify(mockConnections.get(0), never()).close(); assertEquals(2, allocatedConnections.size()); assertEquals(1, idleConnections.size()); assertNull(closedNotification.get()); Connection con3 = ccf.createConnection(); assertNull(createNotification.get()); verifyConnectionIs(mockConnections.get(0), con3); Channel channel3 = con3.createChannel(false); verifyChannelIs(mockChannels.get(0), channel3); assertEquals(2, allocatedConnections.size()); assertEquals(0, idleConnections.size()); channel2.close(); con2.close(); assertEquals(2, allocatedConnections.size()); assertEquals(1, idleConnections.size()); channel3.close(); con3.close(); assertEquals(2, allocatedConnections.size()); assertEquals(2, idleConnections.size()); assertEquals(1, cachedChannels.get(con1).size()); assertEquals(1, cachedChannels.get(con2).size()); /* * Cache size is 2; neither should have been a real close. * con2 (mock2) and con1 should still be in the cache. */ verify(mockConnections.get(0), never()).close(30000); assertNull(closedNotification.get()); verify(mockChannels.get(1), never()).close(); verify(mockConnections.get(1), never()).close(30000); verify(mockChannels.get(1), never()).close(); assertEquals(2, idleConnections.size()); Iterator<?> iterator = idleConnections.iterator(); verifyConnectionIs(mockConnections.get(1), iterator.next()); verifyConnectionIs(mockConnections.get(0), iterator.next()); /* * Now a closed cached connection */ when(mockConnections.get(1).isOpen()).thenReturn(false); con3 = ccf.createConnection(); assertNotNull(closedNotification.get()); assertSame(mockConnections.get(1), closedNotification.getAndSet(null)); verifyConnectionIs(mockConnections.get(0), con3); assertNull(createNotification.get()); assertEquals(2, allocatedConnections.size()); assertEquals(1, idleConnections.size()); channel3 = con3.createChannel(false); verifyChannelIs(mockChannels.get(0), channel3); channel3.close(); con3.close(); assertNull(closedNotification.get()); assertEquals(2, allocatedConnections.size()); assertEquals(2, idleConnections.size()); /* * Now a closed cached connection when creating a channel */ con3 = ccf.createConnection(); verifyConnectionIs(mockConnections.get(0), con3); assertNull(createNotification.get()); assertEquals(2, allocatedConnections.size()); assertEquals(1, idleConnections.size()); when(mockConnections.get(0).isOpen()).thenReturn(false); channel3 = con3.createChannel(false); assertNotNull(closedNotification.getAndSet(null)); assertNotNull(createNotification.getAndSet(null)); verifyChannelIs(mockChannels.get(2), channel3); channel3.close(); con3.close(); assertNull(closedNotification.get()); assertEquals(2, allocatedConnections.size()); assertEquals(2, idleConnections.size()); Connection con4 = ccf.createConnection(); assertSame(con3, con4); assertEquals(1, idleConnections.size()); Channel channelA = con4.createChannel(false); Channel channelB = con4.createChannel(false); Channel channelC = con4.createChannel(false); channelA.close(); assertEquals(1, cachedChannels.get(con4).size()); channelB.close(); assertEquals(2, cachedChannels.get(con4).size()); channelC.close(); assertEquals(2, cachedChannels.get(con4).size()); // destroy ccf.destroy(); assertNotNull(closedNotification.get()); // physical wasn't invoked, because this mockConnection marked with 'false' for 'isOpen()' verify(mockConnections.get(0)).close(30000); verify(mockConnections.get(1)).close(30000); verify(mockConnections.get(2)).close(30000); }