I am working with Spring WebSocket and Stomp.
Note: the broker is ActiveMQ
I have two #Schedule methods that sends messages to Queue and Topic respectively
For a subscription for a Queue, the code is as follows:
$('#ws_connect').click(function(){
console.log('Connect clicked');
var socket = new SockJS('/project-app/ws/notification');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
console.log('Connected: ' + frame);
$('#notification').append('<p>Connected</p>');
stompClient.subscribe('/queue/somedestination', function (notification) {
... append the content to the html page, it works
});
});
});
If there is no user the Queue is getting message by message. Once the user arrives and do the connection, automatically it gets all the messages enqueued and the Queue pass to be empty. If a new message arrives it appears automatically, it because the user remains connected yet. If the user disconnects and later do the connection, it can see again all the messages enqueued. Until here all is Ok
As follows for a subscription for a Topic
$('#ws_connect').click(function(){
console.log('Connect clicked');
var socket = new SockJS('/project-app/ws/notification');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
console.log('Connected: ' + frame);
$('#notification').append('<p>Connected</p>');
stompClient.subscribe('/topic/somedestination', function (notification) {
... append the content to the html page, it works
});
});
});
Practically the js code is the same than the Queue version. Just the destination is different.
Here the problem is that if exists messages enqueued in the Topic and if the user does the connection the messages do not appear automatically. Of course meanwhile the user remains connected he can see each new message, same case if multiple users are connected to the same Topic all can see the same new message.
But again for the first user to connect to the Topic destination. He is not able to get the messages enqueued from that Topic when he does the connection.
Is it the normal behaviour?
How can be retrieved the messages enqueued?
Perhaps a suggested approach to handle this scenario?
In some way the "worst" scenario would be create multiple Queues for each potential user. But just curious if through Topic is possible get this requeriment
The Topic destination does not store messages when there are no subscribers so the code is working as expected. The only time a Topic would retain messages for subscribers is if the subscriber had created a durable topic subscription and then gone offline at which point the broker would store any message sent to the Topic with the persistent flag enabled. The caveat here being that any message sent to that Topic before the subscription is made would be dropped.
See the documentation for more help.
Related
I'm trying to send delayed messages on RabbitMQ with Spring AMQP.
I'm defining MessageProperties like this:
MessageProperties delayedMessageProperties = new MessageProperties();
delayedMessageProperties.setDelay(45000);
I'm defining the message which should be send in delay time like this:
org.springframework.amqp.core.Message amqpDelayedMessage = org.springframework.amqp.core.MessageBuilder.withBody(objectMapper.writeValueAsString(reversalMessage).getBytes())
.andProperties(reversalMessageProperties).build();
And then, If I send this message with RabbitTemplate, there is no problem. Message is being sent in defined delay time.
rabbitTemplate.convertSendAndReceiveAsType("delay-exchange",delayQueue, amqpDelayedMessage, new ParameterizedTypeReference<org.springframework.amqp.core.Message>() {
});
But I need to send this message asynchronously because I need not to block any other message in the system and to get more performance and if I use asyncRabbitTemplate, message is being delivered immediately. There is no delay.
asyncRabbitTemplate.convertSendAndReceiveAsType("delay-exchange",delayQueue, amqpDelayedMessage, new ParameterizedTypeReference<org.springframework.amqp.core.Message>() {
});
How can I obtain the delay with asnycRabbitTemplate?
This is probably a bug; please open an issue on GitHub.
The convertSendAndReceive() methods are not intended to send and receive raw Message objects.
In the case of the RabbitTemplate the conversion is skipped if the object is already a Message; there are some cases where this skip is not performed with the async template; please edit the question to show your template configuration.
However, since you are dealing with Message directly, don't use the convert... methods at all, simply use
public RabbitMessageFuture sendAndReceive(String exchange, String routingKey, Message message) {
How to connect an endpoint(exchange-exchange-queue) in masstransit, accumulate data in the queue, and then, after some time, connect a consumer to this endpoint?
I wanted to do something like:
Task.Run(async () =>
{
for (var i = 0;; i++)
{
await _bus.Publish(new Event(i), stoppingToken);
await Task.Delay(1_000, stoppingToken);
}
});
// a command comes to connect the consumer
var endpoint = _bus.ConnectReceiveEndpoint();
await endpoint.Ready;
// I prepare the consumer, as soon as it is ready, I connect it,
then I read the data that has accumulated during the preparation
(my consumer needs to load the state before reading the data,
and also cannot skip the data during its preparation)
endpoint.ReceiveEndpoint.ConnectConsumer(() =>
_serviceProvider.GetRequiredService<EventConsumer>());
but this code will not create an exchange-exchange relationship, so the queue will be empty
If the exchange bindings (wired to the receive endpoint, which ultimately is a queue) do not exist when messages are published, they are discarded by RabbitMQ.
You would need to connect the receive endpoint in advance, so that the messages end up in the queue.
When a subscriber of a topic throws an exception non-handled message lands in {subscribername}_error queue.
Given the example:
const string subsriberName = "AnotherSubscriber";
cfg.SubscriptionEndpoint<AnotherThingHappened>(host, subsriberName, configurator =>
{
configurator.Handler<AnotherThingHappened>(context =>
{
Console.WriteLine(context.Message.AnotherThingType);
if (Random.NextDouble() < 0.1)
{
throw new Exception("Oups, I failed :(");
}
return Task.CompletedTask;
});
});
It created "AnotherSubscriber" subscription on topic ObjectCreatedA. But when it fails the message goes to the queue anothersubscriber_error. It makes it harder to diagnose, monitor and replay messages. Because from ASB perspective this is just an ordinary queue.
How do I route failures to the DLQ of topic ObjectCratedA/AnotherSubscriber instead of **_error one?
Thanks in advance.
This is now possible as of MassTransit 6.2, see the related GitHub issue.
Your configuration will now need to look something like:
cfg.SubscriptionEndpoint(
"my-subscription",
"my-topic",
e =>
{
e.ConfigureConsumer<MyConsumer>(provider);
// Send failures to built-in Azure Service Bus Dead Letter queue
e.ConfigureDeadLetterQueueDeadLetterTransport();
e.ConfigureDeadLetterQueueErrorTransport();
});
I was wondering if there is a way to raise events on new subscription to a publisher socket
For example:
PublisherSocket publisher = new PublisherSocket();
publisher.Bind("tcp://*:5555");
NetMQPoller poller = new NetMQPoller { publisher };
poller.RunAsync();
poller.OnSubscription += topic =>
{
AddToPool(topic);
};
Task.Factory.StartNew(() =>
{
SubscriberSocket sub = new SubscriberSocket();
sub.Connect("tcp://127.0.0.1:5555");
Thread.Sleep(1000);
sub.Subscribe("A");
}, TaskCreationOptions.LongRunning);
publisher.ReceiveReady += Publisher_ReceiveReady;
Of course, OnSubscription doesn't really exist, but I was wondering if there is any way around it.
I need my server to be aware of all the subscriptions.
I though about two ways to implement it:
Create an additional router socket in the server, all subscriptions will be sent both to the publisher socket and to the router socket (unsubscriptions as well). This will allow me to poll the subscriptions from the router.
Not use publisher/subscriber at all, create all the pubsub mechanism with router/dealer.
What would you suggest me do?
If you use an XPUB rather than PUB socket you can receive the subscription messages as you would regular messages on any other socket type.
everyone. I have an HTTP API for posting messages in a RabbitMQ broker and I need to implement the request-response pattern in order to receive the responses from the server. So I am something like a bridge between the clients and the server. I push the messages to the broker with specific routing-key and there is a Consumer for that messages, which is publishing back massages as response and my API must consume the response for every request. So the diagram is something like this:
So what I do is the following- For every HTTP session I create a temporary responseQueue(which is bound to the default exchange, with routing key the name of that queue), after that I set the replyTo header of the message to be the name of the response queue(where I will wait for the response) and also set the template replyQueue to that queue. Here is my code:
public void sendMessage(AbstractEvent objectToSend, final String routingKey) {
final Queue responseQueue = rabbitAdmin.declareQueue();
byte[] messageAsBytes = null;
try {
messageAsBytes = new ObjectMapper().writeValueAsBytes(objectToSend);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
MessageProperties properties = new MessageProperties();
properties.setHeader("ContentType", MessageBodyFormat.JSON);
properties.setReplyTo(responseQueue.getName());
requestTemplate.setReplyQueue(responseQueue);
Message message = new Message(messageAsBytes, properties);
Message receivedMessage = (Message)requestTemplate.convertSendAndReceive(routingKey, message);
}
So what is the problem: The message is sent, after that it is consumed by the Consumer and its response is correctly sent to the right queue, but for some reason it is not taken back in the convertSendAndReceived method and after the set timeout my receivedMessage is null. So I tried to do several things- I started to inspect the spring code(by the way it's a real nightmare to do that) and saw that is I don't declare the response queue it creates a temporal for me, and the replyTo header is set to the name of the queue(the same what I do). The result was the same- the receivedMessage is still null. After that I decided to use another template which uses the default exchange, because the responseQueue is bound to that exchange:
requestTemplate.send(routingKey, message);
Message receivedMessage = receivingTemplate.receive(responseQueue.getName());
The result was the same- the responseMessage is still null.
The versions of the amqp and rabbit are respectively 1.2.1 and 1.2.0. So I am sure that I miss something, but I don't know what is it, so if someone can help me I would be extremely grateful.
1> It's strange that RabbitTemplate uses doSendAndReceiveWithFixed if you provide the requestTemplate.setReplyQueue(responseQueue). Looks like it is false in your explanation.
2> To make it worked with fixed ReplyQueue you should configure a reply ListenerContainer:
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(rabbitConnectionFactory);
container.setQueues(responseQueue);
container.setMessageListener(requestTemplate);
3> But the most important part here is around correlation. The RabbitTemplate.sendAndReceive populates correlationId message property, but the consumer side has to get deal with it, too: it's not enough just to send reply to the responseQueue, the reply message should has the same correlationId property. See here: how to send response from consumer to producer to the particular request using Spring AMQP?
BTW there is no reason to populate the Message manually: You can just simply support Jackson2JsonMessageConverter to the RabbitTemplate and it will convert your objectToSend to the JSON bytes automatically with appropriate headers.