From fb08688252d1043b202e1b88a9cd5b37da67794c Mon Sep 17 00:00:00 2001 From: Andrew Stitcher Date: Sun, 18 Dec 2011 05:09:07 +0000 Subject: QPID-3044: Implement JCA Adapter for Java JMS client - Large contributions from Weston Price & Kevin Conner git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@1220336 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/qpid/client/AMQConnectionFactory.java | 6 ++++ .../org/apache/qpid/client/AMQQueueBrowser.java | 40 +++++++++++++++++++--- .../apache/qpid/client/TopicPublisherAdapter.java | 2 +- 3 files changed, 43 insertions(+), 5 deletions(-) (limited to 'java/client/src/main') diff --git a/java/client/src/main/java/org/apache/qpid/client/AMQConnectionFactory.java b/java/client/src/main/java/org/apache/qpid/client/AMQConnectionFactory.java index f0c003e02a..700073488e 100644 --- a/java/client/src/main/java/org/apache/qpid/client/AMQConnectionFactory.java +++ b/java/client/src/main/java/org/apache/qpid/client/AMQConnectionFactory.java @@ -46,6 +46,12 @@ public class AMQConnectionFactory implements ConnectionFactory, QueueConnectionF { private final ConnectionURL _connectionDetails; + // The default constructor is necessary to allow AMQConnectionFactory to be deserialised from JNDI + public AMQConnectionFactory() + { + _connectionDetails = null; + } + public AMQConnectionFactory(final String url) throws URLSyntaxException { if (url == null) diff --git a/java/client/src/main/java/org/apache/qpid/client/AMQQueueBrowser.java b/java/client/src/main/java/org/apache/qpid/client/AMQQueueBrowser.java index d96544adf8..3f9eadeef3 100644 --- a/java/client/src/main/java/org/apache/qpid/client/AMQQueueBrowser.java +++ b/java/client/src/main/java/org/apache/qpid/client/AMQQueueBrowser.java @@ -30,6 +30,7 @@ import javax.jms.Queue; import javax.jms.QueueBrowser; import java.util.ArrayList; import java.util.Enumeration; +import java.util.NoSuchElementException; import java.util.concurrent.atomic.AtomicBoolean; public class AMQQueueBrowser implements QueueBrowser @@ -112,9 +113,12 @@ public class AMQQueueBrowser implements QueueBrowser public QueueBrowserEnumeration(BasicMessageConsumer consumer) throws JMSException { - _nextMessage = consumer == null ? null : consumer.receiveBrowse(); + if (consumer != null) + { + _consumer = consumer; + prefetchMessage(); + } _logger.info("QB:created with first element:" + _nextMessage); - _consumer = consumer; } public boolean hasMoreElements() @@ -126,18 +130,46 @@ public class AMQQueueBrowser implements QueueBrowser public Object nextElement() { Message msg = _nextMessage; + if (msg == null) + { + throw new NoSuchElementException("No messages") ; + } try { _logger.info("QB:nextElement about to receive"); - _nextMessage = _consumer.receiveBrowse(); + prefetchMessage(); _logger.info("QB:nextElement received:" + _nextMessage); } catch (JMSException e) { _logger.warn("Exception caught while queue browsing", e); _nextMessage = null; + try + { + closeConsumer() ; + } + catch (final JMSException jmse) {} // ignore } return msg; } - } + + private void prefetchMessage() throws JMSException + { + _nextMessage = _consumer.receiveBrowse(); + if (_nextMessage == null) + { + closeConsumer() ; + } + } + + private void closeConsumer() throws JMSException + { + if (_consumer != null) + { + BasicMessageConsumer consumer = _consumer ; + _consumer = null ; + consumer.close() ; + } + } + } } diff --git a/java/client/src/main/java/org/apache/qpid/client/TopicPublisherAdapter.java b/java/client/src/main/java/org/apache/qpid/client/TopicPublisherAdapter.java index 81b9940ed5..0f3be4ba18 100644 --- a/java/client/src/main/java/org/apache/qpid/client/TopicPublisherAdapter.java +++ b/java/client/src/main/java/org/apache/qpid/client/TopicPublisherAdapter.java @@ -123,7 +123,7 @@ public class TopicPublisherAdapter implements TopicPublisher public void send(Destination dest, Message msg) throws JMSException { checkPreConditions(); - checkTopic(_topic); + checkTopic(dest); _delegate.send(dest, msg); } -- cgit v1.2.1