Class ReceiveSubscriber

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class ReceiveSubscriber
    extends Object
    implements Closeable
    Generic MessageConsumer class, which has two possible strategies.
    • Use MessageConsumer.receive(timeout) to fetch messages.
    • Use MessageListener.onMessage() to cache messages in a local queue.
    In both cases, the getMessage(long) method is used to return the next message, either directly using receive(timeout) or from the queue using poll(timeout).
    • Constructor Summary

      Constructors 
      Constructor Description
      ReceiveSubscriber​(boolean useProps, String initialContextFactory, String providerUrl, String connfactory, String destinationName, String durableSubscriptionId, String clientId, String jmsSelector, boolean useAuth, String securityPrincipal, String securityCredentials)
      Constructor takes the necessary JNDI related parameters to create a connection and prepare to begin receiving messages.
      ReceiveSubscriber​(int queueSize, boolean useProps, String initialContextFactory, String providerUrl, String connfactory, String destinationName, String durableSubscriptionId, String clientId, String jmsSelector, boolean useAuth, String securityPrincipal, String securityCredentials)
      Constructor takes the necessary JNDI related parameters to create a connection and create an onMessageListener to prepare to begin receiving messages.
    • Constructor Detail

      • ReceiveSubscriber

        public ReceiveSubscriber​(boolean useProps,
                                 String initialContextFactory,
                                 String providerUrl,
                                 String connfactory,
                                 String destinationName,
                                 String durableSubscriptionId,
                                 String clientId,
                                 String jmsSelector,
                                 boolean useAuth,
                                 String securityPrincipal,
                                 String securityCredentials)
                          throws NamingException,
                                 JMSException
        Constructor takes the necessary JNDI related parameters to create a connection and prepare to begin receiving messages.
        The caller must then invoke start() to enable message reception.
        Parameters:
        useProps - if true, use jndi.properties instead of initialContextFactory, providerUrl, securityPrincipal, securityCredentials
        initialContextFactory - name of the initial context factory (will be ignored if useProps is true)
        providerUrl - url of the provider (will be ignored if useProps is true)
        connfactory - name of the object factory to look up in context
        destinationName - name of the destination
        durableSubscriptionId - id for a durable subscription (if empty or null no durable subscription will be done)
        clientId - client id to use (may be empty or null)
        jmsSelector - Message Selector
        useAuth - flag whether auth should be used (will be ignored if useProps is true)
        securityPrincipal - name of the principal to use for auth (will be ignored if useProps is true)
        securityCredentials - credentials for the principal (will be ignored if useProps is true)
        Throws:
        JMSException - if could not create context or other problem occurred.
        NamingException - when lookup of context or destination fails
      • ReceiveSubscriber

        public ReceiveSubscriber​(int queueSize,
                                 boolean useProps,
                                 String initialContextFactory,
                                 String providerUrl,
                                 String connfactory,
                                 String destinationName,
                                 String durableSubscriptionId,
                                 String clientId,
                                 String jmsSelector,
                                 boolean useAuth,
                                 String securityPrincipal,
                                 String securityCredentials)
                          throws NamingException,
                                 JMSException
        Constructor takes the necessary JNDI related parameters to create a connection and create an onMessageListener to prepare to begin receiving messages.
        The caller must then invoke start() to enable message reception.
        Parameters:
        queueSize - maximum queue size, where a queueSize <=0 means no limit
        useProps - if true, use jndi.properties instead of initialContextFactory, providerUrl, securityPrincipal, securityCredentials
        initialContextFactory - name of the initial context factory (will be ignored if useProps is true)
        providerUrl - url of the provider (will be ignored if useProps is true)
        connfactory - name of the object factory to look up in context
        destinationName - name of the destination
        durableSubscriptionId - id for a durable subscription (if empty or null no durable subscription will be done)
        clientId - client id to use (may be empty or null)
        jmsSelector - Message Selector
        useAuth - flag whether auth should be used (will be ignored if useProps is true)
        securityPrincipal - name of the principal to use for auth (will be ignored if useProps is true)
        securityCredentials - credentials for the principal (will be ignored if useProps is true)
        Throws:
        JMSException - if could not create context or other problem occurred.
        NamingException - when lookup of context or destination fails
    • Method Detail

      • start

        public void start()
                   throws JMSException
        Calls Connection.start() to begin receiving inbound messages.
        Throws:
        JMSException - when starting the context fails
      • stop

        public void stop()
                  throws JMSException
        Calls Connection.stop() to stop receiving inbound messages.
        Throws:
        JMSException - when stopping the context fails
      • getMessage

        public Message getMessage​(long timeout)
                           throws JMSException
        Get the next message or null.

        Never blocks for longer than the specified timeout.

        Parameters:
        timeout - in milliseconds
        Returns:
        the next message or null
        Throws:
        JMSException - when receiving the message fails
      • close

        public void close()
        close() will stop the connection first. Then it closes the subscriber, session and connection.
        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
      • onMessage

        public void onMessage​(Message message)