/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2009 Danet GmbH (www.danet.de), PBL BII.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: $
*/
package de.danet.an.workflow.ejbs.util;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;
import javax.ejb.EJBException;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TopicConnection;
import javax.jms.TopicSession;
import de.danet.an.util.EJBUtil;
import de.danet.an.util.ResourceNotAvailableException;
import de.danet.an.workflow.domain.DefaultAuditEvent;
/**
* This class provides helpers for handling queues.
*
* @author mnl
*
*/
public class QueuerUtils {
private static final org.apache.commons.logging.Log logger
= org.apache.commons.logging.LogFactory
.getLog(QueuerUtils.class);
/**
* Queue the given event.
*
* @param evt the <code>WfAuditEvent</code>.
*/
public static void queue
(QueueConnectionFactory qcf, Queue queue, DefaultAuditEvent evt) {
QueueConnection qc = null;
QueueSession qs = null;
try {
qc = qcf.createQueueConnection();
qs = qc.createQueueSession (true, 0);
QueueSender snd = qs.createSender (queue);
snd.setDisableMessageID (true);
snd.setDisableMessageTimestamp (true);
Map args = new HashMap();
args.put("event", evt.replaceSource(null));
ObjectMessage msg = qs.createObjectMessage();
msg.setObject ((Serializable)args);
snd.send (msg);
snd.close ();
if (logger.isDebugEnabled()) {
logger.debug ("Queued with JMS: " + evt.toString());
}
} catch (JMSException e) {
throw new EJBException (e);
} finally {
release(qc, qs);
}
}
/**
* Queue the given event with a requeued count.
*
* @param evt the <code>WfAuditEvent</code>
* @param requeued the number of times this has been queued
*/
public static void queue (QueueConnectionFactory qcf, Queue queue,
DefaultAuditEvent evt, int requeued) {
QueueConnection qc = null;
QueueSession qs = null;
try {
QueueSender snd = qs.createSender (queue);
snd.setDisableMessageID (true);
snd.setDisableMessageTimestamp (true);
Map args = new HashMap();
args.put("event", evt.replaceSource(null));
ObjectMessage msg = qs.createObjectMessage();
msg.setIntProperty("requeuedCount", requeued);
msg.setObject ((Serializable)args);
snd.send (msg);
snd.close ();
if (logger.isDebugEnabled()) {
logger.debug ("Queued with JMS: " + evt.toString());
}
} catch (JMSException e) {
throw new EJBException (e);
} finally {
release(qc, qs);
}
}
/**
* Close the resources required to access a queue.
*
* @param qc the queue connection
* @param qs the queue session
*/
public static void release(QueueConnection qc, QueueSession qs) {
if (qs != null) {
try {
qs.close();
} catch (JMSException e) {
logger.warn("Cannot close QueueSession (ignored): "
+ e.getMessage(), e);
}
}
if (qc != null) {
try {
qc.close();
} catch (JMSException e) {
logger.warn("Cannot close QueueConnection (ignored): "
+ e.getMessage(), e);
}
}
}
/**
* Close the resources required to access a topic.
*
* @param tc the topic connection
* @param ts the topic session
*/
public static void release(TopicConnection tc, TopicSession ts) {
if (ts != null) {
try {
ts.close();
} catch (JMSException e) {
logger.warn("Cannot close TopicSession (ignored): "
+ e.getMessage(), e);
}
}
if (tc != null) {
try {
tc.close();
} catch (JMSException e) {
logger.warn("Cannot close TopicConnection (ignored): "
+ e.getMessage(), e);
}
}
}
}