private static void run() { Config config = new Config("queueTest"); HazelcastInstance hzInstance = Hazelcast.newHazelcastInstance(config); IQueue<Integer> queue = hzInstance.getQueue(QNAME); long startTime = System.currentTimeMillis(); int i = 0; while (i++ < 2000000) { if (i % 10000 == 0) { logger.info(Integer.toString(i) + "\t" + String.format("%8.3f", (double) (System.currentTimeMillis() - startTime) / i)); } queue.add(i); } }
/** * Polls the response queue and processes the received events. If no events are received this operation times out. */ private boolean pollAndProcess(final Set<String> processedKeys, final String syncRequestId, final IQueue<Event> responseQueue) throws InterruptedException, SyncFailureException { final Event event = responseQueue.poll(timeout, TimeUnit.MILLISECONDS); if (event != null) { if (event.id().equals(syncRequestId)) { // Sync completed return true; } else { processEvent(event, processedKeys); } } else { // No response - unable to sync throw new SyncFailureException("Unable to sync, no response from queue"); } return false; }
public int drainTo(Collection<? super E> objects, int i) { if (objects == null) throw new NullPointerException("drainTo null!"); if (i < 0) throw new IllegalArgumentException("Negative maxElements:" + i); if (i == 0) return 0; if (objects instanceof IQueue) { if (((IQueue) objects).getName().equals(getName())) { throw new IllegalArgumentException("Cannot drainTo self!"); } } E e; int counter = 0; while (counter < i && (e = poll()) != null) { objects.add(e); counter++; } return counter; }
@Test public void testQueuePoll() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); final CountDownLatch cl = new CountDownLatch(1); final IQueue<String> queue = hClient.getQueue("testQueuePoll"); assertTrue(queue.offer("a")); assertEquals("a", queue.poll()); new Thread(new Runnable() { public void run() { try { Thread.sleep(100); assertEquals("b", queue.poll(100, TimeUnit.MILLISECONDS)); cl.countDown(); } catch (InterruptedException e) { throw new RuntimeException(e); } } }).start(); Thread.sleep(50); assertTrue(queue.offer("b")); assertTrue(cl.await(200, TimeUnit.MILLISECONDS)); }
@Test public void iterator() { HazelcastClient hClient = getHazelcastClient(); IQueue<String> queue = hClient.getQueue("iterator"); assertTrue(queue.isEmpty()); int count = 100; Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < count; i++) { queue.offer("" + i); map.put(i, 1); } Iterator<String> it = queue.iterator(); while (it.hasNext()) { String o = it.next(); map.put(Integer.valueOf(o), map.get(Integer.valueOf(o)) - 1); } for (int i = 0; i < count; i++) { assertTrue(map.get(i) == 0); } }
@Test public void removeAll() { HazelcastClient hClient = getHazelcastClient(); IQueue<String> queue = hClient.getQueue("removeAll"); assertTrue(queue.isEmpty()); int count = 100; Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < count; i++) { queue.offer("" + i); map.put(i, 1); } List<String> list = new ArrayList<String>(); for (int i = 0; i < count / 2; i++) { list.add(String.valueOf(i)); } queue.removeAll(list); assertTrue(queue.size() == count / 2); }
@Test public void testIterator() { HazelcastClient hClient = getHazelcastClient(); IQueue<String> queue = hClient.getQueue("testIterator"); assertTrue(queue.isEmpty()); int count = 100; Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < count; i++) { queue.offer("" + i); map.put(i, 1); } Iterator<String> it = queue.iterator(); while (it.hasNext()) { String item = it.next(); map.remove(Integer.valueOf(item)); it.remove(); } assertEquals(0, queue.size()); assertEquals(0, map.size()); }
@Setup @SuppressWarnings("unchecked") public void setup() { queues = new IQueue[queueLength]; // the KeyLocality has to be RANDOM here, since we need different queues on each Worker String[] names = generateStringKeys(name, queueLength, name.length() + 5, RANDOM, targetInstance); for (int i = 0; i < queues.length; i++) { queues[i] = targetInstance.getQueue(names[i]); } for (IQueue<Long> queue : queues) { for (int i = 0; i < messagesPerQueue; i++) { queue.add(0L); } } }
@Override public void run() { IQueue<String> demoQueue = hazelcastInstance.getQueue("demo.queue"); while (!shutdown) { String data = null; try { data = demoQueue.poll(2, TimeUnit.SECONDS); } catch (InterruptedException ex) { // ignore } if (data != null) { log.info("Read data: {}", data); } } }
@Bean public Object testHazelcast(HazelcastInstance inst) throws Exception { ConcurrentMap aMap = inst.getMap("cz.rkr"); aMap.put("initialValue1", "val1"); aMap.put("initialValue2", "val2"); aMap.put("initialValue3", "val3"); aMap.put("initialValue4", "val4"); aMap.put("initialValue5", "val5"); IQueue<String> queueCustomers = inst.getQueue("cz.eetlite"); queueCustomers.offer("Tom"); queueCustomers.offer("Mary"); queueCustomers.offer("Jane"); System.out.println("First customer: " + queueCustomers.poll()); System.out.println("Second customer: " + queueCustomers.peek()); System.out.println("Second customer: " + queueCustomers.peek()); System.out.println("First customer: " + queueCustomers.poll()); System.out.println("First customer: " + queueCustomers.poll()); System.out.println("Queue size: " + queueCustomers.size()); queueCustomers.put("TEST"); System.out.println("Queue size: " + queueCustomers.size()); System.out.println("take: " + queueCustomers.take()); System.out.println("Queue size: " + queueCustomers.size()); return aMap; }
@Override @SuppressWarnings("unchecked") public <E> IQueue<E> getIQueue(String name) { name = Objects.requireNonNull(name); final IQueue<E> valu = getBeanSafely(name, IQueue.class); if (null != valu) { return valu; } return hz().getQueue(name); }
public void awaitEmpty( IQueue<?> queue ) throws InterruptedException{ synchronized( monitor ){ if( !queue.isEmpty() ){ monitor.wait(); } } }
private static void run() { Config config = new Config("queueTest"); QueueConfig queueConfig = config.getQueueConfig(QNAME); QueueStoreConfig queueStoreConfig = new QueueStoreConfig(); queueStoreConfig.setEnabled(true); queueStoreConfig.setStoreImplementation(new MockQueueStore()); queueStoreConfig.getProperties().setProperty("memory-limit", "0"); queueConfig.setQueueStoreConfig(queueStoreConfig); HazelcastInstance hzInstance = Hazelcast.newHazelcastInstance(config); IQueue<Integer> queue = hzInstance.getQueue(QNAME); long startTime = System.currentTimeMillis(); int i = 0; while (i++ < 2000000) { if (i % 10000 == 0) { logger.info(Integer.toString(i) + "\t" + String.format("%8.3f", (double) (System.currentTimeMillis() - startTime) / i)); } queue.add(i); } }
private static void consume(HazelcastInstance hazelcastInstance) { IQueue<String> cola = hazelcastInstance.getQueue("cola"); while (true){ try { System.out.println("Taken from queue: "+cola.take()); } catch (InterruptedException e) { e.printStackTrace(); } } }
private static void produce(HazelcastInstance hazelcastInstance) { IQueue<String> cola = hazelcastInstance.getQueue("cola"); int count=0; while (true){ try { cola.offer(Integer.toString(count++)); Thread.sleep(1000); System.out.println("Added to queue. It has now "+cola.size()); } catch (InterruptedException e) { e.printStackTrace(); } } }
private void sendSyncResponse(final String syncId, final String replayFromId) { final IQueue<Event> queue = hz.getQueue(syncId); replayStream(replayFromId).forEach(event -> { try { queue.put(event); } catch (InterruptedException e) { throw new IllegalStateException("Unable to respond to sync request", e); } }); }
private void waitForSyncResponse(final Set<String> processedKeys, final String syncRequestId) throws SyncFailureException { // Wait/handle the response try { // The response queue uses the same name as the entire sync-operation final IQueue<Event> responseQueue = hz.getQueue(syncRequestId); while (true) { if (pollAndProcess(processedKeys, syncRequestId, responseQueue)) { break; } } } catch (final InterruptedException e) { throw new IllegalStateException("Interrupted", e); } }
@Provides @Singleton public HzConsumeCommandsRoute hzConsumeCommandsRoute( @Named("commandsDestinationUri") String commandsDestinationUri, HzInventoryItemCmdProcessor inventoryItemCmdProcessor, IQueue<UUID> eventsQueue) { return new HzConsumeCommandsRoute(commandsDestinationUri, inventoryItemCmdProcessor, eventsQueue); }
@Provides @Singleton public HzConsumeEventsRoute hzConsumeEventsRoute( SnapshotReader<UUID, InventoryItemAggregateRoot> snapshotReader, IMap<UUID, Snapshot<InventoryItemAggregateRoot>> lastSnapshotMap, IQueue<UUID> eventsQueue) { return new HzConsumeEventsRoute(eventsQueueConsumers, snapshotReader, lastSnapshotMap, eventsQueue); }
private void startIdProducer() { producer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { IQueue<Integer> q = Hazelcast.getQueue("default"); if (q.size() < 5000) { for (int i = 0; i < 5000; i++) { q.offer(ids.incrementAndGet()); } } } }, 0, 1000); }
@Override public boolean equals(Object o) { if (o instanceof IQueue && o != null) { return getName().equals(((IQueue) o).getName()); } else { return false; } }
@Test public void testQueueOffer() throws InterruptedException { HazelcastClient hClient = getHazelcastClient(); IQueue<String> queue = hClient.getQueue("testQueueOffer"); assertTrue(queue.offer("a")); assertTrue(queue.offer("b", 10, TimeUnit.MILLISECONDS)); assertEquals("a", queue.poll()); assertEquals("b", queue.poll()); }
@Test public void testQueueRemove() { HazelcastClient hClient = getHazelcastClient(); IQueue<String> queue = hClient.getQueue("testQueueRemove"); assertTrue(queue.offer("a")); assertEquals("a", queue.remove()); }
@Test public void testQueuePeek() { HazelcastClient hClient = getHazelcastClient(); IQueue<String> queue = hClient.getQueue("testQueuePeek"); assertTrue(queue.offer("a")); assertEquals("a", queue.peek()); }
@Test public void element() { HazelcastClient hClient = getHazelcastClient(); IQueue<String> queue = hClient.getQueue("element"); assertTrue(queue.offer("a")); assertEquals("a", queue.element()); }
@Test public void addAll() { HazelcastClient hClient = getHazelcastClient(); IQueue<String> queue = hClient.getQueue("addAll"); List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); assertTrue(queue.addAll(list)); assertEquals("a", queue.poll()); assertEquals("b", queue.poll()); }
@Test public void clear() { HazelcastClient hClient = getHazelcastClient(); IQueue<String> queue = hClient.getQueue("clear"); List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); assertTrue(queue.size() == 0); assertTrue(queue.addAll(list)); assertTrue(queue.size() == 2); queue.clear(); assertTrue(queue.size() == 0); }
@Test public void containsAll() { HazelcastClient hClient = getHazelcastClient(); IQueue<String> queue = hClient.getQueue("containsAll"); List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); assertTrue(queue.size() == 0); assertTrue(queue.addAll(list)); assertTrue(queue.size() == 2); assertTrue(queue.containsAll(list)); }
@Test public void isEmpty() { HazelcastClient hClient = getHazelcastClient(); IQueue<String> queue = hClient.getQueue("isEmpty"); assertTrue(queue.isEmpty()); queue.offer("asd"); assertFalse(queue.isEmpty()); }
@Override protected void execute(String... args) { withHazelcast(hazelcast -> { String name = "default"; IQueue<Integer> queue = hazelcast.getQueue(name); if (args.length > 0) { if ("master".equals(args[0])) { IntStream.rangeClosed(1, 10).forEach(i -> { try { queue.put(i); } catch (InterruptedException e) { } }); } } readConsoleWhile(hazelcast, name, () -> { queue.forEach(e -> show("element = %d.", e)); return null; }, queue::size); }); }
@Verify public void globalVerify() { IQueue queue = targetInstance.getQueue(name + 'q'); IList<TxnCounter> results = targetInstance.getList(name + "results"); TxnCounter total = new TxnCounter(); for (TxnCounter counter : results) { total.add(counter); } logger.info(name + ": " + total + " from " + results.size() + " worker Threads Queue size=" + queue.size()); assertFalse(name + ": firstLock.isLocked()", firstLock.isLocked()); assertFalse(name + ": secondLock.isLocked()", secondLock.isLocked()); // TODO: check if this assert can be re-enabled: assertEquals(total.committed - total.rolled, queue.size()) }