[rabbitmq-discuss] RabbitMQ persistence and multi producers- multi consumers

Francisco Gonzalez-Blanch fblanch at gmail.com
Fri Jul 9 12:49:01 BST 2010


Thanks for the answers!  But If the producer is launched first and then the
consumer, i'm losing all the initial messages until the consumer is created.
I attach you the code snippets with the test.


/** Producer  **/

ConnectionFactory factory = new ConnectionFactory();

        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        factory.setRequestedHeartbeat(0);
        factory.setHost("127.0.0.1");
        factory.setPort(5672);

        Connection conn = factory.newConnection();

        Channel channel = conn.createChannel();
        String exchangeName = "myExchangeFanOut";
        String routingKey = "testRoute";

        for(int i= 0; i<1000000;i++){
            StringBuilder miCadena = new StringBuilder("Message num: ");
            miCadena.append(i);
            byte[] messageBodyBytes = miCadena.toString().getBytes();
            channel.basicPublish(exchangeName, routingKey,
MessageProperties.PERSISTENT_TEXT_PLAIN, messageBodyBytes) ;
            System.out.println("Sending message num: " + i);
        }
        channel.close();
        conn.close();

/** Consumer **/

ConnectionFactory factory = new ConnectionFactory();

        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        factory.setRequestedHeartbeat(0);
        factory.setHost("127.0.0.1");
        factory.setPort(5672);

        Connection conn = factory.newConnection();

        Channel channel = conn.createChannel();
        String exchangeName = "myExchangeFanOut";
        String queueName = "myQueue";
        String routingKey = "testRoute";
        boolean durable = true;
        channel.exchangeDeclare(exchangeName, "fanout", durable);
        channel.queueDeclare(queueName, durable, true, true,null); //
parametros
        channel.queueBind(queueName, exchangeName, routingKey);

        boolean noAck = false;
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(queueName, noAck, consumer);
        boolean runInfinite = true;

        while (runInfinite) {
            QueueingConsumer.Delivery delivery;
            try {
                delivery = consumer.nextDelivery();
            } catch (InterruptedException ie) {
                continue;
            }
            System.out.println("Processing received message. " + new
String(delivery.getBody()));
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(),
false);
        }

        channel.close();
        conn.close();


In addition i have another question , is it possible to make that not all
the consumers get all the messages  with a fanout exchange, i mean is it
possible to balance the messages in between the consumers. Thanks a lot.
regards!

Francisco González-Blanch Rodríguez


On Fri, Jul 9, 2010 at 1:39 PM, Ovidiu Deac <ovidiudeac at gmail.com> wrote:

> If nobody extracts the message from the queue the message won't be lost.
>
> On Fri, Jul 9, 2010 at 1:09 PM, Francisco Gonzalez-Blanch
> <fblanch at gmail.com> wrote:
> > Hi all,
> > I'm a newbie in rabbitmq and i'm working on a distributed data processing
> > application using rabbitmq as distributed task queue. I want to ask you
> > about where can i find information about how to make persistent queues,
> and
> > how to make that, if a producer sends messages to queue and no one is
> listen
> > to it and then a consumer is attached , the consumer gets all the
> messages,
> > even the ones that were produced before the consumer was attached. Thank
> you
> > very much.
> >
> >
> >
> > _______________________________________________
> > rabbitmq-discuss mailing list
> > rabbitmq-discuss at lists.rabbitmq.com
> > https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20100709/3cf288ce/attachment.htm>


More information about the rabbitmq-discuss mailing list