After I went to multiple sites and learned JMS I wrote a JMS standalone client to read messages from a database and send them one by one. I also want to receive message one by one message and then update the database.
I need to send a message to a queue and the other application using standard JMS which will consume a TextMessage and whose body will be read as an ISO-8859-1 string. Also they will similarly send reply as a TextMessage.
I wrote a for loop to read the message one by one from the DB.
I am new to JMS so could you please correct me whether my below code works properly to read and send messages to a queue and receive and update the DB. Is there any thing I need to change in the JMS Type or any thing I need to correct. Does the for loop work fine?
/*MQ Configuration*/
MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
mqQueueConnectionFactory.setHostName(url);
mqQueueConnectionFactory.setChannel(channel);//communications link
mqQueueConnectionFactory.setPort(port);
mqQueueConnectionFactory.setQueueManager(qmgr);//service provider 
mqQueueConnectionFactory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
/*Create Connection */
QueueConnection queueConnection = mqQueueConnectionFactory.createQueueConnection();
queueConnection.start();
/*Create session */
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
/*Create response queue */
//   Queue queue = queueSession.createQueue("QUEUE.RESPONSE");
int messageCount = 0;
Queue queue = queueSession.createQueue(replytoQueueName);
QueueSender queueSender = null;
QueueReceiver queueReceiver=null;
for (Testbean testBean : testbeanList) {
    String testMessage = testBean.getMessage();                 
    /*Create text message */
    textMessage = queueSession.createTextMessage(testMessage);
    logger.info("Text messages sent: " + messageCount);
    textMessage.setJMSReplyTo(queue);
    textMessage.setJMSType("mcd://xmlns");//message type
    textMessage.setJMSExpiration(2*1000);//message expiration
    textMessage.setJMSDeliveryMode(DeliveryMode.PERSISTENT); //message delivery mode either persistent or non-persistemnt
    /*Create sender queue */
    //  QueueSender queueSender = queueSession.createSender(queueSession.createQueue("QUEUE.REQEST"));
    queueSender = queueSession.createSender(queueSession.createQueue(outputQName));
    queueSender.setTimeToLive(2*1000);
    queueSender.send(textMessage);
    /*After sending a message we get message id */
    System.out.println("after sending a message we get message id "+ textMessage.getJMSMessageID());
    String jmsCorrelationID = " JMSCorrelationID = '" + textMessage.getJMSMessageID() + "'";
    /*Within the session we have to create queue reciver */
    queueReceiver = queueSession.createReceiver(queue,jmsCorrelationID);
    /*Receive the message from*/
    Message message = queueReceiver.receive(60*1000);
    //   String responseMsg = ((TextMessage) message).getText();
    byte[] by = ((TextMessage) message).getText().getBytes("ISO-8859-1");
    logger.info(new String(by));
    String responseMsg = new String(by,"UTF-8");
    testDAO rmdao = new testDAO();
    rmdao.updateTest(responseMsg, jmsCorrelationID);        
    messageCount += 1;
}
queueSender.close();
queueReceiver.close();
queueSession.close();
queueConnection.close();
 
     
    