On 06/10/2011 04:12 PM, Ilyushonak Barys wrote:
Could you please share an example of using QMF in java?
Is it any way to discovery queue names from C++ broker (0.8) from java?

Attached is an example that lists the names of all the queues on the broker[1]. It does require the JMS client library to be patched (also attached) however (will try to get that or something similar committed in time for 0.12; see https://issues.apache.org/jira/browse/QPID-3302).

[1] tested against 0.8 broker, but only with latest JMS client code.
Index: client/src/main/java/org/apache/qpid/client/message/AMQMessageDelegate_0_10.java
===================================================================
--- client/src/main/java/org/apache/qpid/client/message/AMQMessageDelegate_0_10.java	(revision 1135068)
+++ client/src/main/java/org/apache/qpid/client/message/AMQMessageDelegate_0_10.java	(working copy)
@@ -741,6 +741,9 @@
         checkPropertyName(propertyName);
         checkWritableProperties();
         setApplicationHeader(propertyName, value);
+        if ("x-amqp-0-10.app-id".equals(propertyName)) {
+            _messageProps.setAppId(value.getBytes());
+        }
     }
 
     private static final Set<Class> ALLOWED = new HashSet();
import javax.jms.*;
import javax.naming.*;
import java.util.*;
//Need the following to decode amqp/list message
import java.nio.ByteBuffer;
import org.apache.qpid.transport.codec.BBDecoder;

public class Example {

    private static List decode(BytesMessage msg) throws JMSException
    {
        //only handles responses up to 2^31-1 bytes long
        byte[] data = new byte[(int) msg.getBodyLength()];
        msg.readBytes(data);
        BBDecoder decoder = new BBDecoder();
        decoder.init(ByteBuffer.wrap(data));
        return decoder.readList();
    }

    private static void getQueryRequest()
    {

    }

    public static void main(String[] args) {
        try {
            String url = "amqp://guest:guest@clientid/test?brokerlist='tcp://localhost:5672'";

            Properties props = new Properties();
            props.setProperty("java.naming.factory.initial", "org.apache.qpid.jndi.PropertiesFileInitialContextFactory");
            props.setProperty("connectionfactory.host", url);
            props.setProperty("destination.broker", "qmf.default.direct/broker");

            Context ctx = new InitialContext(props);
            ConnectionFactory factory = (ConnectionFactory) ctx.lookup("host");
            Destination target = (Destination) ctx.lookup("broker");

            Connection connection = factory.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer sender = session.createProducer(target);
            //In the 0.8 Qpid release, the broker incorrectly required
            //the client's response queue to be bound to
            //qmf.default.direct, requiring the following address:
            Destination responses = session.createQueue("qmf.default.direct/" + UUID.randomUUID() + "; {node: {type: topic}}");
            //However since the 0.10 release, the simpler approach
            //commented out below would be preferred: 
            //Destination responses = session.createTemporaryQueue();
            MessageConsumer receiver = session.createConsumer(responses);
            connection.start();
            MapMessage request = session.createMapMessage();
            request.setJMSReplyTo(responses);
            request.setStringProperty("x-amqp-0-10.app-id", "qmf2");
            request.setStringProperty("qmf.opcode", "_query_request");

            request.setString("_what", "OBJECT");
            Map schemaId = new HashMap();
            schemaId.put("_class_name", "queue");
            request.setObject("_schema_id", schemaId);

            sender.send(request);
            Message response = receiver.receive(10*1000);
            if (response != null) {
                if (response instanceof BytesMessage) {
                    List objects = decode((BytesMessage) response);
                    for (Object i : objects) {
                        Map properties = (Map) ((Map) i).get("_values");
                        System.out.println(new String((byte[]) properties.get("name"), "UTF8"));
                    }
                } else {
                    System.out.println("Received response in incorrect format: " + response);
                }
            } else {
                System.out.println("No response received");
            }
            connection.close();
        } catch (Exception e) {
            System.out.println("Exception occurred: " + e.toString());
            e.printStackTrace();
        }
    }
}

---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:[email protected]

Reply via email to