On 09/26/2010 02:25 AM, [email protected] wrote:
I am using the qpid client API in my program.

WIth that API (and with 0.6 that is the recommended option), you could as Steve suggested use LocalQueue instead of MessageListener. See the attached simple example of the request-response pattern for how that might look.

Do you have any method to
convert the qpid client API to qpid message API? What I have in my program
context is qpid:client:Session and if I use qpid messaging I have to use
qpid:messaging:Session. Is there a way to convert qpid:client:Session to
qpid:messaging:Session?

No, there is no way to convert between them. It is really an either/or decision. I would only recommend using the messaging API if you can update to trunk or wait until the next release (end of October) however. The 0.6 release did not have a fully implemented messaging API. If you do want to consider moving to that API at some point, have a read of http://qpid.apache.org/books/0.7/Programming-In-Apache-Qpid/html/ (refers to trunk code). There is also a client-server example available for that.
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */


#include <qpid/client/Connection.h>
#include <qpid/client/AsyncSession.h>
#include <qpid/client/Session.h>
#include <qpid/client/Message.h>
#include <qpid/client/SubscriptionManager.h>

#include <cstdlib>
#include <iostream>

using namespace qpid::client;
using namespace qpid::sys;
using namespace qpid::framing;

using std::string;

int main(int argc, char** argv) {
    int count = argc>1 ? atoi(argv[1]) : 10;
    ConnectionSettings settings;
    settings.tcpNoDelay = true;
    if (argc>2) settings.host = argv[2];
    if (argc>3) settings.port = atoi(argv[3]);
    Connection connection;
    try {
        connection.open(settings);
        Session session =  connection.newSession();

        std::string response_queue = session.getId().getName();
	session.queueDeclare(arg::queue=response_queue);

    	Message request("data", "request");
	request.getMessageProperties().setReplyTo(ReplyTo("", response_queue));

        SubscriptionManager subscriptions(session);
        LocalQueue responses;
        subscriptions.subscribe(responses, response_queue);

	for (int i = 0; i < count; ++i) {
            async(session).messageTransfer(arg::content=request);
            Message response = responses.get();
	}
        std::cout << "Processed " << count << " requests." << std::endl;

        connection.close();
        return 0;
    } catch(const std::exception& error) {
        std::cout << error.what() << std::endl;
    }
    return 1;
}


/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */


#include <qpid/client/Connection.h>
#include <qpid/client/Session.h>
#include <qpid/client/AsyncSession.h>
#include <qpid/client/Message.h>
#include <qpid/client/SubscriptionManager.h>

#include <cstdlib>
#include <iostream>
#include <string>

using namespace qpid::client;
using namespace qpid::framing;

int main(int argc, char** argv) {
    ConnectionSettings settings;
    settings.tcpNoDelay = true;
    if (argc>1) settings.host = argv[1];
    if (argc>2) settings.port = atoi(argv[2]);
    Connection connection;

    try {
        connection.open(settings);
        Session session =  connection.newSession();

	string request_queue = "request";
	session.queueDeclare(arg::queue=request_queue);

        SubscriptionManager subscriptions(session);
        LocalQueue requests;
        subscriptions.subscribe(requests, request_queue);

        Message msg;
        while (requests.get(msg, qpid::sys::TIME_INFINITE)) {
            if (msg.getMessageProperties().hasReplyTo()) {
                msg.getDeliveryProperties().setRoutingKey(msg.getMessageProperties().getReplyTo().getRoutingKey());
                async(session).messageTransfer(arg::destination=msg.getMessageProperties().getReplyTo().getExchange(),
                                               arg::content=msg);
            } else {
                std::cout << "Dropping request without reply-to: " << msg.getData() << std::endl;
            }
        }

        connection.close();
        return 0;
    } catch(const std::exception& error) {
        std::cout << error.what() << std::endl;
    }
    return 1;
}



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

Reply via email to