package org.jboss.resource.adapter.jms.inflow.dlq;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TopicConnectionFactory;
import javax.naming.Context;
import org.jboss.jms.jndi.JMSProviderAdapter;
import org.jboss.logging.Logger;
import org.jboss.naming.Util;
import org.jboss.resource.adapter.jms.inflow.DLQHandler;
import org.jboss.resource.adapter.jms.inflow.JmsActivation;
import org.jboss.resource.adapter.jms.inflow.JmsActivationSpec;
public abstract class AbstractDLQHandler implements DLQHandler, ExceptionListener
{
protected static final Logger log = Logger.getLogger(AbstractDLQHandler.class);
protected JmsActivation activation;
protected Queue dlq;
protected QueueConnection connection;
public boolean handleRedeliveredMessage(Message msg)
{
boolean handled = handleDelivery(msg);
if (handled)
sendToDLQ(msg);
return handled;
}
public void messageDelivered(Message msg)
{
}
public void setup(JmsActivation activation, Context ctx) throws Exception
{
this.activation = activation;
setupDLQDestination(ctx);
setupDLQConnection(ctx);
}
public void teardown()
{
teardownDLQConnection();
teardownDLQDestination();
}
public void onException(JMSException exception)
{
activation.handleFailure(exception);
}
protected void setupDLQDestination(Context ctx) throws Exception
{
String name = activation.getActivationSpec().getDLQJNDIName();
dlq = (Queue) Util.lookup(ctx, name, Queue.class);
}
protected void teardownDLQDestination()
{
}
protected void setupDLQConnection(Context ctx) throws Exception
{
JmsActivationSpec spec = activation.getActivationSpec();
String user = spec.getDLQUser();
String pass = spec.getDLQPassword();
String clientID = spec.getDLQClientID();
JMSProviderAdapter adapter = activation.getProviderAdapter();
String queueFactoryRef = adapter.getQueueFactoryRef();
log.debug("Attempting to lookup dlq connection factory " + queueFactoryRef);
QueueConnectionFactory qcf = (QueueConnectionFactory) Util.lookup(ctx, queueFactoryRef, TopicConnectionFactory.class);
log.debug("Got dlq connection factory " + qcf + " from " + queueFactoryRef);
log.debug("Attempting to create queue connection with user " + user);
if (user != null)
connection = qcf.createQueueConnection(user, pass);
else
connection = qcf.createQueueConnection();
if (clientID != null)
connection.setClientID(clientID);
connection.setExceptionListener(this);
log.debug("Using queue connection " + connection);
}
protected void teardownDLQConnection()
{
try
{
if (connection != null)
{
log.debug("Closing the " + connection);
connection.close();
}
}
catch (Throwable t)
{
log.debug("Error closing the connection " + connection, t);
}
}
protected abstract boolean handleDelivery(Message msg);
protected void warnDLQ(Message msg, int count, int max)
{
log.warn("Message redelivered=" + count + " max=" + max + " sending it to the dlq " + msg);
}
protected void sendToDLQ(Message msg)
{
int deliveryMode = getDeliveryMode(msg);
int priority = getPriority(msg);
long timeToLive = getTimeToLive(msg);
if (timeToLive < 0)
{
if (log.isTraceEnabled())
log.trace("Not sending the message to the DLQ, it has expired " + msg);
return;
}
Message copy = makeWritable(msg);
if (copy != null)
doSend(copy, deliveryMode, priority, timeToLive);
}
protected int getDeliveryMode(Message msg)
{
try
{
return msg.getJMSDeliveryMode();
}
catch (Throwable t)
{
return Message.DEFAULT_DELIVERY_MODE;
}
}
protected int getPriority(Message msg)
{
try
{
return msg.getJMSPriority();
}
catch (Throwable t)
{
return Message.DEFAULT_PRIORITY;
}
}
protected long getTimeToLive(Message msg)
{
try
{
long expires = msg.getJMSExpiration();
if (expires == Message.DEFAULT_TIME_TO_LIVE)
return Message.DEFAULT_TIME_TO_LIVE;
return expires - System.currentTimeMillis();
}
catch (Throwable t)
{
return Message.DEFAULT_TIME_TO_LIVE;
}
}
protected Message makeWritable(Message msg)
{
boolean trace = log.isTraceEnabled();
try
{
HashMap tmp = new HashMap();
for (Enumeration en = msg.getPropertyNames(); en.hasMoreElements();)
{
String key = (String) en.nextElement();
tmp.put(key, msg.getObjectProperty(key));
}
msg.clearProperties();
for (Iterator i = tmp.keySet().iterator(); i.hasNext();)
{
String key = (String) i.next();
try
{
msg.setObjectProperty(key, tmp.get(key));
}
catch (JMSException ignored)
{
if (trace)
log.trace("Could not copy message property " + key, ignored);
}
}
msg.setStringProperty(JBOSS_ORIG_MESSAGEID, msg.getJMSMessageID());
Destination destination = msg.getJMSDestination();
if (destination != null)
msg.setStringProperty(JBOSS_ORIG_DESTINATION, destination.toString());
return msg;
}
catch (Throwable t)
{
log.error("Unable to make writable " + msg, t);
return null;
}
}
protected void doSend(Message msg, int deliveryMode, int priority, long timeToLive)
{
QueueSession session = null;
try
{
session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
QueueSender sender = session.createSender(dlq);
sender.send(msg, deliveryMode, priority, timeToLive);
}
catch (Throwable t)
{
handleSendError(msg, t);
}
finally
{
if (session != null)
{
try
{
session.close();
}
catch (Throwable t)
{
log.trace("Ignored ", t);
}
}
}
}
protected void handleSendError(Message msg, Throwable t)
{
log.error("DLQ " + dlq + " error sending message " + msg, t);
}
}