Retry to establish a JMS connection while ActiveMQ broker is not available - jms

Here is my scenario. I have few ActiveMQ (JBoss-AMQ) producers and consumers installed as services. In a server restart, what is the best practice of handling such a situation where a producer or a consumer service starts before the ActiveMQ broker service. In that case producer/client cannot establish a connection and starts to hang on as it is even after the broker service starts.
here's my code snippet of connection creation:
try {
connection = connectionFactory.createConnection();
connection.start();
LOGGER.info(STARTED_CONNECTION_WITH_THE_DESTINATION + destinationName);
session = createSession();
destination = session.createQueue(destinationName);
LOGGER.info(CREATED_QUEUE_IN_DESTINATION + destinationName);
if (isImageProcAgent) {
consumer = createConsumer();
LOGGER.info(CONSUMER_HAS_BEEN_INITIALIZED);
} else {
producer = session.createProducer(destination);
LOGGER.info(PRODUCER_HAS_BEEN_INITIALIZE);
}
} catch (MessagingException e) {
LOGGER.error(e);
} catch (JMSException e) {
LOGGER.error(e);
}
I'm new to JMS so appreciate your support.

This can be achieved by configuring a failover as this document explains.
according to my code snippet, the change I required it:
destination = session.createQueue("failover:"+destinationName);
producer = session.createProducer("failover:"+destination);

Related

javax.jms.JMSException: Duplicate durable subscription detected

I have a JMS application deployed as a Docker image in AWS Fargate. Two services are running for the task. However the problem is I am getting this:
2021-03-24 05:15:43.022 ERROR 1 --- [ main] com.hp.ext.cpq.pubsub.SnsTopicPublisher : Exception happened in readJmsTopicPublishToSnsTopic --->javax.jms.JMSException: Duplicate durable subscription detected
This is the code I am using to create the durable subscriber:
SnsTopicPublisher asyncSubscriber = this.ctx.getBean(SnsTopicPublisher.class);
if (prop.getProperty("tibco.msgSourceType").equalsIgnoreCase("TOPIC")) {
dest_t = session.createTopic(prop.getProperty("tibco.msgSource"));
**TopicSubscriber topicSubscriber = session.createDurableSubscriber(dest_t, "pfpDurable");**
topicSubscriber.setMessageListener(asyncSubscriber);
logger.debug("Set Jms Topic Listener ---> asyncSubscriber");
}
if (prop.getProperty("tibco.msgSourceType").equalsIgnoreCase("QUEUE")) {
dest_q = session.createQueue(prop.getProperty("tibco.msgSource"));
MessageConsumer msgConsumer_p = session.createConsumer(dest_q);
msgConsumer_p.setMessageListener(asyncSubscriber);
logger.debug("Set Jms Queue Listener ---> asyncSubscriber");
}
I am getting the error for the marked line from AWS cloud watch logs.
Most likely you have a connection (and generally other JMS objects) leak. When the exception is thrown, you need to close resources in a finally {} block, similar to the JDBC pattern.
Also, you might want to look at using a Pooled Connection. This allows for open+close pattern on JMS connections without really closing connections to the server. Check out the activemq-jms-pool which is a JMS-standard pool (not ActiveMQ specific) that works with most JMS brokers, including Tibco and IBM MQ.
Connection connection = null;
Session session = null;
MessageConsumer messageConsumer = null;
try {
connection = connectionFactory.createConnection();
connection.start();
.. do some JMS
} catch (JMSException e) {
// handle errors
} finally {
if (messageConsumer != null) {
try { messageConsumer.close(); } catch (JMSException e) { logger.error("Error closing MessagingConsumer", e);
}
if (session != null) {
try { session.close(); } catch (JMSException e) { logger.error("Error closing Session", e);
}
if (connection != null) {
try { connection.close(); } catch (JMSException e) { logger.error("Error closing Connection", e);
}
}
Note: The JMS specification only allows 1 durable subscription per topic using the API you have in the code. The JMS v2.0 allows for a Shared Durable Subscription to support multiple consumers.

JMS connection pooling with IBM MQ Client

We are using MQIPT 9.2 between our IBM MQ 9.x server and IBM MQ clients. We're also using the IBM MQ client jar in Java to connect to the queue manager to push and receive messages which is working fine. However connection creation is taking time and every time it will take time if we create connection just in time.
How we can implement JMS connection pooling for IBM MQ?
The following depicts our connectivity:
[][1
Is there any standard way so that we can implement connection pooling?
Used Code below
System.out.println("<<<<<<<<<Starting test for push messages>>>>>>>>>>");
try {
// Create a keystore object for the truststore
KeyStore trustStore = KeyStore.getInstance("JKS");
char[] keyPassphrase = "*******".toCharArray();
trustStore.load(new FileInputStream(
"JKS File path"),
keyPassphrase);
TrustManagerFactory trustManagerFactory = TrustManagerFactory
.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
System.out.println("SSL certificates loaded in message sending");
// Create default MQ connection factory
MQQueueConnectionFactory factory = new MQQueueConnectionFactory();
factory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
factory.setQueueManager(QMGRNAME);
factory.setHostName(HOSTNAME);
factory.setChannel(CHANNEL);
factory.setPort(1414);
factory.setSSLFipsRequired(false);
factory.setSSLSocketFactory(sslSocketFactory);
factory.setClientReconnectTimeout(100);
factory.setStringProperty(WMQConstants.USERID, user);
factory.setStringProperty(WMQConstants.PASSWORD, password);
factory.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true);
factory.setStringProperty(WMQConstants.WMQ_SSL_CIPHER_SUITE, "cipher suite");
mqConnection = (MQQueueConnection) factory.createQueueConnection();
MQQueueSession session = (MQQueueSession) mqConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
// Start the connection
System.out.println("Connection starting while sending message");
mqConnection.start();
System.out.println("Connection started while sending message");
for (int i = 0; i <50; i++) {
System.out.println("Preparing message before sending");
long uniqueNumber = System.currentTimeMillis() % 1000;
JMSTextMessage message = (JMSTextMessage) session
.createTextMessage("SimplePTP - msg" + uniqueNumber);
System.out.println("message prepared while sending , text: " + message.getText());
Destination destination = session.createQueue(destinationName);
MQMessageProducer producer = (MQMessageProducer) session.createProducer(destination);
// And, send the message
producer.send(message);
System.out.println("Sent message****************:\n" + message);
}
/*
* if (connection != null) { System.out.
* println("*************connection closing after message sent********************"
* ); connection.close(); System.out.
* println("*************connection closed after message sent********************"
* ); }
*/
System.out.println("<<<<<<<<<<Test ended>>>>>>>>>>>>");
} catch (JMSException j) {
j.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("finally block after message sent************ ");
if (mqConnection != null) {
try {
mqConnection.close();
System.out.println("connection closed after message sent in finally block\n");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("<<<<<<<<<<Test ended from finally >>>>>>>>>>>>");
}
When using the above code connection creation takes time and it's creating and closing a connection for each messsage. This is a bad practice so I created a list and added connection into it which works well. However, I want to use a proper connection pool instead.
You can use:
<bean class="org.apache.activemq.jms.pool.PooledConnectionFactory"
id="source.pooledConnectionFactory" primary="true">
<property name="maxConnections" value="1"/>
<property name="idleTimeout" value="0"/>
<property name="connectionFactory" ref="factory"/>
</bean>
(sorry for the XML when you posted Java DSL, but you get the idea). Basically, wrap your connection factory with the ActiveMQ JMS pooled connection factory.
Alternatively, you can use:
<dependency>
<groupId>org.messaginghub</groupId>
<artifactId>pooled-jms</artifactId>
<version>1.1.0</version>
</dependency>
JmsPoolConnectionFactory pooledCF = new JmsPoolConnectionFactory();
pooledCF.setConnectionFactory(connectionFactory());
pooledCF.setMaxConnections(1);
The org.messaginghub project is a branch of the ActiveMQ code and has no ActiveMQ dependencies.

MQ MessageConsumer does not respond to receive() method

I have a java program I run to write messages to Mid-Tier IBM MQ's to test functionality before attaching our main programs to them. The write method looks like the following below:
private static void sendSingleMessage(ConnectionFactory connectionFactory,
String[] messages, String destination) throws Exception {
Connection connection = null;
try {
connection = connectionFactory.createConnection();
for (String payload : messages) {
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(destination);
MessageProducer producer = session.createProducer(queue);
Message msg = session.createTextMessage(payload);
System.out.println("Sending text '" + payload + "'");
producer.send(msg);
session.close();
System.out.println("Message sent");
}
} finally {
if (connection != null) {
connection.close();
}
}
}
The connectionFactory is setup before this method executes, but within that method I set the MQConncetionFactory properties(host,port,channel,queuemanager, etc...) This send method works and I can see the queue depth increasing on my IBM MQ Explorer when I call it from my main method.
When I run a similar readSingleMessage method, the code gets stuck on the consumer.receive() and never finishes executing. See below:
private static void readSingleMessage(ConnectionFactory connectionFactory,
String[] messages, String destination) throws Exception {
Connection connection = null;
try {
connection = connectionFactory.createConnection();
for (String payload : messages) {
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(destination);
MessageConsumer consumer = session.createConsumer(queue);
System.out.println("Recieving text '" + payload + "'");
consumer.receive();
session.close();
System.out.println("Received message");
}
} finally {
if (connection != null) {
connection.close();
}
}
}
Is there anyway I can further debug this, or find why I am able to write to the queue, but unable to read a message off of it?
You have to start the JMS Connection by calling the start() method on it. You cannot receive any messages until the connection is started. This is noted in the JMS Specification and Javadoc.
As an aside, if you use the JMS 2.0 "simplified" API and create a JMSContext object (an object which is essentially a combined Connection and Session) you do not need to call start to receive messages. A consumer crated from it can be used to receive messages without being explicitly started.

Spring JMS Template Sync Receive on Non-Durable Subscriber Message Loss

1. Background
We are evaluating Spring JMS and testing out the JMSTemplate for various scenarios - queues, topics (durable, non-durable).
We experienced message loss for non-durable topic subscribers and would like to seek clarifications here.
2. Problem Statement
a) We wrote a standalone java program that would call the JMSTemplate.receive method every n secs to receive messages synchronously from a non-durable topic**.
b) We noticed that there is always message loss after the 1st invocation of the JMSTemplate.receive method. This was due to the JMSTemplate.receive method stopping the connection when it reaches ConnectionFactoryUtils.releaseConnection(...).
JMSTemplate:
public <T> T execute(SessionCallback<T> action, boolean startConnection) throws JmsException
{
Assert.notNull(action, "Callback object must not be null");
Connection conToClose = null;
Session sessionToClose = null;
try {
Session sessionToUse = ConnectionFactoryUtils.doGetTransactionalSession(
getConnectionFactory(), this.transactionalResourceFactory, startConnection);
if (sessionToUse == null) {
conToClose = createConnection();
sessionToClose = createSession(conToClose);
if (startConnection) {
conToClose.start();
}
sessionToUse = sessionToClose;
}
if (logger.isDebugEnabled()) {
logger.debug("Executing callback on JMS Session: " + sessionToUse);
}
return action.doInJms(sessionToUse);
}
catch (JMSException ex) {
throw convertJmsAccessException(ex);
}
finally {
JmsUtils.closeSession(sessionToClose);
ConnectionFactoryUtils.releaseConnection(conToClose, getConnectionFactory(), startConnection); // the connection is stopped here
}
}
ConnectionFactoryUtils.releaseConnection(...):
public static void releaseConnection(Connection con, ConnectionFactory cf, boolean started) {
if (con == null) {
return;
}
if (started && cf instanceof SmartConnectionFactory && ((SmartConnectionFactory) cf).shouldStop(con)) {
try {
con.stop(); // connection was stopped here
}
catch (Throwable ex) {
logger.debug("Could not stop JMS Connection before closing it", ex);
}
}
try {
con.close();
}
catch (Throwable ex) {
logger.debug("Could not close JMS Connection", ex);
}
3. Validation with Spring Documentation
The Spring JMS documentation advised to use pooled connections, so we made sure we did.
Our java program is obtaining the JMS Connection Factories from WLS JMS and MQ JMS (LDAP) Providers and decorated with SingleConnectionFactory and CachingConnectionFactory in respective test cases.
This is what we observed during testing:
a) SingleConnectionFactory - Connection was stopped (Consumer/Session were closed as well).
b) CachingConnectionFactory - Connection was also stopped (although Consumer/Session were cached and not closed)
4. Questions:
a) Has anybody hit the same issue as us?
b) Would you consider this as a defect of Spring JMS for the use case of Non-Durable Subscriptions?
c) We are considering customizing a CachingConnectionFactory that won't stop the connection. Any downsides?
Note: We are aware that Async MessageListeners like DMLC/SMLC and Sync Durable Topic Subscribers using JMSTemplate would not have this issue. We just wish to clarify for Sync Non-Durable Topic Subscribers using JMSTemplate.
Would greatly appreciate any comments and thoughts.
Thanks!
Victor

How to configure jms on WebSphere MQ using java

I'm trying to configure Jms and WebSphere using java and using Jboss 6.3 in remote system.But am getting ClassNotFoundException in creation of MQQueueConnection Class.Here I please fine code.
Actually M not getting proper steps that what to do,I took help from IBM Knowledge Center but that is not helpful for me.
Please anyone who knows about it guide me and for below code Which jar files is required?
try {
MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
// Config
cf.setHostName("167.190.249.202");
cf.setPort(1422);
cf.setTransportType(WMQConstants.WMQ_CM_CLIENT);
cf.setQueueManager("QM.EMPIRE");
cf.setChannel("EMPIRE.CONN");
MQQueueConnection connection = (MQQueueConnection) cf.createQueueConnection();
MQQueueSession session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
MQQueue queue = (MQQueue) session.createQueue("queue:///Q1");
MQQueueSender sender = (MQQueueSender) session.createSender(queue);
MQQueueReceiver receiver = (MQQueueReceiver) session.createReceiver(queue);
long uniqueNumber = System.currentTimeMillis() % 1000;
JMSTextMessage message = (JMSTextMessage) session.createTextMessage("SimplePTP "+ uniqueNumber);
// Start the connection
connection.start();
sender.send(message);
System.out.println("Sent message:\\n" + message);
JMSMessage receivedMessage = (JMSMessage) receiver.receive(10000);
System.out.println("\\nReceived message:\\n" + receivedMessage);
sender.close();
receiver.close();
session.close();
connection.close();
System.out.println("\\nSUCCESS\\n");
}
catch (JMSException jmsex) {
System.out.println(jmsex);
System.out.println("\\nFAILURE\\n");
}
catch (Exception ex) {
System.out.println(ex);
System.out.println("\\nFAILURE\\n");
}
}
}
It is far better to point your CLASSPATH to where the MQ JAR files are installed rather than copy the MQ JAR files (i.e. you won't get the 'ClassNotFoundException' error).
But if you do copy the MQ JAR files then for an MQ JMS application, you pretty much need all of them:
com.ibm.mq.jar
com.ibm.mq.commonservices.jar
com.ibm.mq.headers.jar
com.ibm.mq.jmqi.jar
com.ibm.mq.pcf.jar
com.ibm.mqjms.jar
connector.jar
fscontext.jar
jms.jar
jndi.jar
jta.jar
ldap.jar
providerutil.jar

Resources