2015-07-10 31 views
1

我的应用程序中有一些基于优先级的任务。我有两个队列“队列1”和“队列2”,我想给队列1最高优先级和队列2最低优先级。我设置队列1的优先级号码是255,队列2的优先级号码是200.我在执行任务时遇到问题,一旦执行优先级任务,进程正在等待,直到任务完成同步。但根据我们的项目需求,这个过程不应该等待,而是要开始。我如何实现它?RabbitMQ 3.5和消息优先级队列进程正在等待,直到任务完成

我审阅this博客,

收到的消息部分:

final CountDownLatch latch = new CountDownLatch(3); 
    ch.basicConsume(QUEUE, true, new DefaultConsumer(ch) { 
     @Override 
     public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { 
      System.out.println("Received " + new String(body)); 
      latch.countDown(); 
     } 
    }); 

    latch.await(); 

我已经在RabbitMQ的https://pamlesleylu.wordpress.com/2013/02/02/hello-world-for-spring-amqp-and-rabbitmq/

监制完成这一过程,而不优先级队列

public void execute() { 
    System.out.println("execute..."); 
    messageQueue.convertAndSend("hello " + counter.incrementAndGet()); 
} 

消费者

public void onMessage(Message msg) { 
    System.out.println(new String(msg.getBody())); 
    try { 
     Thread.sleep(1000); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
} 

回答

0

您需要将任务交给另一个线程(例如,使用TaskExecutor)。

但请记住,该消息将会消失,因此如果服务器崩溃,您将失去该消息。

在侦听器容器中增加concurrency可能会更好,因此您可以一次处理多个邮件。