org.redisson.api.RQueue Java Examples

The following examples show how to use org.redisson.api.RQueue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedissionUtilsTest.java    From Redis_Learning with Apache License 2.0 6 votes vote down vote up
/** 
 * RQueue ӳ��Ϊ redis server��list���� 
 * ����--�����ȳ� 
 *  redis server ����: 
 * �鿴���м�---->keys * 
 * �鿴key������--->type testQueue 
 * �鿴key��ֵ ---->lrange testQueue 0 10  
 */  
@Test  
public void testGetRQueue() {  
    RQueue<Integer> rQueue=RedissionUtils.getInstance().getRQueue(redisson, "testQueue");  
    //�������  
    rQueue.clear();  
    Collection<Integer> c=Arrays.asList(12,45,12,34,56,78);  
    rQueue.addAll(c);  
    //�鿴����Ԫ��  
    System.out.println(rQueue.peek());  
    System.out.println(rQueue.element());  
    //�Ƴ�����Ԫ��  
    System.out.println(rQueue.poll());  
    System.out.println(rQueue.remove());  
    //�������  
    System.out.println(Arrays.toString(rQueue.toArray()));  
}
 
Example #2
Source File: RedissonQueueTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemove() {
    RQueue<Integer> queue = getQueue();
    queue.add(1);
    queue.add(2);
    queue.add(3);
    queue.add(4);

    queue.remove();
    queue.remove();

    assertThat(queue).containsExactly(3, 4);
    queue.remove();
    queue.remove();

    Assert.assertTrue(queue.isEmpty());
}
 
Example #3
Source File: RedissonDelayedQueueTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testDealyedQueuePollLastAndOfferFirstTo() {
    RBlockingQueue<Integer> queue1 = redisson.getBlockingQueue("test");
    RDelayedQueue<Integer> dealyedQueue = redisson.getDelayedQueue(queue1);
    
    dealyedQueue.offer(3, 5, TimeUnit.SECONDS);
    dealyedQueue.offer(2, 2, TimeUnit.SECONDS);
    dealyedQueue.offer(1, 1, TimeUnit.SECONDS);

    RQueue<Integer> queue2 = redisson.getQueue("deque2");
    queue2.offer(6);
    queue2.offer(5);
    queue2.offer(4);

    assertThat(dealyedQueue.pollLastAndOfferFirstTo(queue2.getName())).isEqualTo(1);
    assertThat(queue2).containsExactly(1, 6, 5, 4);
    
    dealyedQueue.destroy();
}
 
Example #4
Source File: DataHandler.java    From kkbinlog with Apache License 2.0 5 votes vote down vote up
private void doRunWithoutLock() {
    try {
        RQueue<EventBaseDTO> queue = redissonClient.getQueue(dataKey);
        EventBaseDTO dto;
        while ((dto = queue.poll()) != null) {
            doHandleWithoutLock(dto, retryTimes);
        }
    } catch (Exception e) {
        e.printStackTrace();
        log.severe("接收处理数据失败:" + e.toString());
    }

}
 
Example #5
Source File: DataHandler.java    From kkbinlog with Apache License 2.0 5 votes vote down vote up
private void doRunWithLock() {
    RLock rLock = redissonClient.getLock(dataKeyLock);
    EventBaseDTO dto;
    boolean lockRes = false;
    try {
        // 尝试加锁,最多等待50ms(防止过多线程等待),上锁以后6个小时自动解锁(防止redis队列太长,当前拿到锁的线程处理时间过长)
        lockRes = rLock.tryLock(50, 6 * 3600 * 1000, TimeUnit.MILLISECONDS);
        if (!lockRes) {
            return;
        }
        DATA_KEY_IN_PROCESS.add(dataKey);
        //拿到锁之后再获取队列
        RQueue<EventBaseDTO> queue = redissonClient.getQueue(dataKey);
        if (!queue.isExists() || queue.isEmpty()) {
            return;
        }
        //拿到锁且 队列不为空 进入
        while ((dto = queue.peek()) != null) {
            //处理完毕,把数据从队列摘除
            boolean handleRes = doHandleWithLock(dto, 0);
            if (handleRes) {
                queue.remove();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        log.severe("接收处理数据失败:" + e.toString());
    } finally {
        //forceUnlock是可以释放别的线程拿到的锁的,需要判断是否是当前线程持有的锁
        if (lockRes) {
            rLock.forceUnlock();
            rLock.delete();
            DATA_KEY_IN_PROCESS.remove(dataKey);
        }
    }
}
 
Example #6
Source File: DataPublisherRedisImpl.java    From kkbinlog with Apache License 2.0 5 votes vote down vote up
public void doPublish(String clientId, String dataKey, EventBaseDTO data) {
    RQueue<EventBaseDTO> dataList = redissonClient.getQueue(dataKey);
    boolean result = dataList.offer(data);
    log.info("推送结果{},推送信息,{}",result, data);
    String notifier = NOTIFIER.concat(clientId);
    RTopic<String> rTopic = redissonClient.getTopic(notifier);
    rTopic.publish(dataKey);
}
 
Example #7
Source File: RedissonQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddOffer() {
    RQueue<Integer> queue = getQueue();
    queue.add(1);
    queue.offer(2);
    queue.add(3);
    queue.offer(4);

    assertThat(queue).containsExactly(1, 2, 3, 4);
    Assert.assertEquals((Integer)1, queue.poll());
    assertThat(queue).containsExactly(2, 3, 4);
    Assert.assertEquals((Integer)2, queue.element());
}
 
Example #8
Source File: RedissonQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPollLimited() {
    RQueue<Integer> queue = getQueue();
    queue.addAll(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
    List<Integer> elements = queue.poll(3);
    assertThat(elements).containsExactly(1, 2, 3);
    List<Integer> elements2 = queue.poll(10);
    assertThat(elements2).containsExactly(4, 5, 6, 7);
    List<Integer> elements3 = queue.poll(5);
    assertThat(elements3).isEmpty();
}
 
Example #9
Source File: RedissonCollectionMapReduceTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
private RList<String> getCollection() {
    RList<String> list = null;
    if (RList.class.isAssignableFrom(mapClass)) {
        list = redisson.getList("list");
    } else if (RQueue.class.isAssignableFrom(mapClass)) {
        list = (RList<String>) redisson.<String>getQueue("queue");
    }
    return list;
}
 
Example #10
Source File: QueueExamples.java    From redisson-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();

    RQueue<String> queue = redisson.getQueue("myQueue");
    queue.add("1");
    queue.add("2");
    queue.add("3");
    queue.add("4");
    
    queue.contains("1");
    queue.peek();
    queue.poll();
    queue.element();
    
    for (String string : queue) {
        // iteration through bulk loaded values
    }
    
    boolean removedValue = queue.remove("1");
    queue.removeAll(Arrays.asList("1", "2", "3"));
    queue.containsAll(Arrays.asList("4", "1", "0"));
    
    List<String> secondList = new ArrayList<>();
    secondList.add("4");
    secondList.add("5");
    queue.addAll(secondList);

    RQueue<String> secondQueue = redisson.getQueue("mySecondQueue");
    
    queue.pollLastAndOfferFirstTo(secondQueue.getName());
    
    redisson.shutdown();
}
 
Example #11
Source File: DelayedQueueDecorator.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public void decorate(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, RedissonNamespaceParserSupport helper) {
    Assert.state(element.hasAttribute(DESTINATION_QUEUE_REF),
            "Illegal state. property \"" + DESTINATION_QUEUE_REF
            + "\" is required in the \""
            + helper.getName(element)
            + "\" element.");
    helper.addConstructorArgs(new RuntimeBeanReference(
                    helper.getAttribute(element, DESTINATION_QUEUE_REF)),
            RQueue.class, builder);
}
 
Example #12
Source File: RedissonQueueTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test(expected = NoSuchElementException.class)
public void testRemoveEmpty() {
    RQueue<Integer> queue = getQueue();
    queue.remove();
}
 
Example #13
Source File: RedissonQueueTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
<T> RQueue<T> getQueue() {
    return redisson.getQueue("queue");
}
 
Example #14
Source File: RedissonCollectionMapReduceTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Parameterized.Parameters(name = "{index} - {0}")
public static Iterable<Object[]> mapClasses() {
    return Arrays.asList(new Object[][]{
        {RList.class}, {RQueue.class}
    });
}
 
Example #15
Source File: BlockingDequeExamples.java    From redisson-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();

    RBlockingDeque<String> deque = redisson.getBlockingDeque("myQueue");
    deque.add("1");
    deque.add("2");
    deque.add("3");
    deque.add("4");
    
    deque.contains("1");
    deque.peek();
    deque.poll();
    deque.element();
    
    for (String string : deque) {
        // iteration through bulk loaded values
    }
    
    boolean removedValue = deque.remove("1");
    deque.removeAll(Arrays.asList("1", "2", "3"));
    deque.containsAll(Arrays.asList("4", "1", "0"));
    
    List<String> secondList = new ArrayList<>();
    secondList.add("4");
    secondList.add("5");
    deque.addAll(secondList);

    RQueue<String> secondQueue = redisson.getQueue("mySecondQueue");
    
    deque.pollLastAndOfferFirstTo(secondQueue.getName());
    
    deque.addLast("8");
    deque.addFirst("9");
    
    deque.addLast("30");
    deque.addFirst("80");
    
    String firstValue = deque.pollFirst();
    String lastValue = deque.pollLast();
    
    String peekFirstValue = deque.peekFirst();
    String peekLastValue = deque.peekLast();
    
    String firstRemoved = deque.removeFirst();
    String lastRemoved = deque.removeLast();
    
    
    Thread t = new Thread(() -> {
        try {
            String element1 = deque.poll(10, TimeUnit.SECONDS);
            
            String element2 = deque.take();
            
            String element3 = deque.pollFirst(3, TimeUnit.SECONDS);
            
            String element4 = deque.pollLast(3, TimeUnit.SECONDS);
            
            String element5 = deque.takeFirst();
            
            String element6 = deque.takeLast();
            
            String element7 = deque.pollLastAndOfferFirstTo(secondQueue.getName(), 4, TimeUnit.SECONDS);
            
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
    
    t.start();
    t.join();

    redisson.shutdown();
}
 
Example #16
Source File: DequeExamples.java    From redisson-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();

    RDeque<String> deque = redisson.getDeque("myQueue");
    deque.add("1");
    deque.add("2");
    deque.add("3");
    deque.add("4");
    
    deque.contains("1");
    deque.peek();
    deque.poll();
    deque.element();
    
    for (String string : deque) {
        // iteration through bulk loaded values
    }
    
    boolean removedValue = deque.remove("1");
    deque.removeAll(Arrays.asList("1", "2", "3"));
    deque.containsAll(Arrays.asList("4", "1", "0"));
    
    List<String> secondList = new ArrayList<>();
    secondList.add("4");
    secondList.add("5");
    deque.addAll(secondList);

    RQueue<String> secondQueue = redisson.getQueue("mySecondQueue");
    
    deque.pollLastAndOfferFirstTo(secondQueue.getName());
    
    deque.addLast("8");
    deque.addFirst("9");
    
    deque.addLast("30");
    deque.addFirst("80");
    
    String firstValue = deque.pollFirst();
    String lastValue = deque.pollLast();
    
    String peekFirstValue = deque.peekFirst();
    String peekLastValue = deque.peekLast();
    
    String firstRemoved = deque.removeFirst();
    String lastRemoved = deque.removeLast();
    
    redisson.shutdown();
}
 
Example #17
Source File: TracingRQueue.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
public TracingRQueue(RQueue<V> queue, TracingRedissonHelper tracingRedissonHelper) {
  super(queue, tracingRedissonHelper);
  this.queue = queue;
  this.tracingRedissonHelper = tracingRedissonHelper;
}
 
Example #18
Source File: TracingRedissonClient.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public <V> RQueue<V> getQueue(String name, Codec codec) {
  return new TracingRQueue<>(redissonClient.getQueue(name, codec), tracingRedissonHelper);
}
 
Example #19
Source File: TracingRedissonClient.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public <V> RDelayedQueue<V> getDelayedQueue(RQueue<V> destinationQueue) {
  return new TracingRDelayedQueue<>(redissonClient.getDelayedQueue(destinationQueue),
      tracingRedissonHelper);
}
 
Example #20
Source File: TracingRedissonClient.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public <V> RQueue<V> getQueue(String name) {
  return new TracingRQueue<>(redissonClient.getQueue(name), tracingRedissonHelper);
}
 
Example #21
Source File: RedissionUtils.java    From Redis_Learning with Apache License 2.0 2 votes vote down vote up
/** 
 * ��ȡ���� 
 * @param redisson 
 * @param objectName 
 * @return 
 */  
public <V> RQueue<V> getRQueue(RedissonClient redisson,String objectName){  
    RQueue<V> rQueue=redisson.getQueue(objectName);  
    return rQueue;  
}