You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@qpid.apache.org by Damon Rolfs <dr...@gmail.com> on 2011/02/04 06:56:37 UTC

Examples of Java Client connecting to C++ Broker?

I'm trying to write a simple Java client (using qpid-java-client-0.8) to
demonstrate communicating with a C++ QPID broker (AMQP 0.10). I would
appreciate any example code demonstrating a Java qpid client connecting to a
C++ qpid broker. I've found several examples of Java JMS and native clients
connecting to Java brokers, but none for Java -> C++.

I've been working to adapt the pure Java examples, however I've run into
issues I've been unable to resolve. I've had best luck using the AMQ*
classes rather than the JMS classes. I've been able to connect to the broker
and send a message into the exchange. The message gets dropped however
because my routing key is not set, despite my code setting it by explicitly
calling the AMQDestination.setRoutingKey() method. When I print the
Destination, I get: JMS Destination: :////?routingkey=''.

Using another (C++) client, I place a message for the Java client in a
queue. I have not been able to receive that message using the Java client.

Thank you in advance for your guidance.

Damon

Here's a snippet from my client sample (I removed some of the log code that
shows up below):

AMQConnection connection = new AMQConnection( host, port, "root", "r00tpw",
"10.1.3.3", "/" );
Session session = connection.createSession( true, Session.AUTO_ACKNOWLEDGE
);
AMQAnyDestination outboundQ = new AMQAnyDestination(
"ADDR:"+sendToExchange+"; {create: always}" );
outboundQ.setRoutingKey( new AMQShortString(routingKey) ); // routing key is
a string: B.RRAA.AMQPApp2Key.Something

AMQAnyDestination inboundQ = new AMQAnyDestination(
"ADDR:"+receivingQueue+"; {create: always}" );

MessageProducer producer = session.createProducer( outboundQ );
MessageConsumer consumer = session.createConsumer( inboundQ );

BytesMessage outboundMessage = createEmpMessage( session, empDataString );
// creates and populates a JMS BytesMessage
producer.send( outboundMessage );

Message inboundMessage = consumer.receive( 10 * 1000 ); // I've also tried
setting up a listener to no avail
String msgType = (inboundMessage != null) ? inboundMessage.getJMSType :
"###NULL###";
Logger.info( "RECEIVED:type:"+msgType+"\
tcontent:"+inboundMessage );
connection.close();

this is the log output:

983 [run-main] INFO itcm.JvmAmqpClient$ - identity:amqpapplication1
983 [run-main] INFO itcm.JvmAmqpClient$ - host:10.1.3.3
986 [run-main] INFO itcm.JvmAmqpClient$ - port:10002
986 [run-main] INFO itcm.JvmAmqpClient$ - count:1
986 [run-main] INFO itcm.JvmAmqpClient$ -
sendToExchange:FromExternalAMQPApp1.Ex
986 [run-main] INFO itcm.JvmAmqpClient$ -
routingKey:B.RRAA.AMQPApp2Key.Something
986 [run-main] INFO itcm.JvmAmqpClient$ - receivingQueue:ToAMQPApp1Queue
986 [run-main] INFO itcm.JvmAmqpClient$ - empDataString:Hello World!
986 [run-main] INFO itcm.JvmAmqpClient$ - Attempting to connection to:
amqp://root:********@10.1.3.3:10002/?brokerlist='tcp://10.1.3.3:10002<http://10.1.3.3:10002/?brokerlist=%27tcp://10.1.3.3:10002>
'
1334 [run-main] INFO org.apache.qpid.client.AMQConnection -
Connection:amqp://root:********@
10.1.3.3/?ssl='false'&brokerlist='tcp://10.1.3.3:10002'&ssl='false<http://10.1.3.3/?ssl=%27false%27&brokerlist=%27tcp://10.1.3.3:10002%27&ssl=%27false>
'
3382 [run-main] INFO org.apache.qpid.client.protocol.AMQProtocolSession -
Using ProtocolVersion for Session:0-10
3434 [run-main] INFO
org.apache.qpid.client.handler.ClientMethodDispatcherImpl - New Method
Dispatcher:AMQProtocolSession[null]
3491 [run-main] INFO org.apache.qpid.client.AMQConnection - Connecting with
ProtocolHandler Version:0-10
3944 [run-main] INFO org.apache.qpid.client.AMQConnection - Connected with
ProtocolHandler Version:0-10
3945 [run-main] INFO itcm.JvmAmqpClient$ - creating Session...
3981 [run-main] INFO org.apache.qpid.client.AMQSession - Created
session:org.apache.qpid.client.AMQSession_0_10@5ba0d9
3996 [run-main] INFO itcm.JvmAmqpClient$ - creating destination...
4101 [run-main] INFO itcm.JvmAmqpClient$ - outboundQ:
'FromExternalAMQPApp1.Ex'/None; {
  'create': 'always'
}
4101 [run-main] INFO itcm.JvmAmqpClient$ - outboundQ.routingKey:
B.RRAA.AMQPApp2Key.Something
4101 [run-main] INFO itcm.JvmAmqpClient$ - inboundQ: 'ToAMQPApp1Queue'/None;
{
  'create': 'always'
}
4101 [run-main] INFO itcm.JvmAmqpClient$ - connecting producer to
FromExternalAMQPApp1.Ex
4126 [run-main] INFO org.apache.qpid.client.BasicMessageProducer_0_10 -
MessageProducer
org.apache.qpid.client.BasicMessageProducer_0_10@165a5fausing publish
mode : ASYNC_PUBLISH_ALL
4126 [run-main] INFO itcm.JvmAmqpClient$ - connecting consumer to
ToAMQPApp1Queue
4159 [run-main] INFO org.apache.qpid.client.AMQSession - Prefetching delayed
existing messages will not flow until requested via receive*() or setML().
4206 [run-main] INFO itcm.JvmAmqpClient$ - crafting EMP message...
4224 [run-main] INFO itcm.JvmAmqpClient$ - bytemessage:Body:
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Hello
World!\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"...
JMS Correlation ID: null
JMS timestamp: 0
JMS expiration: 0
JMS priority: 4
JMS delivery mode: 2
JMS reply to: null
JMS Redelivered: false
JMS Destination: :////?routingkey=''
JMS Type: null
JMS MessageID: null
JMS Content-Type: application/octet-stream
AMQ message number: -1
Properties:<NONE>
4224 [run-main] INFO itcm.JvmAmqpClient$ - out msg destination:
:////?routingkey=''
4225 [run-main] INFO itcm.JvmAmqpClient$ - sending message:Body:
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Hello
World!\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"...
JMS Correlation ID: null
JMS timestamp: 0
JMS expiration: 0
JMS priority: 4
JMS delivery mode: 2
JMS reply to: null
JMS Redelivered: false
JMS Destination: :////?routingkey=''
JMS Type: null
JMS MessageID: null
JMS Content-Type: application/octet-stream
AMQ message number: -1
Properties:<NONE>
4229 [run-main] INFO itcm.JvmAmqpClient$ - receiving message...
4238 [run-main] INFO org.apache.qpid.client.AMQSession.Dispatcher -
Dispatcher-Channel-1 created
4239 [Dispatcher-Channel-1] INFO
org.apache.qpid.client.AMQSession.Dispatcher - Dispatcher-Channel-1 started
14240 [run-main] INFO itcm.JvmAmqpClient$ - ... RECEIVED:
14240 [run-main] INFO itcm.JvmAmqpClient$ - ###NULL###
14240 [run-main] INFO itcm.JvmAmqpClient$ - closing connection...
14240 [run-main] INFO org.apache.qpid.client.AMQSession - Closing session:
org.apache.qpid.client.AMQSession_0_10@5ba0d9
14240 [run-main] INFO org.apache.qpid.client.BasicMessageConsumer - Closing
consumer:1[21416300]
14241 [Dispatcher-Channel-1] INFO
org.apache.qpid.client.AMQSession.Dispatcher - Dispatcher-Channel-1 thread
terminating for channel 1:org.apache.qpid.client.AMQSession_0_10@5ba0d9
17517 [IoReceiver - /10.1.3.3:10002] WARN
org.apache.qpid.transport.SessionDelegate - CLOSED:
[ssn:"a7b91704-3c30-42b9-8fec-67205cbced31"]

Re: Examples of Java Client connecting to C++ Broker?

Posted by Damon Rolfs <dr...@gmail.com>.
Just wanted to shoot a quick thank you email. Your message nudged me in the
right direction and I was able to demonstrate the Java - C++ Broker
connectivity I hoped and in my preferred manner, via JMS. I found Drain to
be very helpful, too!  Thanks!

On Fri, Feb 4, 2011 at 2:18 AM, Gordon Sim <gs...@redhat.com> wrote:

> On 02/04/2011 05:56 AM, Damon Rolfs wrote:
>
>> I'm trying to write a simple Java client (using qpid-java-client-0.8) to
>> demonstrate communicating with a C++ QPID broker (AMQP 0.10). I would
>> appreciate any example code demonstrating a Java qpid client connecting to
>> a
>> C++ qpid broker. I've found several examples of Java JMS and native
>> clients
>> connecting to Java brokers, but none for Java ->  C++.
>>
>
> Have you tried the Drain/Spout examples? They are useful for experimenting
> with different types of addresses. There are equivalents in other languages
> also.
>
>
>  I've been working to adapt the pure Java examples, however I've run into
>> issues I've been unable to resolve. I've had best luck using the AMQ*
>> classes rather than the JMS classes. I've been able to connect to the
>> broker
>> and send a message into the exchange. The message gets dropped however
>> because my routing key is not set, despite my code setting it by
>> explicitly
>> calling the AMQDestination.setRoutingKey() method. When I print the
>> Destination, I get: JMS Destination: :////?routingkey=''.
>>
>> Using another (C++) client, I place a message for the Java client in a
>> queue. I have not been able to receive that message using the Java client.
>>
>> Thank you in advance for your guidance.
>>
>> Damon
>>
>> Here's a snippet from my client sample (I removed some of the log code
>> that
>> shows up below):
>>
>> AMQConnection connection = new AMQConnection( host, port, "root",
>> "r00tpw",
>> "10.1.3.3", "/" );
>> Session session = connection.createSession( true, Session.AUTO_ACKNOWLEDGE
>> );
>> AMQAnyDestination outboundQ = new AMQAnyDestination(
>> "ADDR:"+sendToExchange+"; {create: always}" );
>>
>
> That address will create a *queue* with whatever name the string
> sendToExchange contains. If you want to create an exchange you need to
> specify the node type. E.g. "my-exchange; {create: always, node:
> {type:topic}}".
>
> However my advice would not be to create your exchange on-demand as part of
> you application, but preconfigure the broker. This is simpler and less error
> prone in my opinion.
>
> E.g. get the management scripts and then run:
>
>   qpid-config add exchange topic my-exchange
>
>
>  outboundQ.setRoutingKey( new AMQShortString(routingKey) ); // routing key
>> is
>> a string: B.RRAA.AMQPApp2Key.Something
>>
>> AMQAnyDestination inboundQ = new AMQAnyDestination(
>> "ADDR:"+receivingQueue+"; {create: always}" );
>>
>
> You don't have the receiving queue bound to the exchange you are sending to
> here. (Though as above you aren't actually creating an exchange you are
> creating a queue).
>
> If you want this queue to exist independent of the lifecycle of the
> receiver then I would create and bind it using qpid-config rather than doing
> so on-demand.
>
> If however you simply want a subscription queue for the lifetime of the
> consumer, bound with a given routing-key, then you don't need to explicitly
> create that queue. Just create a consumer from an address that resolves to
> the exchange and specifies a subject containing the desired routing-key.
>
> There is a short description of addresses at
> http://qpid.apache.org/books/0.8/Programming-In-Apache-Qpid/html/ch02s04.htmlwhich might be worth a look if you have not done so already.
>
> I've attached a very simple example that I think demonstrates what you are
> attempting here. Hope that helps.
>
>
>  MessageProducer producer = session.createProducer( outboundQ );
>> MessageConsumer consumer = session.createConsumer( inboundQ );
>>
>> BytesMessage outboundMessage = createEmpMessage( session, empDataString );
>> // creates and populates a JMS BytesMessage
>> producer.send( outboundMessage );
>>
>> Message inboundMessage = consumer.receive( 10 * 1000 ); // I've also tried
>> setting up a listener to no avail
>> String msgType = (inboundMessage != null) ? inboundMessage.getJMSType :
>> "###NULL###";
>> Logger.info( "RECEIVED:type:"+msgType+"\
>> tcontent:"+inboundMessage );
>> connection.close();
>>
>
>
> ---------------------------------------------------------------------
> Apache Qpid - AMQP Messaging Implementation
> Project:      http://qpid.apache.org
> Use/Interact: mailto:users-subscribe@qpid.apache.org
>

Re: Examples of Java Client connecting to C++ Broker?

Posted by Rajith Attapattu <ra...@gmail.com>.
In addition to what Gordon mentioned, I strongly recommend to use the JMS
interfaces as much as possible.

1. You could use the jndi.properties file to abstract out the connections
2. If you need to dynamically create any queues I'd recommend using
session.createQueue() and session.createTopic() methods instead of using the
internal Qpid implementations.
3. If you have pre configured destinations you could define them in your
jndi.properties file as well.

Hope this helps.
If you have any further questions please feel free to ask them here.

Rajith

On Fri, Feb 4, 2011 at 5:18 AM, Gordon Sim <gs...@redhat.com> wrote:

> On 02/04/2011 05:56 AM, Damon Rolfs wrote:
>
>> I'm trying to write a simple Java client (using qpid-java-client-0.8) to
>> demonstrate communicating with a C++ QPID broker (AMQP 0.10). I would
>> appreciate any example code demonstrating a Java qpid client connecting to
>> a
>> C++ qpid broker. I've found several examples of Java JMS and native
>> clients
>> connecting to Java brokers, but none for Java ->  C++.
>>
>
> Have you tried the Drain/Spout examples? They are useful for experimenting
> with different types of addresses. There are equivalents in other languages
> also.
>
>
>  I've been working to adapt the pure Java examples, however I've run into
>> issues I've been unable to resolve. I've had best luck using the AMQ*
>> classes rather than the JMS classes. I've been able to connect to the
>> broker
>> and send a message into the exchange. The message gets dropped however
>> because my routing key is not set, despite my code setting it by
>> explicitly
>> calling the AMQDestination.setRoutingKey() method. When I print the
>> Destination, I get: JMS Destination: :////?routingkey=''.
>>
>> Using another (C++) client, I place a message for the Java client in a
>> queue. I have not been able to receive that message using the Java client.
>>
>> Thank you in advance for your guidance.
>>
>> Damon
>>
>> Here's a snippet from my client sample (I removed some of the log code
>> that
>> shows up below):
>>
>> AMQConnection connection = new AMQConnection( host, port, "root",
>> "r00tpw",
>> "10.1.3.3", "/" );
>> Session session = connection.createSession( true, Session.AUTO_ACKNOWLEDGE
>> );
>> AMQAnyDestination outboundQ = new AMQAnyDestination(
>> "ADDR:"+sendToExchange+"; {create: always}" );
>>
>
> That address will create a *queue* with whatever name the string
> sendToExchange contains. If you want to create an exchange you need to
> specify the node type. E.g. "my-exchange; {create: always, node:
> {type:topic}}".
>
> However my advice would not be to create your exchange on-demand as part of
> you application, but preconfigure the broker. This is simpler and less error
> prone in my opinion.
>
> E.g. get the management scripts and then run:
>
>   qpid-config add exchange topic my-exchange
>
>
>  outboundQ.setRoutingKey( new AMQShortString(routingKey) ); // routing key
>> is
>> a string: B.RRAA.AMQPApp2Key.Something
>>
>> AMQAnyDestination inboundQ = new AMQAnyDestination(
>> "ADDR:"+receivingQueue+"; {create: always}" );
>>
>
> You don't have the receiving queue bound to the exchange you are sending to
> here. (Though as above you aren't actually creating an exchange you are
> creating a queue).
>
> If you want this queue to exist independent of the lifecycle of the
> receiver then I would create and bind it using qpid-config rather than doing
> so on-demand.
>
> If however you simply want a subscription queue for the lifetime of the
> consumer, bound with a given routing-key, then you don't need to explicitly
> create that queue. Just create a consumer from an address that resolves to
> the exchange and specifies a subject containing the desired routing-key.
>
> There is a short description of addresses at
> http://qpid.apache.org/books/0.8/Programming-In-Apache-Qpid/html/ch02s04.htmlwhich might be worth a look if you have not done so already.
>
> I've attached a very simple example that I think demonstrates what you are
> attempting here. Hope that helps.
>
>
>  MessageProducer producer = session.createProducer( outboundQ );
>> MessageConsumer consumer = session.createConsumer( inboundQ );
>>
>> BytesMessage outboundMessage = createEmpMessage( session, empDataString );
>> // creates and populates a JMS BytesMessage
>> producer.send( outboundMessage );
>>
>> Message inboundMessage = consumer.receive( 10 * 1000 ); // I've also tried
>> setting up a listener to no avail
>> String msgType = (inboundMessage != null) ? inboundMessage.getJMSType :
>> "###NULL###";
>> Logger.info( "RECEIVED:type:"+msgType+"\
>> tcontent:"+inboundMessage );
>> connection.close();
>>
>
>
> ---------------------------------------------------------------------
> Apache Qpid - AMQP Messaging Implementation
> Project:      http://qpid.apache.org
> Use/Interact: mailto:users-subscribe@qpid.apache.org
>



-- 
Regards,

Rajith Attapattu
Red Hat
http://rajith.2rlabs.com/

Re: Examples of Java Client connecting to C++ Broker?

Posted by Gordon Sim <gs...@redhat.com>.
On 02/04/2011 05:56 AM, Damon Rolfs wrote:
> I'm trying to write a simple Java client (using qpid-java-client-0.8) to
> demonstrate communicating with a C++ QPID broker (AMQP 0.10). I would
> appreciate any example code demonstrating a Java qpid client connecting to a
> C++ qpid broker. I've found several examples of Java JMS and native clients
> connecting to Java brokers, but none for Java ->  C++.

Have you tried the Drain/Spout examples? They are useful for 
experimenting with different types of addresses. There are equivalents 
in other languages also.

> I've been working to adapt the pure Java examples, however I've run into
> issues I've been unable to resolve. I've had best luck using the AMQ*
> classes rather than the JMS classes. I've been able to connect to the broker
> and send a message into the exchange. The message gets dropped however
> because my routing key is not set, despite my code setting it by explicitly
> calling the AMQDestination.setRoutingKey() method. When I print the
> Destination, I get: JMS Destination: :////?routingkey=''.
>
> Using another (C++) client, I place a message for the Java client in a
> queue. I have not been able to receive that message using the Java client.
>
> Thank you in advance for your guidance.
>
> Damon
>
> Here's a snippet from my client sample (I removed some of the log code that
> shows up below):
>
> AMQConnection connection = new AMQConnection( host, port, "root", "r00tpw",
> "10.1.3.3", "/" );
> Session session = connection.createSession( true, Session.AUTO_ACKNOWLEDGE
> );
> AMQAnyDestination outboundQ = new AMQAnyDestination(
> "ADDR:"+sendToExchange+"; {create: always}" );

That address will create a *queue* with whatever name the string 
sendToExchange contains. If you want to create an exchange you need to 
specify the node type. E.g. "my-exchange; {create: always, node: 
{type:topic}}".

However my advice would not be to create your exchange on-demand as part 
of you application, but preconfigure the broker. This is simpler and 
less error prone in my opinion.

E.g. get the management scripts and then run:

    qpid-config add exchange topic my-exchange

> outboundQ.setRoutingKey( new AMQShortString(routingKey) ); // routing key is
> a string: B.RRAA.AMQPApp2Key.Something
>
> AMQAnyDestination inboundQ = new AMQAnyDestination(
> "ADDR:"+receivingQueue+"; {create: always}" );

You don't have the receiving queue bound to the exchange you are sending 
to here. (Though as above you aren't actually creating an exchange you 
are creating a queue).

If you want this queue to exist independent of the lifecycle of the 
receiver then I would create and bind it using qpid-config rather than 
doing so on-demand.

If however you simply want a subscription queue for the lifetime of the 
consumer, bound with a given routing-key, then you don't need to 
explicitly create that queue. Just create a consumer from an address 
that resolves to the exchange and specifies a subject containing the 
desired routing-key.

There is a short description of addresses at 
http://qpid.apache.org/books/0.8/Programming-In-Apache-Qpid/html/ch02s04.html 
which might be worth a look if you have not done so already.

I've attached a very simple example that I think demonstrates what you 
are attempting here. Hope that helps.

> MessageProducer producer = session.createProducer( outboundQ );
> MessageConsumer consumer = session.createConsumer( inboundQ );
>
> BytesMessage outboundMessage = createEmpMessage( session, empDataString );
> // creates and populates a JMS BytesMessage
> producer.send( outboundMessage );
>
> Message inboundMessage = consumer.receive( 10 * 1000 ); // I've also tried
> setting up a listener to no avail
> String msgType = (inboundMessage != null) ? inboundMessage.getJMSType :
> "###NULL###";
> Logger.info( "RECEIVED:type:"+msgType+"\
> tcontent:"+inboundMessage );
> connection.close();