FileDocCategorySizeDatePackage
JmsSession.javaAPI DocJBoss 4.2.121375Fri Jul 13 21:01:16 BST 2007org.jboss.resource.adapter.jms

JmsSession

public class JmsSession extends Object implements javax.jms.TopicSession, javax.jms.QueueSession, javax.jms.Session
Adapts the JMS QueueSession and TopicSession API to a JmsManagedConnection.
author
Peter Antman .
author
Jason Dillon .
author
Adrian Brock
version
$Revision: 63350 $

Fields Summary
private static final Logger
log
private JmsManagedConnection
mc
The managed connection for this session.
private JmsConnectionRequestInfo
info
The connection request info
private JmsSessionFactory
sf
The session factory for this session
private HashSet
consumers
The message consumers
private HashSet
producers
The message producers
private boolean
trace
Whether trace is enabled
Constructors Summary
public JmsSession(JmsManagedConnection mc, JmsConnectionRequestInfo info)
Construct a JmsSession.

param
mc The managed connection for this session.

   
                   
        
   
      this.mc = mc;
      this.info = info;
      if (trace)
         log.trace("new JmsSession " + this + " mc=" + mc + " cri=" + info);
   
Methods Summary
voidaddConsumer(javax.jms.MessageConsumer consumer)

      synchronized (consumers)
      {
         consumers.add(consumer);
      }
   
voidaddProducer(javax.jms.MessageProducer producer)

      synchronized (producers)
      {
         producers.add(producer);
      }
   
voidcheckStrict()

      if (mc != null && mc.getManagedConnectionFactory().isStrict())
         throw new IllegalStateException(JmsSessionFactory.ISE);
   
public voidclose()
Closes the session. Sends a ConnectionEvent.CONNECTION_CLOSED to the managed connection.

throws
JMSException Failed to close session.

      sf.closeSession(this);
      closeSession();
   
voidcloseSession()

      if (mc != null)
      {
         log.trace("Closing session");

         try
         {
            mc.stop();
         }
         catch (Throwable t)
         {
            log.trace("Error stopping managed connection", t);
         }
         
         synchronized (consumers)
         {
            for (Iterator i = consumers.iterator(); i.hasNext();)
            {
               JmsMessageConsumer consumer = (JmsMessageConsumer) i.next();
               try
               {
                  consumer.closeConsumer();
               }
               catch (Throwable t)
               {
                  log.trace("Error closing consumer", t);
               }
               i.remove();
            }
         }

         synchronized (producers)
         {
            for (Iterator i = producers.iterator(); i.hasNext();)
            {
               MessageProducer producer = (MessageProducer) i.next();
               try
               {
                  producer.close();
               }
               catch (Throwable t)
               {
                  log.trace("Error closing producer", t);
               }
               i.remove();
            }
         }
         
         mc.removeHandle(this);
         ConnectionEvent ev = new ConnectionEvent(mc, ConnectionEvent.CONNECTION_CLOSED);
         ev.setConnectionHandle(this);
         mc.sendEvent(ev);
         mc = null;
      }
   
public voidcommit()

      Session session = getSession();
      if (info.isTransacted() == false)
         throw new IllegalStateException("Session is not transacted");
      if (trace)
         log.trace("Commit session " + this);
      session.commit();
   
public javax.jms.QueueBrowsercreateBrowser(javax.jms.Queue queue)

      
      if(info.getType() == JmsConnectionFactory.TOPIC)
      {
         throw new IllegalStateException("Cannot create browser for javax.jms.TopicSession");
         
      }

      Session session = getSession();
      if (trace)
         log.trace("createBrowser " + session + " queue=" + queue);
      QueueBrowser result = session.createBrowser(queue);
      if (trace)
         log.trace("createdBrowser " + session + " browser=" + result);
      return result;
   
public javax.jms.QueueBrowsercreateBrowser(javax.jms.Queue queue, java.lang.String messageSelector)

      Session session = getSession();
      if (trace)
         log.trace("createBrowser " + session + " queue=" + queue + " selector=" + messageSelector);
      QueueBrowser result = session.createBrowser(queue, messageSelector);
      if (trace)
         log.trace("createdBrowser " + session + " browser=" + result);
      return result;
   
public javax.jms.BytesMessagecreateBytesMessage()

      Session session = getSession();
      if (trace)
         log.trace("createBytesMessage" + session);
      return session.createBytesMessage();
   
public javax.jms.MessageConsumercreateConsumer(javax.jms.Destination destination)

      Session session = getSession();
      if (trace)
         log.trace("createConsumer " + session + " dest=" + destination);
      MessageConsumer result = session.createConsumer(destination);
      result = new JmsMessageConsumer(result, this);
      if (trace)
         log.trace("createdConsumer " + session + " consumer=" + result);
      addConsumer(result);
      return result;
   
public javax.jms.MessageConsumercreateConsumer(javax.jms.Destination destination, java.lang.String messageSelector)

      Session session = getSession();
      if (trace)
         log.trace("createConsumer " + session + " dest=" + destination + " messageSelector=" + messageSelector);
      MessageConsumer result = session.createConsumer(destination, messageSelector);
      result = new JmsMessageConsumer(result, this);
      if (trace)
         log.trace("createdConsumer " + session + " consumer=" + result);
      addConsumer(result);
      return result;
   
public javax.jms.MessageConsumercreateConsumer(javax.jms.Destination destination, java.lang.String messageSelector, boolean noLocal)

      Session session = getSession();
      if (trace)
         log.trace("createConsumer " + session + " dest=" + destination + " messageSelector=" + messageSelector + " noLocal=" + noLocal);
      MessageConsumer result = session.createConsumer(destination, messageSelector, noLocal);
      result = new JmsMessageConsumer(result, this);
      if (trace)
         log.trace("createdConsumer " + session + " consumer=" + result);
      addConsumer(result);
      return result;
   
public javax.jms.TopicSubscribercreateDurableSubscriber(javax.jms.Topic topic, java.lang.String name)

      if(info.getType() == JmsConnectionFactory.QUEUE)
      {
         throw new IllegalStateException("Cannot create durable subscriber from javax.jms.QueueSession");         
      }
      
      Session session = getSession();
      if (trace)
         log.trace("createDurableSubscriber " + session + " topic=" + topic + " name=" + name);
      TopicSubscriber result = session.createDurableSubscriber(topic, name);
      result = new JmsTopicSubscriber(result, this);
      if (trace)
         log.trace("createdDurableSubscriber " + session + " JmsTopicSubscriber=" + result);
      addConsumer(result);
      return result;
   
public javax.jms.TopicSubscribercreateDurableSubscriber(javax.jms.Topic topic, java.lang.String name, java.lang.String messageSelector, boolean noLocal)

      Session session = getSession();
      if (trace)
         log.trace("createDurableSubscriber " + session + " topic=" + topic + " name=" + name + " selector=" + messageSelector + " noLocal=" + noLocal);
      TopicSubscriber result = session.createDurableSubscriber(topic, name, messageSelector, noLocal);
      result = new JmsTopicSubscriber(result, this);
      if (trace)
         log.trace("createdDurableSubscriber " + session + " JmsTopicSubscriber=" + result);
      addConsumer(result);
      return result;
   
public javax.jms.MapMessagecreateMapMessage()

      Session session = getSession();
      if (trace)
         log.trace("createMapMessage" + session);
      return session.createMapMessage();
   
public javax.jms.MessagecreateMessage()

      Session session = getSession();
      if (trace)
         log.trace("createMessage" + session);
      return session.createMessage();
   
public javax.jms.ObjectMessagecreateObjectMessage()

      Session session = getSession();
      if (trace)
         log.trace("createObjectMessage" + session);
      return session.createObjectMessage();
   
public javax.jms.ObjectMessagecreateObjectMessage(java.io.Serializable object)

      Session session = getSession();
      if (trace)
         log.trace("createObjectMessage(Object)" + session);
      return session.createObjectMessage(object);
   
public javax.jms.MessageProducercreateProducer(javax.jms.Destination destination)

      Session session = getSession();
      if (trace)
         log.trace("createProducer " + session + " dest=" + destination);
      MessageProducer result = getSession().createProducer(destination);
      if (trace)
         log.trace("createdProducer " + session + " producer=" + result);
      addProducer(result);
      return result;
   
public javax.jms.TopicPublishercreatePublisher(javax.jms.Topic topic)

      TopicSession session = getTopicSession();
      if (trace)
         log.trace("createPublisher " + session + " topic=" + topic);
      TopicPublisher result = session.createPublisher(topic);
      if (trace)
         log.trace("createdPublisher " + session + " publisher=" + result);
      addProducer(result);
      return result;
   
public javax.jms.QueuecreateQueue(java.lang.String queueName)

      if(info.getType() == JmsConnectionFactory.TOPIC)
      {
         throw new IllegalStateException("Cannot create browser or javax.jms.TopicSession");
         
      }

      Session session = getSession();
      if (trace)
         log.trace("createQueue " + session + " queueName=" + queueName);
      Queue result = session.createQueue(queueName);
      if (trace)
         log.trace("createdQueue " + session + " queue=" + result);
      return result;
   
public javax.jms.QueueReceivercreateReceiver(javax.jms.Queue queue)

      QueueSession session = getQueueSession();
      if (trace)
         log.trace("createReceiver " + session + " queue=" + queue);
      QueueReceiver result = session.createReceiver(queue);
      result = new JmsQueueReceiver(result, this);
      if (trace)
         log.trace("createdReceiver " + session + " receiver=" + result);
      addConsumer(result);
      return result;
   
public javax.jms.QueueReceivercreateReceiver(javax.jms.Queue queue, java.lang.String messageSelector)

      QueueSession session = getQueueSession();
      if (trace)
         log.trace("createReceiver " + session + " queue=" + queue + " selector=" + messageSelector);
      QueueReceiver result = session.createReceiver(queue, messageSelector);
      result = new JmsQueueReceiver(result, this);
      if (trace)
         log.trace("createdReceiver " + session + " receiver=" + result);
      addConsumer(result);
      return result;
   
public javax.jms.QueueSendercreateSender(javax.jms.Queue queue)

      QueueSession session = getQueueSession();
      if (trace)
         log.trace("createSender " + session + " queue=" + queue);
      QueueSender result = session.createSender(queue);
      if (trace)
         log.trace("createdSender " + session + " sender=" + result);
      addProducer(result);
      return result;
   
public javax.jms.StreamMessagecreateStreamMessage()

      Session session = getSession();
      if (trace)
         log.trace("createStreamMessage" + session);
      return session.createStreamMessage();
   
public javax.jms.TopicSubscribercreateSubscriber(javax.jms.Topic topic)

      TopicSession session = getTopicSession();
      if (trace)
         log.trace("createSubscriber " + session + " topic=" + topic);
      TopicSubscriber result = session.createSubscriber(topic);
      result = new JmsTopicSubscriber(result, this);
      if (trace)
         log.trace("createdSubscriber " + session + " JmsTopicSubscriber=" + result);
      addConsumer(result);
      return result;
   
public javax.jms.TopicSubscribercreateSubscriber(javax.jms.Topic topic, java.lang.String messageSelector, boolean noLocal)

      TopicSession session = getTopicSession();
      if (trace)
         log.trace("createSubscriber " + session + " topic=" + topic + " selector=" + messageSelector + " noLocal=" + noLocal);
      TopicSubscriber result = session.createSubscriber(topic, messageSelector, noLocal);
      result = new JmsTopicSubscriber(result, this);
      if (trace)
         log.trace("createdSubscriber " + session + " JmsTopicSubscriber=" + result);
      addConsumer(result);
      return result;
   
public javax.jms.TemporaryQueuecreateTemporaryQueue()

      if(info.getType() == JmsConnectionFactory.TOPIC)
      {
         throw new IllegalStateException("Cannot create temporary queue for javax.jms.TopicSession");
         
      }
      Session session = getSession();
      if (trace)
         log.trace("createTemporaryQueue " + session);
      TemporaryQueue temp = session.createTemporaryQueue();
      if (trace)
         log.trace("createdTemporaryQueue " + session + " temp=" + temp);
      sf.addTemporaryQueue(temp);
      return temp;
   
public javax.jms.TemporaryTopiccreateTemporaryTopic()

      if(info.getType() == JmsConnectionFactory.QUEUE)
      {
         throw new IllegalStateException("Cannot create temporary topic for javax.jms.QueueSession");         
      }
      
      Session session = getSession();
      if (trace)
         log.trace("createTemporaryTopic " + session);
      TemporaryTopic temp = session.createTemporaryTopic();
      if (trace)
         log.trace("createdTemporaryTopic " + session + " temp=" + temp);
      sf.addTemporaryTopic(temp);
      return temp;
   
public javax.jms.TextMessagecreateTextMessage()

      Session session = getSession();
      if (trace)
         log.trace("createTextMessage" + session);
      return session.createTextMessage();
   
public javax.jms.TextMessagecreateTextMessage(java.lang.String string)

      Session session = getSession();
      if (trace)
         log.trace("createTextMessage(String)" + session);
      return session.createTextMessage(string);
   
public javax.jms.TopiccreateTopic(java.lang.String topicName)

      if(info.getType() == JmsConnectionFactory.QUEUE)
      {
         throw new IllegalStateException("Cannot create topic for javax.jms.QueueSession");         
      }

      Session session = getSession();
      if (trace)
         log.trace("createTopic " + session + " topicName=" + topicName);
      Topic result = session.createTopic(topicName);
      if (trace)
         log.trace("createdTopic " + session + " topic=" + result);
      return result;
   
voiddestroy()

      mc = null;
   
public intgetAcknowledgeMode()

      getSession(); // check closed
      return info.getAcknowledgeMode();
   
public javax.jms.MessageListenergetMessageListener()
Always throws an Exception.

throws
IllegalStateException Method not allowed.

      throw new IllegalStateException("Method not allowed");
   
javax.jms.QueueSessiongetQueueSession()

      Session s = getSession();
      if( !(s instanceof QueueSession) )
         throw new InvalidDestinationException("Attempting to use QueueSession methods on: "+this);
      return (QueueSession) s; 
   
javax.jms.SessiongetSession()
Ensure that the session is opened.

return
The session
throws
IllegalStateException The session is closed

      // ensure that the connection is opened
      if (mc == null)
         throw new IllegalStateException("The session is closed");
      
      Session session = mc.getSession();
      if (trace)
         log.trace("getSession " + session + " for " + this);
      return session;
   
javax.jms.TopicSessiongetTopicSession()

      Session s = getSession();
      if( !(s instanceof TopicSession) )
         throw new InvalidDestinationException("Attempting to use TopicSession methods on: "+this);
      return (TopicSession) s; 
   
public booleangetTransacted()

      getSession(); // check closed
      return info.isTransacted();
   
public voidrecover()

      Session session = getSession();
      if (info.isTransacted())
         throw new IllegalStateException("Session is transacted");
      if (trace)
         log.trace("Recover session " + this);
      session.recover();
   
voidremoveConsumer(javax.jms.MessageConsumer consumer)

      synchronized (consumers)
      {
         consumers.remove(consumer);
      }
   
voidremoveProducer(javax.jms.MessageProducer producer)

      synchronized (producers)
      {
         producers.remove(producer);
      }
   
public voidrollback()

      Session session = getSession();
      if (info.isTransacted() == false)
         throw new IllegalStateException("Session is not transacted");
      if (trace)
         log.trace("Rollback session " + this);
      session.rollback();
   
public voidrun()
Always throws an Error.

throws
Error Method not allowed.

      // should this really throw an Error?
      throw new Error("Method not allowed");
   
public voidsetJmsSessionFactory(JmsSessionFactory sf)

      this.sf = sf;
   
voidsetManagedConnection(JmsManagedConnection mc)

      if (this.mc != null)
         this.mc.removeHandle(this);
      this.mc = mc;
   
public voidsetMessageListener(javax.jms.MessageListener listener)
Always throws an Exception.

throws
IllegalStateException Method not allowed.

      throw new IllegalStateException("Method not allowed");
   
voidstart()

      if (mc != null)
         mc.start();
   
voidstop()

      if (mc != null)
         mc.stop();
   
public voidunsubscribe(java.lang.String name)

      if(info.getType() == JmsConnectionFactory.QUEUE)
      {
         throw new IllegalStateException("Cannot unsubscribe for javax.jms.QueueSession");         
      }

      Session session = getSession();
      if (trace)
         log.trace("unsubscribe " + session + " name=" + name);
      session.unsubscribe(name);