/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.apache.jmeter.protocol.jms.sampler;

import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

import javax.jms.*;

/**
 * Receiver of pseudo-synchronous reply messages.
 * 
 * @author Martijn Blankestijn
 * @version $Id$.
 */
public class Receiver implements Runnable {
	private boolean active;

	private QueueSession session;

	private QueueReceiver consumer;

	private QueueConnection conn;

	// private static Receiver receiver;
	static Logger log = LoggingManager.getLoggerForClass();

	private Receiver(QueueConnectionFactory factory, Queue receiveQueue) throws JMSException {
		conn = factory.createQueueConnection();
		session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
		consumer = session.createReceiver(receiveQueue);
		if (log.isDebugEnabled()) {
			log.debug("Receiver - ctor. Starting connection now");
		}
		conn.start();
		if (log.isInfoEnabled()) {
			log.info("Receiver - ctor. Connection to messaging system established");
		}
	}

	public static synchronized Receiver createReceiver(QueueConnectionFactory factory, Queue receiveQueue)
			throws JMSException {
		// if (receiver == null) {
		Receiver receiver = new Receiver(factory, receiveQueue);
		Thread thread = new Thread(receiver);
		thread.start();
		// }
		return receiver;
	}

	public void run() {
		activate();
		Message reply;

		while (isActive()) {
			reply = null;
			try {
				reply = consumer.receive(5000);
				if (reply != null) {

					if (log.isDebugEnabled()) {
						log.debug("Received message, correlation id:" + reply.getJMSCorrelationID());
					}

					if (reply.getJMSCorrelationID() == null) {
						log.warn("Received message with correlation id null. Discarding message ...");
					} else {
						MessageAdmin.getAdmin().putReply(reply.getJMSCorrelationID(), reply);
					}
				}

			} catch (JMSException e1) {
				e1.printStackTrace();
			}
		}
		// not active anymore
		if (session != null) {
			try {
				session.close();
				if (conn != null)
					conn.close();
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}
	}

	public synchronized void activate() {
		active = true;
	}

	public synchronized void deactivate() {
		active = false;
	}

	private synchronized boolean isActive() {
		return active;
	}

}

@@ -1010,57 +1010,8 @@
  * %0A
- * @author Martijn Blankestijn%0A * @version $Id$.%0A
  */%0A
@@ -1217,22 +1217,36 @@
 eiver;%0A%09
-static
+private static final
  Logger 
@@ -1547,39 +1547,8 @@
 e);%0A
-%09%09if (log.isDebugEnabled()) %7B%0A%09
 %09%09lo
@@ -1600,20 +1600,16 @@
  now%22);%0A
-%09%09%7D%0A
 %09%09conn.s
@@ -1620,38 +1620,8 @@
 ();%0A
-%09%09if (log.isInfoEnabled()) %7B%0A%09
 %09%09lo
@@ -1691,20 +1691,16 @@
 shed%22);%0A
-%09%09%7D%0A
 %09%7D%0A%0A%09pub
@@ -2565,27 +2565,45 @@
 %09%09%09%09
-e1.printStackTrace(
+log.error(%22Error handling receive%22,e1
 );%0A%09
@@ -2689,16 +2689,27 @@
 lose();%0A
+%09%09%09%09try %7B%0A%09
 %09%09%09%09if (
@@ -2721,17 +2721,20 @@
 != null)
-%0A
+ %7B%0A%09
 %09%09%09%09%09con
@@ -2740,24 +2740,32 @@
 nn.close();%0A
+%09%09%09%09%09%7D%0A%09
 %09%09%09%7D catch (
@@ -2790,26 +2790,125 @@
 %09%09%09%09
-e.printStackTrace(
+%09log.error(%22Error closing connection%22,e);%0A%09%09%09%09%7D%0A%09%09%09%7D catch (JMSException e) %7B%0A%09%09%09%09log.error(%22Error closing session%22,e
 );%0A%09

Tidy Exception handling and logging