Example usage for javax.jms Session createDurableSubscriber

List of usage examples for javax.jms Session createDurableSubscriber

Introduction

In this page you can find the example usage for javax.jms Session createDurableSubscriber.

Prototype

TopicSubscriber createDurableSubscriber(Topic topic, String name, String messageSelector, boolean noLocal)
        throws JMSException;

Source Link

Document

Creates an unshared durable subscription on the specified topic (if one does not already exist), specifying a message selector and the noLocal parameter, and creates a consumer on that durable subscription.

Usage

From source file:org.logicblaze.lingo.jms.JmsServiceExporter.java

/**
 * Factory method to create the consumer
 *//*from w  w  w .  j  a va 2  s.  c  o m*/
protected MessageConsumer createConsumer() throws JMSException {
    Session session = getResponseRequestor().getSession();
    if (subscriberName != null) {
        if (destination instanceof Topic) {
            Topic topic = (Topic) destination;
            return session.createDurableSubscriber(topic, subscriberName, messageSelector, noLocal);
        } else {
            throw new IllegalArgumentException(
                    "Cannot specify the subscriberName property when using a Queue destination");
        }

    } else {
        return session.createConsumer(destination, messageSelector, noLocal);
    }
}

From source file:com.adaptris.core.jms.VendorImplementationImp.java

@Override
public MessageConsumer createTopicSubscriber(ConsumeDestination cd, String subscriptionId, JmsActorConfig c)
        throws JMSException {
    Session s = c.currentSession();
    Topic t = createTopic(cd.getDestination(), c);
    MessageConsumer result = null;//  w  w w.  jav a 2 s  .  c om
    if (!isEmpty(subscriptionId)) {
        result = s.createDurableSubscriber(t, subscriptionId, cd.getFilterExpression(), false);
    } else {
        result = s.createConsumer(t, cd.getFilterExpression());
    }
    return result;
}

From source file:com.eviware.soapui.impl.wsdl.submit.transports.jms.HermesJmsRequestTransport.java

protected TopicSubscriber createDurableSubscription(SubmitContext submitContext, Session topicSession,
        JMSConnectionHolder jmsConnectionHolder) throws JMSException, NamingException {

    Topic topicSubscribe = jmsConnectionHolder.getTopic(jmsConnectionHolder.getJmsEndpoint().getReceive());

    // create durable subscriber
    TopicSubscriber topicDurableSubsriber = topicSession.createDurableSubscriber(topicSubscribe,
            StringUtils.hasContent(durableSubscriptionName) ? durableSubscriptionName
                    : "durableSubscription" + jmsConnectionHolder.getJmsEndpoint().getReceive(),
            submitContext.expand(messageSelector), false);
    return topicDurableSubsriber;
}

From source file:com.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingWorker.java

@Override
public boolean subscribe(String jobname, String selector) {
    this.topic = getTopic();
    if (this.topic != null) {
        while (!Thread.currentThread().isInterrupted()) {
            try {
                if (!isConnected()) {
                    if (!connect()) {
                        return false;
                    }/*from w  ww  .j  a v a2  s  .c  om*/
                }
                if (subscriber == null) {
                    log.info("Subscribing job '" + jobname + "' to '" + this.topic + "' topic.");
                    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                    Topic destination = session.createTopic(this.topic);

                    subscriber = session.createDurableSubscriber(destination, jobname, selector, false);
                    log.info("Successfully subscribed job '" + jobname + "' to '" + this.topic
                            + "' topic with selector: " + selector);
                } else {
                    log.fine("Already subscribed to '" + this.topic + "' topic with selector: " + selector
                            + " for job '" + jobname);
                }
                return true;
            } catch (JMSException ex) {

                // Either we were interrupted, or something else went
                // wrong. If we were interrupted, then we will jump ship
                // on the next iteration. If something else happened,
                // then we just unsubscribe here, sleep, so that we may
                // try again on the next iteration.

                log.log(Level.SEVERE, "JMS exception raised while subscribing job '" + jobname
                        + "', retrying in " + RETRY_MINUTES + " minutes.", ex);
                if (!Thread.currentThread().isInterrupted()) {

                    unsubscribe(jobname);

                    try {
                        Thread.sleep(RETRY_MINUTES * 60 * 1000);
                    } catch (InterruptedException ie) {
                        // We were interrupted while waiting to retry.
                        // We will jump ship on the next iteration.

                        // NB: The interrupt flag was cleared when
                        // InterruptedException was thrown. We have to
                        // re-install it to make sure we eventually
                        // leave this thread.
                        Thread.currentThread().interrupt();
                    }
                }
            }
        }
    }
    return false;
}

From source file:nl.nn.adapterframework.jms.JMSFacade.java

private MessageConsumer getTopicSubscriber(Session session, Topic topic, String selector)
        throws NamingException, JMSException {
    MessageConsumer messageConsumer;/*from   w w w. j a v  a 2 s.c  om*/
    if (subscriberType.equalsIgnoreCase("DURABLE")) {
        messageConsumer = session.createDurableSubscriber(topic, destinationName, selector, false);
        if (log.isDebugEnabled())
            log.debug("[" + name + "] got durable subscriber for topic [" + destinationName
                    + "] with selector [" + selector + "]");
    } else {
        messageConsumer = session.createConsumer(topic, selector, false);
        if (log.isDebugEnabled())
            log.debug("[" + name + "] got transient subscriber for topic [" + destinationName
                    + "] with selector [" + selector + "]");
    }
    return messageConsumer;
}

From source file:org.apache.axis2.transport.jms.JMSUtils.java

/**
 * This is a JMS spec independent method to create a MessageConsumer. Please be cautious when
 * making any changes// ww  w.  ja  v  a 2 s .  c om
 *
 * @param session JMS session
 * @param destination the Destination
 * @param isQueue is the Destination a queue?
 * @param subscriberName optional client name to use for a durable subscription to a topic
 * @param messageSelector optional message selector
 * @param pubSubNoLocal should we receive messages sent by us during pub-sub?
 * @param isDurable is this a durable topic subscription?
 * @param jmsSpec11 should we use JMS 1.1 API ?
 * @return a MessageConsumer to receive messages
 * @throws JMSException on errors, to be handled and logged by the caller
 */
public static MessageConsumer createConsumer(Session session, Destination destination, Boolean isQueue,
        String subscriberName, String messageSelector, boolean pubSubNoLocal, boolean isDurable,
        boolean jmsSpec11) throws JMSException {

    if (jmsSpec11 || isQueue == null) {
        if (isDurable) {
            return session.createDurableSubscriber((Topic) destination, subscriberName, messageSelector,
                    pubSubNoLocal);
        } else {
            return session.createConsumer(destination, messageSelector, pubSubNoLocal);
        }
    } else {
        if (isQueue) {
            return ((QueueSession) session).createReceiver((Queue) destination, messageSelector);
        } else {
            if (isDurable) {
                return ((TopicSession) session).createDurableSubscriber((Topic) destination, subscriberName,
                        messageSelector, pubSubNoLocal);
            } else {
                return ((TopicSession) session).createSubscriber((Topic) destination, messageSelector,
                        pubSubNoLocal);
            }
        }
    }
}

From source file:org.mule.transport.jms.Jms11Support.java

public MessageConsumer createConsumer(Session session, Destination destination, String messageSelector,
        boolean noLocal, String durableName, boolean topic, ImmutableEndpoint endpoint) throws JMSException {
    if (durableName == null) {
        if (topic) {
            return session.createConsumer(destination, messageSelector, noLocal);
        } else {/*  ww w .  ja  v  a  2 s  .  co  m*/
            return session.createConsumer(destination, messageSelector);
        }
    } else {
        if (topic) {
            return session.createDurableSubscriber((Topic) destination, durableName, messageSelector, noLocal);
        } else {
            throw new JMSException("A durable subscriber name was set but the destination was not a Topic");
        }
    }
}

From source file:org.openadaptor.auxil.connector.jms.JMSReadConnector.java

protected MessageConsumer createMessageConsumerFor(Session connectorSession) {
    if (messageConsumer == null) {
        MessageConsumer newConsumer;/*from   ww  w  .  ja  v a2 s .c  om*/
        if (destination == null) {
            destination = jmsConnection.lookupDestination(getDestinationName());
        }
        try {
            if (durable) {
                newConsumer = connectorSession.createDurableSubscriber((Topic) destination,
                        getDurableSubscriptionName(), getMessageSelector(), isNoLocal());
            } else {
                if (isNoLocal()) { // The value of noLocal only seems to matter if it's true.
                    newConsumer = connectorSession.createConsumer(destination, getMessageSelector(),
                            isNoLocal());
                } else {
                    newConsumer = connectorSession.createConsumer(destination, getMessageSelector());
                }
            }
        } catch (JMSException e) {
            throw new ConnectionException("Unable to subscribe to Destination: [" + getDestinationName() + "]",
                    e, this);
        }
        messageConsumer = newConsumer;
    }
    return messageConsumer;
}

From source file:org.wso2.carbon.inbound.endpoint.protocol.jms.factory.JMSConnectionFactory.java

public MessageConsumer createMessageConsumer(Session session, Destination destination) {
    try {//  www .  ja  v  a  2s.com
        if (JMSConstants.JMS_SPEC_VERSION_2_0.equals(jmsSpec) && isSharedSubscription) {
            if (isDurable) {
                return session.createSharedDurableConsumer((Topic) destination, subscriptionName,
                        messageSelector);
            } else {
                return session.createSharedConsumer((Topic) destination, subscriptionName, messageSelector);
            }
        } else if ((JMSConstants.JMS_SPEC_VERSION_1_1.equals(jmsSpec))
                || (JMSConstants.JMS_SPEC_VERSION_2_0.equals(jmsSpec) && !isSharedSubscription)) {
            if (isDurable) {
                return session.createDurableSubscriber((Topic) destination, subscriptionName, messageSelector,
                        noPubSubLocal);
            } else {
                return session.createConsumer(destination, messageSelector);
            }
        } else {
            if (this.destinationType.equals(JMSConstants.JMSDestinationType.QUEUE)) {
                return ((QueueSession) session).createReceiver((Queue) destination, messageSelector);
            } else {
                if (isDurable) {
                    return ((TopicSession) session).createDurableSubscriber((Topic) destination,
                            subscriptionName, messageSelector, noPubSubLocal);
                } else {
                    return ((TopicSession) session).createSubscriber((Topic) destination, messageSelector,
                            false);
                }
            }
        }
    } catch (JMSException e) {
        logger.error("JMS Exception while creating consumer. " + e.getMessage(), e);
    }
    return null;
}

From source file:tools.ConsumerTool.java

@Override
public void run() {
    Connection connection = null;
    Session session = null;
    try {/*  w  w w .j av a 2  s.  co  m*/
        connection = connectionFactory.createConnection();
        if (clientId != null) {
            connection.setClientID(clientId);
        }
        connection.start();
        session = connection.createSession(transacted, acknowledgeMode);
        Destination destination = null;
        if (jndiLookupDestinations) {
            destination = (Destination) context.lookup(destinationName);
        } else {
            if (useQueueDestinations) {
                if (useTemporaryDestinations) {
                    destination = session.createTemporaryQueue();
                } else {
                    destination = session.createQueue(destinationName);
                }
            } else {
                if (useTemporaryDestinations) {
                    destination = session.createTemporaryTopic();
                } else {
                    destination = session.createTopic(destinationName);
                }
            }
        }

        if (useQueueBrowser) {
            runQueueBrowser(session, (Queue) destination);
        } else {
            MessageConsumer consumer = null;
            if (useQueueDestinations) { //Queues
                if (selector != null) {
                    consumer = session.createConsumer(destination, selector);
                } else {
                    consumer = session.createConsumer(destination);
                }
            } else { //Queues
                if (durable) { //Durable Subscribers
                    if (selector != null) {
                        consumer = session.createDurableSubscriber((Topic) destination, subscriptionName,
                                selector, false);
                    } else {
                        consumer = session.createDurableSubscriber((Topic) destination, subscriptionName);
                    }
                } else { //Non-Durable Subscribers
                    if (selector != null) {
                        consumer = session.createConsumer(destination, selector);
                    } else {
                        consumer = session.createConsumer(destination);
                    }
                }
            }

            if (useAsyncListener) {
                final Session consumerSession = session;

                final AtomicInteger perConsumerReceivedMessages = new AtomicInteger(0);
                consumer.setMessageListener(new MessageListener() {

                    @Override
                    public void onMessage(Message message) {
                        perConsumerReceivedMessages.incrementAndGet();
                        handleMessage(consumerSession, message, perConsumerReceivedMessages.get());
                    }
                });
                while (perConsumerReceivedMessages.get() < numMessages) {
                    Thread.sleep(100);
                }
            } else {
                int perConsumerReceivedMessages = 0;
                while (perConsumerReceivedMessages < numMessages) {

                    Message message = null;
                    if (receiveTimeoutMS > -1) {
                        message = consumer.receive(receiveTimeoutMS);
                    } else {
                        message = consumer.receive();
                    }

                    if (message != null) {
                        perConsumerReceivedMessages++;
                        handleMessage(session, message, perConsumerReceivedMessages);
                    }
                }
            }
            consumer.close();
        }

    } catch (Exception ex) {
        LOGGER.error("ConsumerTool hit exception: " + ex.getMessage(), ex);
    } finally {
        if (session != null) {
            try {
                session.close();
            } catch (JMSException e) {
                LOGGER.error("JMSException closing session", e);
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                LOGGER.error("JMSException closing connection", e);
            }
        }
    }
}