It depends on the nature of the topic:
if the topic is durable (has durable consumers subscribing to it), the broker will hold the messages in the topic until all the durable consumers consumes the messages.
if the topic is non-durable (no durable consumers), the message will not even be sent to the topic, as there will be no durable subscription.
For your example, I'll consider that you are using durable subscriptions / consumers:
Case 1:
- T-2 C1 and C2 make durable subscription to the topic
- T-1 C1 and C2 disconnect
- T0: M0 is posted
- T1: M1 is posted
- T2: C1 connects. C1 receives M0 and M1
- T3: M3 is posted. C1 receives M3
- T4: M4 is posted. C1 receives M4
- T5: C2 connects, C2 receives M0, M1, M2, M3, M4
That's because they are holding durable subscriptions
You need to be very careful when using durable topics / queues: if the consumer doesn't unsubscribe, the broker will hold the messages until the message store explodes. You will need to make sure it doesn't happen (by setting eviction policies and / or putting a Time to Live on the messages).
Of course the previous example will vary depending when the consumer does the durable subscription.
If you are using non-durable topics:
- T-2 C1 and C2 make normal subscription to the topic
- T-1 C1 and C2 disconnect
- T0: M0 is posted
- T1: M1 is posted
- T2: C1 connects. C1 does not receive anything
- T3: M3 is posted. C1 receives M3
- T4: M4 is posted. C1 receives M4
- T5: C2 connects, C2 does not receive anything