qpid C++ client and thread safety

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

qpid C++ client and thread safety

Adam Crain
Hi,

 

I have an application that needs to both read and write messages to a
broker, i.e. events can come from two sources

 

1)      the broker, something I've setup callbacks for

2)      the outside world, for arguments sake say someone presses a
button

 

Can I make read/write equally responsive using a single Session object?

 

i.e. can I launch a thread to handle my subscriptions, i.e.:

 

connection.open(host, port);
Session session =  connection.newSession();

SubscriptionManager subscriptions(session);

Listener listener(subscriptions);

subscriptions.subscribe(listener, "message_queue");

subscriptions.start(); //non-blocking call, starts another thread, runs
until stop is called

 

and then continue on in the parent thread responding to events from the
other source:

 

void OnEvent()

{

        message.getDeliveryProperties().setRoutingKey("routing_key");
        message.setData("Hi, Mom!");
        session.messageTransfer(arg::content=message,
arg::destination="amq.direct");

}

 

These were just code fragments taken from
http://qpid.apache.org/docs/api/cpp/html/index.html. Any insight you can
provide into this would be appreciated.

 

 

Adam Crain

Manager of R&D

Plymouth Systems, Inc.

919-428-1002

 

Reply | Threaded
Open this post in threaded view
|

Re: qpid C++ client and thread safety

Azim Fatehi
I don't believe the qpid objects are thread-safe. I had a case with two
threads sending messages using the same session object and found that it
didn't work unless I protected access to the qpid objects with a mutex.


On Wed, Jan 20, 2010 at 12:54 PM, Adam Crain <[hidden email]
> wrote:

> Hi,
>
>
>
> I have an application that needs to both read and write messages to a
> broker, i.e. events can come from two sources
>
>
>
> 1)      the broker, something I've setup callbacks for
>
> 2)      the outside world, for arguments sake say someone presses a
> button
>
>
>
> Can I make read/write equally responsive using a single Session object?
>
>
>
> i.e. can I launch a thread to handle my subscriptions, i.e.:
>
>
>
> connection.open(host, port);
> Session session =  connection.newSession();
>
> SubscriptionManager subscriptions(session);
>
> Listener listener(subscriptions);
>
> subscriptions.subscribe(listener, "message_queue");
>
> subscriptions.start(); //non-blocking call, starts another thread, runs
> until stop is called
>
>
>
> and then continue on in the parent thread responding to events from the
> other source:
>
>
>
> void OnEvent()
>
> {
>
>        message.getDeliveryProperties().setRoutingKey("routing_key");
>        message.setData("Hi, Mom!");
>        session.messageTransfer(arg::content=message,
> arg::destination="amq.direct");
>
> }
>
>
>
> These were just code fragments taken from
> http://qpid.apache.org/docs/api/cpp/html/index.html. Any insight you can
> provide into this would be appreciated.
>
>
>
>
>
> Adam Crain
>
> Manager of R&D
>
> Plymouth Systems, Inc.
>
> 919-428-1002
>
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: qpid C++ client and thread safety

Gordon Sim
In reply to this post by Adam Crain
On 01/20/2010 06:54 PM, Adam Crain wrote:

> Hi,
>
>
>
> I have an application that needs to both read and write messages to a
> broker, i.e. events can come from two sources
>
>
>
> 1)      the broker, something I've setup callbacks for
>
> 2)      the outside world, for arguments sake say someone presses a
> button
>
>
>
> Can I make read/write equally responsive using a single Session object?
>
>
>
> i.e. can I launch a thread to handle my subscriptions, i.e.:
>
>
>
> connection.open(host, port);
> Session session =  connection.newSession();
>
> SubscriptionManager subscriptions(session);
>
> Listener listener(subscriptions);
>
> subscriptions.subscribe(listener, "message_queue");
>
> subscriptions.start(); //non-blocking call, starts another thread, runs
> until stop is called

There is a bug with start() that does not allow you to handle any
exceptions making that unreliable. However you can yourself create a
separate dispatch thread and call SubscriptionManager::run() on it.
(This is all start() does, and by doing so yourself you could catch and
handle exceptions).

> and then continue on in the parent thread responding to events from the
> other source:
>
>
>
> void OnEvent()
>
> {
>
>          message.getDeliveryProperties().setRoutingKey("routing_key");
>          message.setData("Hi, Mom!");
>          session.messageTransfer(arg::content=message,
> arg::destination="amq.direct");
>
> }

It should certainly be safe to send messages on the same session from
one thread while processing messages received on that session from another.


However, unless there is any need to have them on the same session (e.g.
you need to transactionally tie the input and output streams together
into atomic chunks), I would probably create a separate session for sending.

> These were just code fragments taken from
> http://qpid.apache.org/docs/api/cpp/html/index.html. Any insight you can
> provide into this would be appreciated.
>
>
>
>
>
> Adam Crain
>
> Manager of R&D
>
> Plymouth Systems, Inc.
>
> 919-428-1002
>
>
>
>


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

Reply | Threaded
Open this post in threaded view
|

Re: qpid C++ client and thread safety

Gordon Sim
In reply to this post by Azim Fatehi
On 01/20/2010 07:50 PM, Azim Fatehi wrote:
> I don't believe the qpid objects are thread-safe. I had a case with two
> threads sending messages using the same session object and found that it
> didn't work unless I protected access to the qpid objects with a mutex.

If you still have some test code for that case I'd be happy to have a
look. It certainly *should* be the case that you can have two threads
sending on the same session, so any failure may be a bug.


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