I am using Spring Integration to produce messages to a JMS topic on Red Hat AMQ 7.8.2. It is taking 3 to 4 seconds to send the first message on application startup/restart and it does take 40 to 50 ms for messages followed by first message. It might be taking 3 to 4 seconds to create the connection factory & establish the connection with broker for the first message.
<si-jms:outbound-channel-adapter id="communication"
connection-factory="connectionFactoryCached"
channel="communicationChannel"
destination-name="${communications.topic}"
pub-sub-domain="true" />
<bean id="connectionFactory"
class="javax.jms.ConnectionFactory"
factory-bean="simpleAmqConnectionFactory"
factory-method="createConnectionFactory" />
<bean id="connectionFactoryCached"
class="org.springframework.jms.connection.CachingConnectionFactory"
p:targetConnectionFactory-ref="connectionFactory"
p:sessionCacheSize="3" />
When communication happens for the first time there are a lot of things happening that are likely the causes:
Creation of a connection: The very first time a message is sent a connection is created with AMQ broker. This has quite something such as network communication, authentication. session setup etc, this usually takes some time.
Resource management: Very first message, Spring integration needs to allocate and then afterwards configure the resources such as
ConnectionFactory,Connection,Session,MessageProducer. This is usually a single time cost that isn't gonna happen going forward for messages coming later.Other factors:
Now to the solution: Adjust the connection so that all the JMS connection related resources are setup during the startup of the application. When you startup the application to establish the connection, just send a dummy message to dummy topic to enable the connection process being done during the startup.
Maybe use connection pooling. This allows to reuse the connection instead of creating new one each time.
If you can, send async messages so that your app doesn't have to wait for the connection.