how to specify queue in qpid with same exchange in JMS API? - jms

I am following example as
https://svn.apache.org/repos/asf/qpid/trunk/qpid/java/client/example/src/main/java/org/apache/qpid/example/Hello.java
Here they have one exchange and one queue. How to send message to different queue in same exchange ? How to specify routing information in message ?
I am using java / JMS api

You can use topic exchange to deliver messages.
Consumer specifies routing key to a outgoing message.
Producer binds a queue to a topic type exchange with a binding key. Of course, you can bind two or more queues to one topic exchange with same binding key.
When a message with a routing key arrives in broker, the topic exchange will deliver it according to the match between routing key and binding key.
The above is the basic principle from AMQP. The rest is to implement your code in Java way.

Related

What is the bus- queue in MassTransit?

When a consumer connects to a receive endpoint, a uniquely named exchange and queue is created such as:
bus-{MachineName}-{ApplicationName}-{Identifier}
What is the purpose of this queue / exchange? Are there any docs detailing this?
This a non-durable queue and exchange per endpoint, used to handle request-response. Responses are sent to that address, as well as faults.

Implementing Request/Reply Pattern with Spring and RabbitMQ with already existing queues

Let me start by describing the system. There are 2 applications, let's call them Client and Server. There are also 2 queues, request queue and reply queue. The Client publishes to the request queue, and the server listens for that request to process it. After the Server processes the message, it publishes it to the reply queue, which the Client is subscribed to. The Server application always publishes the reply to the predefined reply queue, not a queue that the Client application determines.
I cannot make updates to the Server application. I can only update the Client application. The queues are created and managed by the Server application.
I am trying to implement request/reply pattern from Client, such that the reply from the Server is synchronously returned. I am aware of the "sendAndReceive" approach with spring, and how it works with a temporary queue for reply purposes, and also with a fixed reply queue.
Spring AMQP - 3.1.9 Request/Reply Messaging
Here are the questions I have:
Can I utilize this approach with existing queues, which are managed and created by the Server application? If yes, please elaborate.
If my Client application is a scaled app (multiple instances of it are running at the same time), then how do I also implement it in such a way, that the wrong instance (one in which the request did not originate) does not read the reply from the queue?
Am I able to use the "Default" exchange to my advantage here, in addition to a routing key?
Thanks for your time and your responses.
Yes; simply use a Reply Listener Container wired into the RabbitTemplate.
IMPORTANT: the server must echo the correlationId message property set by the client, so that the reply can be correlated to the request in the client.
You can't. Unlike JMS, RabbitMQ has no notion of message selection; each consumer (in this case, reply container) needs its own queue. Otherwise, the instances will get random replies and it is possible (highly likely) that the reply will go to the wrong instance.
...it publishes it to the reply queue...
With RabbitMQ, publishers don't publish to queues, they publish to exchanges with a routing key. It is bad practice to tightly couple publishers to queues. If you can't change the server to publish the reply to an exchange, with a routing key that contains something from the request message (or use the replyTo property), you are out of luck.
Using the default exchange encourages the bad practice I mentioned in 2 (tightly coupling producers to queues). So, no, it doesn't help.
EDIT
If there's something in the reply that allows you to correlate it to a request; one possibility would be to add a delegating consumer on the server's reply queue. Receive the reply, perform the correlation, route the reply to the proper replyTo.

SpringAMQP RabbitMQ how to send directly to Queue without Exchange

I'm using SpringAMQP with Rabbit template. How to send messages directly to Queues omitting Exchange? How can i do it?
How can i do it?
You can't; publishers don't know about queues; just exchanges and routing keys.
However, all queues are bound to the default exchange ("") with the queue name as its routing key.
If you are using Spring AMQP's RabbitTemplate, it is configured to publish to the default exchange by default, so you can use
convertAndSend("myQueue", "foo")`
Or even...
template.setDefaultRoutingKey("myQueue");
then
template.convertAndSend("foo");
or
template.send(aMessage);

RabbitMQ topology created by MassTransit

I'am trying to understand the topology of queues and exchanges MT creates in RabbitMQ.
I cannot get these two statements:
we generate an exchange for each queue so that we can do direct sends
to the queue. it is bound as a fanout exchange (is it about sending vs publishing?)
control queues are exclusive and auto-delete - they go away when you
go away and are not shared.
What for does MT need to send direct messages? Does this relate to control queues used by MT internally?
There is also no mentioning about dead letter queue, does that imply MT does not support one out of the box?
Oops, looked in a wrong place. It's here.
Michael Aldworth has described this in details in his excellent MassTransit Send vs. Publish blog post.
Essentially, for each MT endpoint, you get a exchange-queue pair, which is named after the queueName parameter value in the endpoint configuration. Topics, at the other hand, are RabbitMQ exchanges named after the message contract full type name.
Publish
You send a message to the topic, meaning to the message type exchange. MassTransit creates binding between the message type exchange and queue-name exchange on start up. In such way subscriptions work on the RabbitMQ levels. Publisher never knows who will be receiving the published message, if anyone at all.
Send
When sending, however, you need to specify the receiver address. By doing this you instruct MassTransit to deliver the message directly to the queue-name exchange. There is no binding between message-type exchange and queue-name exchange involved here. Therefore, the message will be delivered even if there is no consumer for this type of message at the target service. In such case the message will be moved to the deadletter queue (queue-name_Skipped).

Can we use request/reply model in publish/subscribe messaging?

"Java Message Service" book by O'Reilly Media says:
use request/reply model in point-to-point messaging.
We can use message selectors in pub/sub messaging, so writing a request/reply model is as simple as writing a simple selector on reply topic:
publisher publishes a message with some unique property(such as UUID as correlationID)
subscriber responds for the message with the same UUID as correlationID
publisher(also subscriber of the reply topic) selects messages with the UUID sent.
Is this a wrong pattern?
Request/Reply messaging pattern is typically used for invoking a service hosted by service provider. Based on service request, a provider will reply with an appropriate response. So it's one-to-one. Here requestor and responder know each other.
In case of pub/sub, publisher and subscriber do not know each other. There could be a number of publisher publishing on a topic and there could be thousands of subscribers listening for that topic. So after receiving publication, if a subscriber replies to request using a topic, then that publication could go to a number of subscribers. Such a thing might flood the network.
In my opinion Request/Reply model must be used in P2P messaging and not Pub/Sub.

Resources