How do I detect the deletion of a queue and then re-declare it - spring-amqp

My application will create a queue which is auto-delete at startup. The application environment may lose its network and rabbitmq server will delete queue, therefore, after the network is restored, the listener will continue to report errors: the queue cannot be found. How do I detect this and recreate the queue?
How about using cachingConnectionFactory.setCloseExceptionLogger?

If you add a Queue #Bean and a RabbitAdmin #Bean, the admin will redeclare the queue each time a connection is opened. It registers itself as a ConnetionListener with the connection factory.
See the documentation.

Related

TIdSchedulerOfThreadDefault or TIdSchedulerOfThreadPool why use and what do

Should i use they with TIdTcpServer, and where they can improve something on my application?
i ask it because maybe they can improve some speed/agility action on TIdTcpServer since i use a Queue.
TIdTCPServer runs a thread for every client that is connected. Those threads are managed by the TIdScheduler that is assigned to the TIdTCPServer.Scheduler property. If you do not assign a scheduler of your own, a default TIdSchedulerOfThreadDefault is created internally for you.
The difference between TIdSchedulerOfThreadDefault and TIdSchedulerOfThreadPool is:
TIdSchedulerOfThreadDefault creates a new thread when a client connects, and then terminates that thread when the client disconnects.
TIdSchedulerOfThreadPool maintains a pool of idle threads. When a client connects, a thread is pulled out of the pool if one is available, otherwise a new thread is created. When the client disconnects, the thread is put back in the pool for reuse if the scheduler's PoolSize will not be exceeded, otherwise the thread is terminated.
From the OS's perspective, creating a new thread is an expensive operation. So in general, using a thread pool is usually preferred for better performance, but at the cost of using memory and resources for idle threads hanging around waiting to be used.
Whichever component you decide to use will not have much effect on how the server performs while processing active clients, only how it performs while handling socket connects/disconnects.

TIdCommandHandler: OnCommand wait terminate

I have a server using TIdCmdTCPServer and several clients. Those clients could send messages at the same time. When I simulate this (put 10 clients to send a command at the same time for example) the OnCommand event of TIdCommandHandler is called, but before it terminates, it's interrupted and it's called again by other client, and so on.
So, is there a way to configures OnCommand from TIdCommandHandler to finish the event before be called again?
Like most other servers in Indy, TIdCmdTCPServer is a multi-threaded component. Each connected client is managed by its own worker thread. The OnCommand event handler is called in the context of each thread that receives a client's command. It is thus possible for multiple OnCommand handlers to be running in parallel at the same time, so your event handler code must be written in a thread-safe manner. You should not try to serialize the events themselves, only serialize access to any shared resources that the events access across thread boundaries.

Spring AMQP Reporting

We are using spring AMQP to listen to rabbitMQ for messages. I want to be able to report the metrics once we finished processing batch of messages, that means when we exhausts all the message in the queue. I m not sure how to do that in Spring AMQP. browsing spring document, it mentions advice chain to SimpleRabbitListenerContainerFactory, but that's mainly for RetryInterceptor. is there anyway allow me to report?
There is nothing in the framework to notify the listener that there are no new messages available.
You could examine the queue using rabbitadmin to see a message count but that would be expensive to do it on every message delivery.
Some ideas:
You could schedule a task to run after some period when no messages are received (and cancel/reschedule each time a new message arrives).
You could have the sending system add a marker to the "last" message so the receiver knows the batch is complete.
Instead of using the message listener container, use RabbitTemplate.receive() (or receiveAndConvert()) which, by default, returns null when there are no messages in the queue. Call them in a loop until there are no messages. When that happens, issue your report, then go into a polling loop (with a sleep) to poll for the next "batch".

Spring AMQP advantages / disadvantages of using SimpleMessageListenerContainer over Receiving message from a queue

I would like to know the advantages and disadvantages of using SimpleMessageListenerContainer over receiving a message manually using Spring AMQP. Another question is when we create SimpleMessageListenerContainer setting a queue, does the rabbitmq calls the listeneradaptor or does SimpleMessageListenerContainer keeps polling the queue to check for messages and calls the registered adaptor when their is message.
It depends on your requirements; the listener container gives you an async (message-driven) approach. Otherwise, if you use the RabbitTemplatereceive methods, you are polling the queue. The container does not poll the queue, the broker pushes messages to the container according to the prefetch settings (default 1) - if using ackmode AUTO.

How to terminate windows service with blocking call using TcpListener

I have a windows service which runs a separate background thread. Inside the thread it starts a TCP server which listens to clients using TcpListener.
I'd like to know how I can close the service down gracefully when there is a blocking read like so:
listener.AcceptTcpClient();
I've found that apparently a windows service can abort any other threads as long as they are set-up as background threads, but what if one of the threads is blocking? Does this make a difference and if so, what is the best way to handle this situation?
Best way will be to call listener.Close() on service's stopping event. It will abort blocking call with SocketException.
State of the thread (blocked or running) does not affect the fact that thread is background. So if you call listener.AcceptTcpClient() from a background thread it will still be aborted when service stops,

Resources