All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.petals.se.ase.jms.ConsumerRunnable Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2011-2012 EBM WebSourcing, 2012-2023 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see http://www.gnu.org/licenses/
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.se.ase.jms;

import java.util.logging.Logger;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.ow2.petals.se.ase.EpConfiguration;
import org.ow2.petals.se.ase.listener.SessionAwareMessageListenerProxy;

/**
 * @author Pierre-Yves Gibello - EBM WebSourcing
 */
public class ConsumerRunnable implements Runnable {

	boolean running_ = false;
	String queue_;
	SessionAwareMessageListener jmsListener_;
	SessionAwareMessageListenerProxy sessionAwareListener_;
	Logger logger_;
	int stopTimeout_ = 0;

	Session session_;
	MessageConsumer consumer_;
	Destination destination_;
	
	public ConsumerRunnable(String queue, SessionAwareMessageListener jmsListener, EpConfiguration config)
	throws JMSException {
		queue_ = queue;
		jmsListener_ = jmsListener;
		logger_ = jmsListener.getLogger();
		
		JmsUtils jmsUtils = config.getJmsUtils();
		if(jmsUtils == null) jmsUtils = new JmsUtils(); // should not happen, but...
        session_ = jmsUtils.createJmsSession(true, Session.SESSION_TRANSACTED);
		sessionAwareListener_ = new SessionAwareMessageListenerProxy(session_, jmsListener_);
		stopTimeout_ = config.getInt(EpConfiguration.STOP_TIMEOUT);
	}

	public void shutdown() {
		try {
			stop();
			session_.close();
		} catch(Exception ignore) {
		} finally {
			session_ = null;
			running_ = false;
		}
	}
	
	public void stop() {
		if(stopTimeout_ > 0) {
			try {
				Thread.sleep(1000 * stopTimeout_);
			} catch (InterruptedException ignore) {
			}
		}
		running_ = false;
		try {
			consumer_.close();
		} catch (JMSException e) {
			if(logger_ != null) logger_.warning("SE-ASE-W0010: An error occurs stopping the sending process: [" + queue_ + " consumer thread] " + e);
		}
	}
	
	private void start() {
		try {
			if(destination_ == null) destination_ = session_.createQueue(queue_);
			consumer_ = session_.createConsumer(destination_);
			consumer_.setMessageListener(sessionAwareListener_);
		} catch (JMSException e) {
			if(logger_ != null) logger_.warning("SE-ASE-W0023: An error occurs starting the sending process: [" + queue_ + " consumer thread] " + e);
		}
		running_ = true;
	}

	@Override
	public void run() {
		start();
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
		
		while(running_) { // infinite loop...
			try {
				Thread.sleep(2000);
			} catch (InterruptedException ignore) {
			}
		}	
	}

	public boolean isRunning() {
		return running_;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy