jms-spec
  1. jms-spec
  2. JMS_SPEC-53

Make Connection and other interfaces implement AutoCloseable

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.1
    • Fix Version/s: 2.0ED, 2.0
    • Labels:
      None

      Description

      This is a proposal to change all the JMS interfaces that currently implement a close() method to implement the java.lang.AutoCloseable interface?

      This would affect Connection, Session, MessageConsumer, MessageProducer, QueueBrowser.

      This is a new feature of Java SE 7 which makes it easier to write code which closes a resource after use.

      There's a nice explanation here:
      http://www.javacodegeeks.com/2011/07/java-7-try-with-resources-explained.html]

      Briefly, it allows you to write code such as:

      try { 
         Connection conn = connectionFactory.createConnection();
         Session sess = conn.createSession(false,Session.AUTO_ACKNOWLEDGE;
         MessageProducer producer = seession.createProducer(dest);
      }{
         Message mess = sess.createTextMessage("hello");
         producer.send(mess);
      } catch(JMSException e){
        // exception handling
      }
      

      When this code is executed, the close() methods on the connection, session and producer are always called after use.

      • There's no need to call close() on any of the objects that are created.
      • There's no need to provide a finally block containing the calls to close().
      • Objects are closed in the reverse order in which they were created.
      • There's no need to guard against calling close() on a null value.
      • There's no need to use a nested try/catch block within the finally block to catch exceptions thrown by close()
      • If the try() block throws an exception, and a subsequent call to close() throws a second exception as a consequence, then it is the first exception, not the second exception, that is passed to the catch block. So you only see the exception that really matters. Any suppressed exceptions can still be accessed from the thrown exception if needed.

      This change would be of benefit to both Java SE and Java EE applications. The only drawback I can think of is that it would introduce a dependency on Java SE 7. This isn't an issue for Java EE 7 applications since these are already dependent on Java SE 7. But it would force Java SE applications to use Java SE 7. However by the time JMS 2.0 is released, in a year from now, Java 7 will be pretty widespread so this might not be a problem.

        Issue Links

          Activity

          Hide
          Nigel Deakin added a comment - - edited

          I've now updated the javadocs and the draft spec with details of this new feature (these changes are additive, so those docs include other changes).

          The updated Javadocs are here:
          http://java.net/projects/jms-spec/sources/repository/content/jms2.0/target/jms-2.0-javadoc.jar
          (One-line changes to Connection, Session, MessageProducer, MessageConsumer and QueueBrowser)

          The updated draft spec is here:
          http://java.net/projects/jms-spec/sources/repository/content/jms2.0/specification/word/JMS20.pdf
          (all changes are highlighted clearly with changebars, but the only place I've changed is 4.3.5 "Closing a Connection", 4.4.1 "Closing a Session", the example in section 9.1.3. Creating a Connection", and, in the changelog, section 11.5.3 "Automatically closing a connection or session")

          I've now made Connection, Session, MessageProducer, MessageConsumer and QueueBrowser all extend AutoCloseable. (Note that MessagingContext, part of the proposed simplified API, already extends AutoCloseable)

          (This updates the previous version of this comment which only referred to changes to Connection).

          Show
          Nigel Deakin added a comment - - edited I've now updated the javadocs and the draft spec with details of this new feature (these changes are additive, so those docs include other changes). The updated Javadocs are here: http://java.net/projects/jms-spec/sources/repository/content/jms2.0/target/jms-2.0-javadoc.jar (One-line changes to Connection , Session , MessageProducer , MessageConsumer and QueueBrowser ) The updated draft spec is here: http://java.net/projects/jms-spec/sources/repository/content/jms2.0/specification/word/JMS20.pdf (all changes are highlighted clearly with changebars, but the only place I've changed is 4.3.5 "Closing a Connection", 4.4.1 "Closing a Session", the example in section 9.1.3. Creating a Connection", and, in the changelog, section 11.5.3 "Automatically closing a connection or session") I've now made Connection , Session , MessageProducer , MessageConsumer and QueueBrowser all extend AutoCloseable . (Note that MessagingContext , part of the proposed simplified API , already extends AutoCloseable ) (This updates the previous version of this comment which only referred to changes to Connection ).
          Hide
          Nigel Deakin added a comment -

          I've also changed javax.jms.SyncMessageConsumer to extend AutoCloseable, for consistency with MessageConsumer. API and javadocs updated.

          Show
          Nigel Deakin added a comment - I've also changed javax.jms.SyncMessageConsumer to extend AutoCloseable, for consistency with MessageConsumer. API and javadocs updated.

            People

            • Assignee:
              Nigel Deakin
              Reporter:
              Nigel Deakin
            • Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: