Search code examples
javaamqpqpid

Can't get Java QPID to work


I'm trying to create a simple AMQP application in Java. The application should simply bounce a number between two clients. Each time the number is received the client should add 1 to it and send it back to the other client. However, I cannot get the example to work at all. I can send the first number, but it is never received. In the webinterface of the broker I see zero connections and messages. I am doing something wrong, but can't figure out what it is. Hopefully someone here can spot the error(s). Below is the code I've written.

    Messenger mng = Proton.messenger();
    mng.start();
    mng.subscribe("localhost:5672");

    Message msg = Proton.message();
    msg.setAddress("localhost:5672");
    msg.setSubject("foobar");


    if (args.length > 2 &&  args[2].equals("foo"))
    {
        System.out.println("Sending initial: 1");
        msg.setBody(new AmqpValue("1"));
        mng.put(msg);   
        mng.send();
        System.out.println("Sent initial: 1");
    }


    try 
    {
        while (true)
        {
            mng.recv(1);
            while(mng.incoming() > 0) {
                Message message = mng.get();
                int consumed = Integer.parseInt(message.getBody().toString());
                System.out.println("Received: " + consumed);
                consumed = consumed % 100;
                if (consumed == 0)
                {
                    long seconds = (System.currentTimeMillis() - start) / 1000;
                    start = System.currentTimeMillis();
                    System.out.println("Last hundred messages took (s): " + seconds);
                }

                msg.setBody(new AmqpValue(""+(consumed+1)));
                mng.put(msg);
                mng.send();
            }
        }
    } 
    catch (Exception e) 
    {
        System.out.println("proton error: " + e.getMessage());
    }

Solution

  • Well, I turned off my firewall completely and changed the code a bit to listen and subscribe to my local IP address and that worked well for me:

    Messenger mng = Proton.messenger();
    mng.start();
    mng.subscribe("amqp://~xxx.xxx.xxx.xxx");
    
    Message msg = Proton.message();
    msg.setAddress("amqp://yyy.yyy.yyy.yyy");
    ....