dk.netarkivet.common.distribute
Class JMSConnection

java.lang.Object
  extended by dk.netarkivet.common.distribute.JMSConnection
All Implemented Interfaces:
CleanupIF
Direct Known Subclasses:
JMSConnectionSunMQ

public abstract class JMSConnection
extends java.lang.Object
implements CleanupIF

Handles the communication with a JMS broker. Note on Thread-safety: the methods and fields of JMSConnection are not accessed by multiple threads (though JMSConnection itself creates threads). Thus no synchronization is needed on methods and fields of JMSConnection. A shutdown hook is also added, which closes the connection.


Field Summary
(package private) static java.lang.String CONSUMER_KEY_SEPARATOR
           
protected static org.apache.commons.logging.Log log
           
 
Constructor Summary
protected JMSConnection()
          Sets the broker address and port number.
 
Method Summary
 void cleanup()
          Clean up.
protected  void close()
          Close all connections to the JMS broker.
 java.lang.String getHost()
           
 java.lang.String getPort()
           
protected abstract  javax.jms.Queue getQueue(java.lang.String queueName)
          Should be implemented according to a specific JMS broker.
protected abstract  javax.jms.QueueConnectionFactory getQueueConnectionFactory()
          Should be implemented according to a specific JMS broker.
protected abstract  javax.jms.Topic getTopic(java.lang.String topicName)
          Should be implemented according to a specific JMS broker.
protected abstract  javax.jms.TopicConnectionFactory getTopicConnectionFactory()
          Should be implemented according to a specific JMS broker.
protected  void initConnection()
          Initializes the JMS connection.
 java.util.List<javax.jms.Message> removeAllMessages(ChannelID mq)
          Remove all messages waiting in a queue/topic.
 void removeListener(ChannelID mq, javax.jms.MessageListener ml)
          Removes the specified MessageListener from the given queue or topic.
 void reply(NetarkivetMessage nMsg)
          Submit an object to the reply queue.
 void resend(NetarkivetMessage msg, ChannelID to)
          Sends a message msg to the channel defined by the parameter to - NOT the channel defined in the message.
 void send(NetarkivetMessage nMsg)
          Submit an object to the destination queue.
protected  void sendMessage(NetarkivetMessage nMsg, ChannelID to)
          Submit an ObjectMessage to the destination channel.
 void setListener(ChannelID mq, javax.jms.MessageListener ml)
          Method adds a listener to the given queue or topic.
static NetarkivetMessage unpack(javax.jms.Message msg)
          Unwraps a NetarkivetMessage from an ObjectMessage.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static final org.apache.commons.logging.Log log

CONSUMER_KEY_SEPARATOR

static final java.lang.String CONSUMER_KEY_SEPARATOR
See Also:
Constant Field Values
Constructor Detail

JMSConnection

protected JMSConnection()
Sets the broker address and port number.

Method Detail

initConnection

protected void initConnection()
Initializes the JMS connection. Creating and starting connections to queues and topics. Adds the JMSConnection to a shutdown hook.

Throws:
IOFailure - if initialization fails.

getQueueConnectionFactory

protected abstract javax.jms.QueueConnectionFactory getQueueConnectionFactory()
                                                                       throws javax.jms.JMSException
Should be implemented according to a specific JMS broker.

Returns:
QueueConnectionFactory
Throws:
javax.jms.JMSException

getTopicConnectionFactory

protected abstract javax.jms.TopicConnectionFactory getTopicConnectionFactory()
                                                                       throws javax.jms.JMSException
Should be implemented according to a specific JMS broker.

Returns:
TopicConnectionFactory
Throws:
javax.jms.JMSException

getQueue

protected abstract javax.jms.Queue getQueue(java.lang.String queueName)
                                     throws javax.jms.JMSException
Should be implemented according to a specific JMS broker.

Parameters:
queueName - the name of the wanted Queue
Returns:
Queue
Throws:
javax.jms.JMSException

getTopic

protected abstract javax.jms.Topic getTopic(java.lang.String topicName)
                                     throws javax.jms.JMSException
Should be implemented according to a specific JMS broker.

Parameters:
topicName - The name of the wanted Topic
Returns:
Topic
Throws:
javax.jms.JMSException

send

public final void send(NetarkivetMessage nMsg)
Submit an object to the destination queue. This method should not be overwritten. Overwrite the sendMessage to change functionality.

Parameters:
nMsg - The NetarkivetMessage to send to the destination queue (null not allowed)
Throws:
ArgumentNotValid - if nMsg is null.
IOFailure - if the operation failed.

reply

public void reply(NetarkivetMessage nMsg)
Submit an object to the reply queue.

Parameters:
nMsg - The NetarkivetMessage to send to the reply queue (null not allowed)
Throws:
ArgumentNotValid - if nMsg is null.
IOFailure - if the operation failed.

sendMessage

protected void sendMessage(NetarkivetMessage nMsg,
                           ChannelID to)
                    throws IOFailure
Submit an ObjectMessage to the destination channel.

Parameters:
nMsg - the NetarkivetMessage to be wrapped and send as an ObjectMessage
to - the destination channel
Throws:
IOFailure - if message failed to be sent.

resend

public final void resend(NetarkivetMessage msg,
                         ChannelID to)
Sends a message msg to the channel defined by the parameter to - NOT the channel defined in the message.

Parameters:
msg - Message to be sent
to - The destination channel

close

protected void close()
Close all connections to the JMS broker.

Throws:
IOFailure - if closing one of the internal connection objects failed.

cleanup

public void cleanup()
Clean up.

Specified by:
cleanup in interface CleanupIF

unpack

public static NetarkivetMessage unpack(javax.jms.Message msg)
                                throws ArgumentNotValid
Unwraps a NetarkivetMessage from an ObjectMessage.

Parameters:
msg - a javax.jms.ObjectMessage
Returns:
a NetarkivetMessage
Throws:
ArgumentNotValid - when msg in valid or reply from JMS server is invalid

setListener

public void setListener(ChannelID mq,
                        javax.jms.MessageListener ml)
                 throws IOFailure
Method adds a listener to the given queue or topic.

Parameters:
mq - the messagequeue to listen to
ml - the messagelistener
Throws:
IOFailure - if the operation failed.

removeListener

public void removeListener(ChannelID mq,
                           javax.jms.MessageListener ml)
                    throws IOFailure
Removes the specified MessageListener from the given queue or topic.

Parameters:
mq -
ml -
Throws:
IOFailure

removeAllMessages

public java.util.List<javax.jms.Message> removeAllMessages(ChannelID mq)
Remove all messages waiting in a queue/topic. When this method ends, there are no messages on the queue/topic (though some can still arrive afterwards). For a queue, this does not ensure that all messages disappear, other processes could still make it in and take some. For a topic, this does not ensure that nobody recieve these messages.

Parameters:
mq - The queue/topic to remove messages from
Returns:
A list of all messages removed from the queue/topic.

getHost

public java.lang.String getHost()

getPort

public java.lang.String getPort()