Hello
i'm new in rabbitmq. can i to send messages with a specific priority of the producer for the consumer?

that is, if the queue has more than one message, the consumer must first receive the message that has the highest priority

we tested the property of the message "priority" but i not obtained a good result. i send my code.

Producer.java
public class Producer {

private static final String TASK_QUEUE_NAME = "task_queue";

public static void main(String[] argv)
throws java.io.IOException {

ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
boolean durable = true;
channel.queueDeclare(TASK_QUEUE_NAME, durable, false, false, null);
BasicProperties props = new BasicProperties.Builder().priority(0).build();

String message;

for (int i = 0; i < 3; i++) {
message = "cerooo";

channel.basicPublish("", TASK_QUEUE_NAME, props, message.getBytes());

System.out.println(" [x] Sent '" + message + "'");
}
channel.close();
connection.close();
}
}

Consumer.java
public class Consumer {

private static final String TASK_QUEUE_NAME = "task_queue";

public static void main(String[] argv)
throws java.io.IOException,
java.lang.InterruptedException {

ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();

channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

channel.basicQos(1);

QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(TASK_QUEUE_NAME, false, consumer);

while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());

System.out.println(" [x] Received '" + message + "'");
Thread.sleep(2000);
System.out.println(" [x] Done");

channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
}
}


Any ideas how I can do this?

thanks in advance.



Saludos,
Yuri.





Fin a la injusticia, LIBERTAD AHORA A NUESTROS CINCO COMPATRIOTAS QUE SE ENCUENTRAN INJUSTAMENTE EN PRISIONES DE LOS EEUU!
http://www.antiterroristas.cu
http://justiciaparaloscinco.wordpress.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20120126/00d22603/attachment.htm>

Search Discussions

  • Jerry Kuch at Jan 26, 2012 at 4:53 pm
    Hi, Yuri:

    Rabbit doesn't currently implement priorities, but you might consider an
    approach like the following, which some people have found helpful:

    http://dougbarth.github.com/2011/07/01/approximating-priority-with-rabbitmq.html

    Best regards,
    Jerry

    ----- Original Message -----
    From: "Yurisleidy Hern?ndez Moya" <ymoya at uci.cu>
    To: rabbitmq-discuss at lists.rabbitmq.com
    Sent: Thursday, January 26, 2012 8:26:57 AM
    Subject: [rabbitmq-discuss] priority of the messages



    Hello
    i'm new in rabbitmq. can i to send messages with a specific priority of the producer for the consumer?

    that is, if the queue has more than one message, the consumer must first receive the message that has the highest priority

    we tested the property of the message "priority" but i not obtained a good result. i send my code.

    Producer.java
    public class Producer {

    private static final String TASK_QUEUE_NAME = "task_queue";

    public static void main(String[] argv)
    throws java.io.IOException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    boolean durable = true;
    channel.queueDeclare(TASK_QUEUE_NAME, durable, false, false, null);
    BasicProperties props = new BasicProperties.Builder().priority(0).build();

    String message;

    for (int i = 0; i < 3; i++) {
    message = "cerooo";

    channel.basicPublish("", TASK_QUEUE_NAME, props, message.getBytes());

    System.out.println(" [x] Sent '" + message + "'");
    }
    channel.close();
    connection.close();
    }
    }

    Consumer.java
    public class Consumer {

    private static final String TASK_QUEUE_NAME = "task_queue";

    public static void main(String[] argv)
    throws java.io.IOException,
    java.lang.InterruptedException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    channel.basicQos(1);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(TASK_QUEUE_NAME, false, consumer);

    while (true) {
    QueueingConsumer.Delivery delivery = consumer.nextDelivery();
    String message = new String(delivery.getBody());

    System.out.println(" [x] Received '" + message + "'");
    Thread.sleep(2000);
    System.out.println(" [x] Done");

    channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
    }
    }


    Any ideas how I can do this?

    thanks in advance.



    Saludos,
    Yuri.





    _______________________________________________
    rabbitmq-discuss mailing list
    rabbitmq-discuss at lists.rabbitmq.com
    https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprabbitmq-discuss @
categoriesrabbitmq
postedJan 26, '12 at 4:26p
activeJan 26, '12 at 4:53p
posts2
users2
websiterabbitmq.com
irc#rabbitmq

People

Translate

site design / logo © 2022 Grokbase