jford 2004/07/26 20:15:17
Modified: src/java/org/apache/jetspeed/services/messaging/jms
AbstractJMSMessagingService.java
Log:
Added logging
Revision Changes Path
1.2 +174 -45
jakarta-jetspeed/src/java/org/apache/jetspeed/services/messaging/jms/AbstractJMSMessagingService.java
Index: AbstractJMSMessagingService.java
===================================================================
RCS file:
/home/cvs/jakarta-jetspeed/src/java/org/apache/jetspeed/services/messaging/jms/AbstractJMSMessagingService.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- AbstractJMSMessagingService.java 23 Jul 2004 00:24:01 -0000 1.1
+++ AbstractJMSMessagingService.java 27 Jul 2004 03:15:16 -0000 1.2
@@ -33,17 +33,30 @@
import javax.naming.NamingException;
import javax.servlet.ServletConfig;
+import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
+import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.jetspeed.services.messaging.MessagingService;
import org.apache.turbine.services.InitializationException;
import org.apache.turbine.services.TurbineBaseService;
/**
+ * This class provides basic functionality for accessing the JMS server.
+ * This implementation conforms to the JMS 1.1 specification.
+ *
+ * New implementations of a JMS Messaging Service should extend this
+ * class and implement the createConnection method. Also, any default
+ * variables that need to be set should be added to a default constructor.
+ *
* @author <a href="mailto:[EMAIL PROTECTED]">Jeremy Ford</a>
* @version $Id$
*/
public abstract class AbstractJMSMessagingService extends TurbineBaseService
implements MessagingService
{
+ //static logger
+ private static JetspeedLogger logger =
JetspeedLogFactoryService.getLogger(AbstractJMSMessagingService.class.getName());
+
+ //url configuration
protected static final String CONFIG_SCHEME = "scheme";
protected static final String CONFIG_HOST = "host";
protected static final String CONFIG_PORT = "port";
@@ -63,6 +76,7 @@
protected static final String CONFIG_PRIORITY = "priority";
protected static final String CONFIG_TIME_TO_LIVE = "time_to_live";
+ //url defaults
protected String DEFAULT_SCHEME;
protected String DEFAULT_HOST;
protected String DEFAULT_PORT;
@@ -88,6 +102,7 @@
//Delivery Mode Key
protected static final String PERSISTENT = "persistent";
+ protected static final String NON_PERSISTENT = "non_persistent";
//url properties
protected String scheme;
@@ -113,9 +128,9 @@
protected boolean durable;
protected boolean nolocal;
- protected Hashtable destinations = new Hashtable();
- protected Hashtable consumers = new Hashtable();
- protected Hashtable producers = new Hashtable();
+ protected Hashtable destinations = new Hashtable(); //maps destination string
to Destination object
+ protected Hashtable consumers = new Hashtable(); //maps uniqueId to consumer
+ protected Hashtable producers = new Hashtable(); //maps destination string to
Producer object
protected Connection connection;
protected Session session;
@@ -138,30 +153,46 @@
}
catch(JMSException e)
{
+ logger.error("Failure to intialize service due to JMS Exception.", e);
throw new InitializationException(e.getMessage(), e);
}
catch(Exception e)
{
+ logger.error("Unknown exception caught while trying to initialize
messaging service.", e);
throw new InitializationException(e.getMessage(), e);
}
}
protected void initJMSConfiguration()
{
+
+ logger.info("Initializing JMS Configuration.");
+
+ //initialize service properties
scheme = getConfiguration().getString(CONFIG_SCHEME, DEFAULT_SCHEME);
host = getConfiguration().getString(CONFIG_HOST, DEFAULT_HOST);
port = getConfiguration().getString(CONFIG_PORT, DEFAULT_PORT);
name = getConfiguration().getString(CONFIG_NAME, DEFAULT_NAME);
url = getConfiguration().getString(CONFIG_URL, DEFAULT_URL);
+ logger.info("JMS URL (Required) configured to " + this.getProviderURL());
+
connectionFactory = getConfiguration().getString(CONFIG_CONNECTION_FACTORY,
DEFAULT_CONNECTION_FACTORY);
+ logger.info("JMS Connection Facotry (Optional): " + connectionFactory);
+
transacted = getConfiguration().getBoolean(CONFIG_TRANSACTED,
DEFAULT_TRANSACTED);
+ logger.info("JMS Transacted: " + (transacted ? "yes" : "no"));
+
use_topic = getConfiguration().getBoolean(CONFIG_TOPIC, DEFAULT_TOPIC);
durable = getConfiguration().getBoolean(CONFIG_DURABLE, DEFAULT_DURABLE);
nolocal = getConfiguration().getBoolean(CONFIG_NOLOCAL, DEFAULT_NOLOCAL);
+ logger.info("JMS Use Topic: " + (use_topic ? "yes" : "no"));
+ logger.info("JMS Durable: " + (durable ? "yes" : "no"));
+ logger.info("JMS NoLocal: " + (nolocal ? "yes" : "no"));
+
ackMode = DEFAULT_ACK_MODE;
String ackModeStr = getConfiguration().getString(CONFIG_ACK_MODE);
if(ackModeStr != null)
@@ -180,6 +211,8 @@
}
}
+ logger.info("JMS Acknowledgement Mode: " + printAckMode());
+
//producer related config
deliveryMode = DEFAULT_DELIVERY_MODE;
String deliveryModeStr = getConfiguration().getString(CONFIG_DELIVERY_MODE);
@@ -194,8 +227,42 @@
deliveryMode = DeliveryMode.NON_PERSISTENT;
}
}
+
+ logger.info("JMS Delivery Mode: " + printDeliveryMode());
+
priority = getConfiguration().getInt(CONFIG_PRIORITY, DEFAULT_PRIORITY);
timeToLive = getConfiguration().getInt(CONFIG_TIME_TO_LIVE,
DEFAULT_TIME_TO_LIVE);
+
+ logger.info("JMS Message Priority: " + priority);
+ logger.info("JMS Message TimeToLIve: " + timeToLive);
+
+ logger.info("JMS Initialization complete.");
+ }
+
+ protected String printAckMode()
+ {
+ String ackModeStr = AUTO_ACKNOWLEDGE;
+ if(ackMode == Session.CLIENT_ACKNOWLEDGE)
+ {
+ ackModeStr = CLIENT_ACKNOWLEDGE;
+ }
+ else if(ackMode == Session.DUPS_OK_ACKNOWLEDGE)
+ {
+ ackModeStr = DUPS_OK_ACKNOWLEDGE;
+ }
+
+ return ackModeStr;
+ }
+
+ protected String printDeliveryMode()
+ {
+ String deliveryModeStr = PERSISTENT;
+ if(deliveryMode == DeliveryMode.NON_PERSISTENT)
+ {
+ deliveryModeStr = NON_PERSISTENT;
+ }
+
+ return deliveryModeStr;
}
protected String getProviderURL()
@@ -212,7 +279,6 @@
return providerURL;
}
-
protected Context getContext() throws NamingException
{
Hashtable properties = new Hashtable();
@@ -235,7 +301,7 @@
protected abstract void createConnection() throws JMSException;
- /* (non-Javadoc)
+ /**
* @see
org.apache.jetspeed.services.messaging.MessagingService#createMessage(int)
*/
public Message createMessage(int messageType)
@@ -271,7 +337,7 @@
}
catch(JMSException e)
{
- e.printStackTrace();
+ logger.error("Failure to create message", e);
}
return message;
@@ -290,13 +356,13 @@
MessageConsumer consumer = (MessageConsumer)consumers.get(id);
try
{
- System.out.println("Closing consumer for uniqueid " + id);
+ logger.info("Closing consumer " + id);
consumer.close();
+ logger.info("Consumer " + id + " closed successfully.");
}
- catch (JMSException e2)
+ catch (JMSException e)
{
- // TODO Auto-generated catch block
- e2.printStackTrace();
+ logger.error("Failed to close consumer " + id, e);
}
}
@@ -307,40 +373,40 @@
MessageProducer producer = (MessageProducer)producers.get(id);
try
{
- System.out.println("Closing producer for destinaton " + id);
+ logger.info("Closing producer to destination " + id);
producer.close();
+ logger.info("Producer " + id + " closed successfully.");
}
- catch (JMSException e2)
+ catch (JMSException e)
{
- // TODO Auto-generated catch block
- e2.printStackTrace();
+ logger.error("Failed to closed producer " + id, e);
}
}
try
{
- System.out.println("closing session");
+ logger.info("Closing session.");
session.close();
+ logger.info("Session closed.");
}
catch (JMSException e)
{
- // TODO Auto-generated catch block
- e.printStackTrace();
+ logger.error("Failed to close session", e);
}
try
{
- System.out.println("closing connection");
+ logger.info("Closing JMS connection.");
connection.stop();
connection.close();
+ logger.info("Connection closed.");
}
- catch (JMSException e1)
+ catch (JMSException e)
{
- // TODO Auto-generated catch block
- e1.printStackTrace();
+ logger.error("Failed to close connection.", e);
}
}
- /* (non-Javadoc)
+ /**
* @see
org.apache.jetspeed.services.messaging.MessagingService#addMessageListener(java.lang.Object)
*/
public void addMessageListener(MessageListener listener, String id, String
destination)
@@ -368,72 +434,116 @@
}
catch (JMSException e)
{
- // TODO Auto-generated catch block
- e.printStackTrace();
+ logger.error("Failed to add message listener " + id + " to destination
" + destination, e);
}
}
+
+ /**
+ * @see
org.apache.jetspeed.services.messaging.MessagingService#createConsumer(java.lang.String)
+ */
public MessageConsumer createConsumer(String destination)
{
MessageConsumer consumer = null;
try
{
Destination consumerDestination = getDestination(destination);
- consumer = session.createConsumer(consumerDestination);
+ if(consumerDestination == null)
+ {
+ logger.error("Failed to obtain destination " + destination + ". A
consumer cannot be created.");
+ }
+ else
+ {
+ logger.debug("Attempting to create consumer");
+ consumer = session.createConsumer(consumerDestination);
+ logger.debug("Consumer created successfully");
+ }
}
catch (JMSException e)
{
- // TODO Auto-generated catch block
- e.printStackTrace();
+ logger.error("Failed to create consumer to destincation " +
destination);
}
return consumer;
}
+ /**
+ * @see
org.apache.jetspeed.services.messaging.MessagingService#removeMessageListener(java.lang.String)
+ */
public void removeMessageListener(String id)
{
MessageConsumer consumer = (MessageConsumer)consumers.get(id);
- try
+ if(consumer == null)
{
- if(durable && use_topic)
- {
- session.unsubscribe(id);
- }
- consumer.close();
- } catch (JMSException e)
+ logger.error("Attempted to remove consumer " + id + " but could not
find it in cache.");
+ }
+ else
{
- // TODO Auto-generated catch block
- e.printStackTrace();
+ if(durable && use_topic)
+ {
+ try
+ {
+ session.unsubscribe(id);
+ }
+ catch (JMSException e)
+ {
+ logger.error("Failed to unsubscribe durable consumer " + id,
e);
+ }
+ }
+ try
+ {
+ consumer.close();
+ }
+ catch (JMSException e)
+ {
+ logger.error("Failed to close consumer " + id, e);
+ }
+
+ consumers.remove(id);
}
- consumers.remove(id);
}
- /* (non-Javadoc)
+ /**
* @see
org.apache.jetspeed.services.messaging.MessagingService#sendMessage(java.lang.Object)
*/
public void sendMessage(Message message, String destination)
{
try
{
+ logger.debug("Sending message " + message + " to " + destination);
Destination producerDestination = getDestination(destination);
+ if(producerDestination == null)
+ {
+ logger.error("Failed to obtain destination " + destination + ".
Message cannot be sent.");
+ return;
+ }
+
MessageProducer producer = getProducer(destination);
+ if(producer == null)
+ {
+ logger.error("Failed to obtain producer for destination " +
destination + ". Message cannot be sent.");
+ return;
+ }
+
producer.send(producerDestination, message);//, deliveryMode, priority,
0);
+ logger.debug("Message " + message + " sent to " + destination + "
successfully.");
}
catch (JMSException e)
{
- // TODO Auto-generated catch block
- e.printStackTrace();
+ logger.error("Failed to send message to destination " + destination, e);
}
}
protected Destination getDestination(String destination)
{
+ logger.debug("Attempting to find destination " + destination + " in
cache.");
Destination jmsDestination = (Destination) destinations.get(destination);
if(jmsDestination == null)
{
+ logger.debug("Failed to find destination " + destination + " in cache.
Will attempt to create.");
try
{
if (use_topic) {
@@ -444,34 +554,53 @@
}
destinations.put(destination, jmsDestination);
+ logger.debug("Created destination successfully and cached.");
}
catch (JMSException e)
{
- e.printStackTrace();
+ logger.error("Failed to create destination " + destination, e);
}
}
+ else
+ {
+ logger.debug("Found destination in cache.");
+ }
return jmsDestination;
}
protected MessageProducer getProducer(String destination)
{
- Destination producerDestination = (Destination)
destinations.get(destination);
+ Destination producerDestination = getDestination(destination);
+ if(producerDestination == null)
+ {
+ logger.error("Failed to obtain destination " + destination + ". A
producer cannot be created.");
+ return null;
+ }
+
+ logger.debug("Attempting to find producer for destination " + destination +
" in cache.");
MessageProducer producer = (MessageProducer) producers.get(destination);
if(producer == null)
{
try
{
+ logger.debug("Attempting to create producer.");
producer = session.createProducer(producerDestination);
producer.setDeliveryMode(this.deliveryMode);
producer.setTimeToLive(this.timeToLive);
producer.setPriority(this.priority);
+
+ producers.put(destination, producer);
+ logger.debug("Producer created successfully.");
}
catch (JMSException e)
{
- // TODO Auto-generated catch block
- e.printStackTrace();
+ logger.error("Failed to create producer", e);
}
+ }
+ else
+ {
+ logger.debug("Found producer in cache");
}
return producer;
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]