/** * 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)); }
@Override protected QueueChannel doGet() { if (this.capacity != null) { if (this.storeLock != null) { this.queue = new MessageGroupQueue(this.messageGroupStore, this.groupId, this.capacity, this.storeLock); } else { this.queue = new MessageGroupQueue(this.messageGroupStore, this.groupId, this.capacity); } } else if (this.storeLock != null) { this.queue = new MessageGroupQueue(this.messageGroupStore, this.groupId, this.storeLock); } else { this.queue = new MessageGroupQueue(this.messageGroupStore, this.groupId); } return super.doGet(); }
public PriorityChannelSpec messageStore(PriorityCapableChannelMessageStore messageGroupStore, Object groupId) { this.messageGroupQueue = new MessageGroupQueue(messageGroupStore, groupId); this.messageGroupQueue.setPriority(true); return this; }
@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); }
@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 validateMgqInterruptionStoreLock() throws Exception { MessageGroupStore mgs = Mockito.mock(MessageGroupStore.class); Mockito.doAnswer(invocation -> { Thread.sleep(5000); return null; }).when(mgs).addMessageToGroup(Mockito.any(Integer.class), Mockito.any(Message.class)); MessageGroup mg = Mockito.mock(MessageGroup.class); Mockito.when(mgs.getMessageGroup(Mockito.any())).thenReturn(mg); Mockito.when(mg.size()).thenReturn(0); final MessageGroupQueue queue = new MessageGroupQueue(mgs, 1, 1); final AtomicReference<InterruptedException> exceptionHolder = new AtomicReference<InterruptedException>(); Thread t1 = new Thread(() -> queue.offer(new GenericMessage<String>("hello"))); t1.start(); Thread.sleep(500); Thread t2 = new Thread(() -> { queue.offer(new GenericMessage<String>("hello")); try { queue.offer(new GenericMessage<String>("hello"), 100, TimeUnit.SECONDS); } catch (InterruptedException e) { exceptionHolder.set(e); } }); t2.start(); Thread.sleep(1000); t2.interrupt(); Thread.sleep(1000); assertTrue(exceptionHolder.get() instanceof InterruptedException); }
@Test public void testConcurrentReadersWithTimeout() throws Exception { final MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), 1, 1); final AtomicReference<Message<?>> messageHolder1 = new AtomicReference<Message<?>>(); final AtomicReference<Message<?>> messageHolder2 = new AtomicReference<Message<?>>();
@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 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(); }
@Test public void testConcurrentWritersWithTimeout() throws Exception { final MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), 1, 1); final AtomicReference<Boolean> booleanHolder1 = new AtomicReference<Boolean>(true); final AtomicReference<Boolean> booleanHolder2 = new AtomicReference<Boolean>(true);
/** * 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)); }
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);
@Test public void testPollTimeout() throws Exception { MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), "FOO"); Message<?> result = queue.poll(1, TimeUnit.MILLISECONDS); assertNull(result); }
@Test public void testConcurrentWriteReadMulti() throws Exception { final MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), 1, 4); final AtomicReference<Message<?>> messageHolder = new AtomicReference<Message<?>>();
@Bean public MessageChannel messageStoreBackedChannel() { return new QueueChannel( new MessageGroupQueue(<<MessageStoreImplementation>>, "Group ID") ); }
@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 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 testPutAndTake() throws Exception { MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), "FOO"); queue.put(new GenericMessage<>("foo")); Message<?> result = queue.take(); assertNotNull(result); }
@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()); }
@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()); }