public PriorityChannelSpec messageStore(PriorityCapableChannelMessageStore messageGroupStore, Object groupId) { this.messageGroupQueue = new MessageGroupQueue(messageGroupStore, groupId); this.messageGroupQueue.setPriority(true); return this; }
@Override public void put(Message<?> message) throws InterruptedException { final Lock lock = this.storeLock; lock.lockInterruptibly(); try { if (this.capacity != Integer.MAX_VALUE) { while (this.size() == this.capacity) { this.messageStoreNotFull.await(); } } this.doOffer(message); } finally { lock.unlock(); } }
@Test public void testConcurrentWriteRead() throws Exception { final MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), 1, 1); final AtomicReference<Message<?>> messageHolder = new AtomicReference<Message<?>>(); queue.offer(new GenericMessage<String>("hello"), 1000, TimeUnit.SECONDS); Thread t1 = new Thread(() -> { try { queue.offer(new GenericMessage<String>("Hi"), 1000, TimeUnit.SECONDS); } catch (Exception e) { LogFactory.getLog(getClass()).error("queue offer failed", e); } }); Thread t2 = new Thread(() -> { try { queue.poll(1000, TimeUnit.SECONDS); messageHolder.set(queue.poll(1000, TimeUnit.SECONDS)); } catch (Exception e) { LogFactory.getLog(getClass()).error("queue poll failed", e); } }); t1.start(); Thread.sleep(1000); t2.start(); Thread.sleep(1000); assertTrue(messageHolder.get().getPayload().equals("Hi")); }
@Test public void testSize() throws Exception { MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), "FOO"); queue.put(new GenericMessage<String>("foo")); assertEquals(1, queue.size()); queue.poll(100, TimeUnit.MILLISECONDS); assertEquals(0, queue.size()); }
private void doTestConcurrentAccess(int concurrency, final int maxPerTask, final Set<String> set) throws Exception { SimpleMessageStore messageGroupStore = new SimpleMessageStore(); final MessageGroupQueue queue = new MessageGroupQueue(messageGroupStore, "FOO"); ExecutorService executorService = Executors.newCachedThreadPool(); CompletionService<Boolean> completionService = new ExecutorCompletionService<>(executorService); boolean result = true; for (int j = 0; j < maxPerTask; j++) { result &= queue.add(new GenericMessage<>("count=" + big + ":" + j)); if (!result) { logger.warn("Failed to add"); for (int j = 0; j < maxPerTask; j++) { @SuppressWarnings("unchecked") Message<String> item = (Message<String>) queue.poll(10, TimeUnit.SECONDS); result &= item != null; if (!result) { assertEquals(0, queue.size()); messageGroupStore.expireMessageGroups(-10000); assertEquals(Integer.MAX_VALUE, queue.remainingCapacity());
@Test public void testPollEmpty() throws Exception { MessageGroupQueue queue = spy(new MessageGroupQueue(new SimpleMessageStore(), "FOO")); CountDownLatch latch1 = new CountDownLatch(1); AtomicBoolean first = new AtomicBoolean(true); willAnswer(i -> { latch1.countDown(); return first.getAndSet(false) ? null : i.callRealMethod(); }).given(queue).doPoll(); ExecutorService exec = Executors.newSingleThreadExecutor(); CountDownLatch latch2 = new CountDownLatch(1); exec.execute(() -> { try { Message<?> result = queue.poll(100, TimeUnit.MILLISECONDS); if (result != null) { latch2.countDown(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }); assertTrue(latch1.await(10, TimeUnit.SECONDS)); queue.put(new GenericMessage<>("foo")); assertTrue(latch2.await(10, TimeUnit.SECONDS)); exec.shutdownNow(); }
/** * Create a channel based on the provided {@link PriorityCapableChannelMessageStore} * and group id for message store operations. * @param messageGroupStore the {@link PriorityCapableChannelMessageStore} to use. * @param groupId to group message for this channel in the message store. * @since 5.0 */ public PriorityChannel(PriorityCapableChannelMessageStore messageGroupStore, Object groupId) { this(new MessageGroupQueue(messageGroupStore, groupId)); }
@Test public void validateMgqInterruption() throws Exception { final MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), 1, 1); final AtomicReference<InterruptedException> exceptionHolder = new AtomicReference<InterruptedException>(); Thread t = new Thread(() -> { queue.offer(new GenericMessage<String>("hello")); try { queue.offer(new GenericMessage<String>("hello"), 100, TimeUnit.SECONDS); } catch (InterruptedException e) { exceptionHolder.set(e); } }); t.start(); Thread.sleep(1000); t.interrupt(); Thread.sleep(1000); assertTrue(exceptionHolder.get() instanceof InterruptedException); }
@Test public void testCapacityAfterExpiry() throws Exception { SimpleMessageStore messageGroupStore = new SimpleMessageStore(); MessageGroupQueue queue = new MessageGroupQueue(messageGroupStore, "FOO", 2); queue.put(new GenericMessage<>("foo")); assertEquals(1, queue.remainingCapacity()); queue.put(new GenericMessage<>("bar")); assertEquals(0, queue.remainingCapacity()); Message<?> result = queue.poll(100, TimeUnit.MILLISECONDS); assertNotNull(result); assertEquals(1, queue.remainingCapacity()); }
@Test public void testCapacityExceeded() throws Exception { SimpleMessageStore messageGroupStore = new SimpleMessageStore(); MessageGroupQueue queue = new MessageGroupQueue(messageGroupStore, "FOO", 1); queue.put(new GenericMessage<>("foo")); assertFalse(queue.offer(new GenericMessage<>("bar"), 100, TimeUnit.MILLISECONDS)); }
@Test public void testPutAndPoll() throws Exception { MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), "FOO"); queue.put(new GenericMessage<>("foo")); Message<?> result = queue.poll(100, TimeUnit.MILLISECONDS); assertNotNull(result); }
@Test public void testPutAndTake() throws Exception { MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), "FOO"); queue.put(new GenericMessage<>("foo")); Message<?> result = queue.take(); assertNotNull(result); }
@Test public void testPollTimeout() throws Exception { MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), "FOO"); Message<?> result = queue.poll(1, TimeUnit.MILLISECONDS); assertNull(result); }
@Override public int remainingCapacity() { if (this.capacity == Integer.MAX_VALUE) { return Integer.MAX_VALUE; } return this.capacity - this.size(); }
@Override public Message<?> poll(long timeout, TimeUnit unit) throws InterruptedException { Message<?> message = null; long timeoutInNanos = unit.toNanos(timeout); final Lock lock = this.storeLock; lock.lockInterruptibly(); try { message = doPoll(); while (message == null && timeoutInNanos > 0) { timeoutInNanos = this.messageStoreNotEmpty.awaitNanos(timeoutInNanos); message = doPoll(); } } finally { lock.unlock(); } return message; }
@Override public Iterator<Message<?>> iterator() { return getMessages().iterator(); }
@Override public int drainTo(Collection<? super Message<?>> c) { return this.drainTo(c, Integer.MAX_VALUE); }
@Override public boolean offer(Message<?> message) { boolean offered = true; final Lock lock = this.storeLock; try { lock.lockInterruptibly(); try { offered = this.doOffer(message); } finally { lock.unlock(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } return offered; }
@Test public void testConcurrentReadWrite() throws Exception { final MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), 1, 1); final AtomicReference<Message<?>> messageHolder = new AtomicReference<Message<?>>(); Thread t1 = new Thread(() -> { try { messageHolder.set(queue.poll(1000, TimeUnit.SECONDS)); } catch (Exception e) { LogFactory.getLog(getClass()).error("queue poll failed", e); } }); Thread t2 = new Thread(() -> { try { queue.offer(new GenericMessage<String>("hello"), 1000, TimeUnit.SECONDS); } catch (Exception e) { LogFactory.getLog(getClass()).error("queue offer failed", e); } }); t1.start(); t2.start(); Thread.sleep(1000); assertTrue(messageHolder.get() instanceof Message); }