Example usage for javax.jms TopicSession createDurableSubscriber

List of usage examples for javax.jms TopicSession createDurableSubscriber

Introduction

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

Prototype

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

Source Link

Document

Creates an unshared durable subscription on the specified topic (if one does not already exist) and creates a consumer on that durable subscription.

Usage

From source file:org.apache.activemq.usecases.DurableSubscriptionHangTestCase.java

private void registerDurableSubscription() throws JMSException {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://" + brokerName);
    TopicConnection connection = connectionFactory.createTopicConnection();
    connection.setClientID(clientID);/*from   w w  w.java2s .c om*/
    TopicSession topicSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic topic = topicSession.createTopic(topicName);
    TopicSubscriber durableSubscriber = topicSession.createDurableSubscriber(topic, durableSubName);
    connection.start();
    durableSubscriber.close();
    connection.close();
    LOG.info("Durable Sub Registered");
}

From source file:org.apache.activemq.usecases.DurableSubscriptionHangTestCase.java

private Message collectMessagesFromDurableSubscriptionForOneMinute() throws Exception {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://" + brokerName);
    TopicConnection connection = connectionFactory.createTopicConnection();

    connection.setClientID(clientID);// w  w  w  . j a  v a 2  s. com
    TopicSession topicSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic topic = topicSession.createTopic(topicName);
    connection.start();
    TopicSubscriber subscriber = topicSession.createDurableSubscriber(topic, durableSubName);
    LOG.info("About to receive messages");
    Message message = subscriber.receive(120000);
    subscriber.close();
    connection.close();
    LOG.info("collectMessagesFromDurableSubscriptionForOneMinute done");

    return message;
}

From source file:com.zotoh.maedr.device.JmsIO.java

private void inizTopic(Context ctx, Object obj) throws Exception {

    TopicConnectionFactory f = (TopicConnectionFactory) obj;
    final JmsIO me = this;
    TopicConnection conn;//from ww  w .  j  av  a 2 s  .co  m
    Topic t = (Topic) ctx.lookup(_dest);

    if (!isEmpty(_jmsUser)) {
        conn = f.createTopicConnection(_jmsUser, _jmsPwd);
    } else {
        conn = f.createTopicConnection();
    }

    _conn = conn;

    TopicSession s = conn.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);
    TopicSubscriber b;

    if (_durable) {
        b = s.createDurableSubscriber(t, GUID.generate());
    } else {
        b = s.createSubscriber(t);
    }

    b.setMessageListener(new MessageListener() {
        public void onMessage(Message msg) {
            me.onMessage(msg);
        }
    });
}

From source file:org.wso2.carbon.appfactory.eventing.jms.Subscriber.java

public void subscribe() throws AppFactoryEventException {

    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, ANDES_ICF);
    properties.put(CF_NAME_PREFIX + CF_NAME, Util.getTCPConnectionURL());
    properties.put(CarbonConstants.REQUEST_BASE_CONTEXT, "true");
    properties.put("topic", topicName);

    try {//from w  w w . jav a2 s .com
        ctx = new InitialContext(properties);
        // Lookup connection factory
        connFactory = (TopicConnectionFactory) ctx.lookup(CF_NAME);
        topicConnection = connFactory.createTopicConnection();
        TopicSession topicSession = topicConnection.createTopicSession(false, TopicSession.CLIENT_ACKNOWLEDGE);
        // create durable subscriber with subscription ID
        Topic topic = null;
        try {
            topic = (Topic) ctx.lookup(topicName);
        } catch (NamingException e) {
            topic = topicSession.createTopic(topicName);
        }
        TopicSubscriber topicSubscriber = topicSession.createDurableSubscriber(topic, subscriptionId);
        topicSubscriber.setMessageListener(messageListener);
        topicConnection.start();
    } catch (NamingException e) {
        String errorMsg = "Failed to subscribe to topic:" + topicName + " due to " + e.getMessage();
        throw new AppFactoryEventException(errorMsg, e);
    } catch (JMSException e) {
        String errorMsg = "Failed to subscribe to topic:" + topicName + " due to " + e.getMessage();
        throw new AppFactoryEventException(errorMsg, e);
    }

}

From source file:org.wso2.carbon.appfactory.resource.mgt.listeners.TenantCreationDurableSubscriber.java

/**
 * Subscribe as a durable subscriber to the topic.
 *
 * @throws AppFactoryEventException//from  w  w w .j  a v a  2s  . co  m
 */
public void subscribe() throws AppFactoryEventException {
    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, AppFactoryConstants.ANDES_ICF);
    properties.put(AppFactoryConstants.CF_NAME_PREFIX + AppFactoryConstants.CF_NAME,
            Util.getTCPConnectionURL());
    properties.put(CarbonConstants.REQUEST_BASE_CONTEXT, "true");
    properties.put(AppFactoryConstants.TOPIC, topicName);
    TopicConnectionFactory connFactory;
    TopicConnection topicConnection;
    TopicSession topicSession;
    TopicSubscriber topicSubscriber;
    InitialContext ctx;
    try {
        ctx = new InitialContext(properties);
        connFactory = (TopicConnectionFactory) ctx.lookup(AppFactoryConstants.CF_NAME);
        topicConnection = connFactory.createTopicConnection();
        topicSession = topicConnection.createTopicSession(false, TopicSession.CLIENT_ACKNOWLEDGE);
        Topic topic;
        try {
            topic = (Topic) ctx.lookup(topicName);
        } catch (NamingException e) {
            topic = topicSession.createTopic(topicName);
        }
        topicSubscriber = topicSession.createDurableSubscriber(topic, subscriptionId);
        topicSubscriber.setMessageListener(
                new TenantCreationMessageListener(topicConnection, topicSession, topicSubscriber));
        topicConnection.start();
        if (log.isDebugEnabled()) {
            log.debug("Durable Subscriber created for topic " + topicName + " with subscription id : "
                    + subscriptionId);
        }
    } catch (NamingException e) {
        throw new AppFactoryEventException("Failed to subscribe to topic : " + topicName
                + " with subscription id" + " : " + subscriptionId, e);
    } catch (JMSException e) {
        throw new AppFactoryEventException("Failed to subscribe to topic : " + topicName
                + " with subscription id" + " : " + subscriptionId, e);
    }
}

From source file:org.wso2.carbon.appfactory.stratos.listeners.StratosSubscriptionDurableSubscriber.java

/**
 * Subscribe as a durable subscriber to the topic.
 *
 * @throws AppFactoryEventException/*from  w w w.j  av a 2s .  c o m*/
 */
public void subscribe() throws AppFactoryEventException {
    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, AppFactoryConstants.ANDES_ICF);
    properties.put(AppFactoryConstants.CF_NAME_PREFIX + AppFactoryConstants.CF_NAME,
            Util.getTCPConnectionURL());
    properties.put(CarbonConstants.REQUEST_BASE_CONTEXT, true);
    properties.put(AppFactoryConstants.TOPIC, topicName);
    TopicConnectionFactory connFactory;
    TopicConnection topicConnection;
    TopicSession topicSession;
    TopicSubscriber topicSubscriber;
    InitialContext ctx;
    try {
        ctx = new InitialContext(properties);
        connFactory = (TopicConnectionFactory) ctx.lookup(AppFactoryConstants.CF_NAME);
        topicConnection = connFactory.createTopicConnection();
        topicSession = topicConnection.createTopicSession(false, TopicSession.CLIENT_ACKNOWLEDGE);
        Topic topic;
        try {
            topic = (Topic) ctx.lookup(topicName);
        } catch (NamingException e) {
            topic = topicSession.createTopic(topicName);
        }
        topicSubscriber = topicSession.createDurableSubscriber(topic, subscriptionId);
        topicSubscriber.setMessageListener(
                new StratosSubscriptionMessageListener(topicConnection, topicSession, topicSubscriber));
        topicConnection.start();
        if (log.isDebugEnabled()) {
            log.debug("Durable Subscriber created for topic " + topicName + " with subscription id : "
                    + subscriptionId);
        }
    } catch (NamingException e) {
        throw new AppFactoryEventException("Failed to subscribe to topic : " + topicName
                + " with subscription id" + " : " + subscriptionId, e);
    } catch (JMSException e) {
        throw new AppFactoryEventException("Failed to subscribe to topic : " + topicName
                + " with subscription id" + " : " + subscriptionId, e);
    }
}

From source file:org.wso2.carbon.appfactory.eventing.jms.TopicPublisher.java

public void publishMessage(Event event) throws AppFactoryEventException {
    Properties properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY, QPID_ICF);
    properties.put(CF_NAME_PREFIX + CF_NAME, Util.getTCPConnectionURL());
    properties.put(CarbonConstants.REQUEST_BASE_CONTEXT, "true");
    try {//from   w  w w  .  j  av  a 2 s.c o m
        ctx = new InitialContext(properties);
        connFactory = (TopicConnectionFactory) ctx.lookup(CF_NAME);
    } catch (NamingException e) {
        throw new AppFactoryEventException("Failed to initialize InitialContext.", e);
    }

    TopicConnection topicConnection = null;
    TopicSession topicSession = null;
    try {
        topicConnection = connFactory.createTopicConnection();
        topicSession = topicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
        // Send message
        String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
        Topic topic = topicSession.createTopic(event.getTarget());

        //Until MB supports 'Dynamic Topics' we have to create a subscription, therefore forcing Message broker to
        // create the topic.
        String defaultSubscriptionId = tenantDomain + "/" + DEFAULT_SUBSCRIPTION + UUID.randomUUID();
        topicSubscriber = topicSession.createDurableSubscriber(topic, defaultSubscriptionId);
        // We are unsubscribing from the Topic as soon as
        topicSession.unsubscribe(defaultSubscriptionId);

        // create the message to send
        MapMessage mapMessage = topicSession.createMapMessage();
        mapMessage.setString(MESSAGE_TITLE, event.getMessageTitle());
        mapMessage.setString(MESSAGE_BODY, event.getMessageBody());
        javax.jms.TopicPublisher topicPublisher = topicSession.createPublisher(topic);
        topicConnection.start();
        topicPublisher.publish(mapMessage);
        if (log.isDebugEnabled()) {
            log.debug("Message with Id:" + mapMessage.getJMSMessageID() + ", with title:"
                    + event.getMessageTitle() + " was successfully published.");
        }

    } catch (JMSException e) {
        log.error("Failed to publish message due to " + e.getMessage(), e);
        throw new AppFactoryEventException("Failed to publish message due to " + e.getMessage(), e);
    } finally {
        if (topicSubscriber != null) {
            try {
                topicSubscriber.close();
            } catch (JMSException e) {
                log.error("Failed to close default topic subscriber", e);
            }
        }
        if (topicSession != null) {
            try {
                topicSession.close();
            } catch (JMSException e) {
                log.error("Failed to close topic session", e);
            }
        }
        if (topicConnection != null) {
            try {
                topicConnection.close();
            } catch (JMSException e) {
                log.error("Failed to close topic connection", e);
            }
        }
    }
}

From source file:org.wso2.carbon.event.core.internal.delivery.jms.JMSDeliveryManager.java

public void subscribe(Subscription subscription) throws EventBrokerException {

    if (isDeactivated()) {
        return;/* w  ww . j a v a 2s .co  m*/
    }

    // in a multi tenant envirionment deployment synchronizer may creates subscriptions before
    // the event observer get activated.
    if (this.subscriptionIDSessionDetailsMap.containsKey(subscription.getId())) {
        log.warn(
                "There is an subscription already exists for the subscription with id " + subscription.getId());
        return;
    }
    JMSMessageListener jmsMessageListener = new JMSMessageListener(this.notificationManager, subscription);
    try {
        TopicConnection topicConnection = getTopicConnection(subscription.getOwner());
        TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

        String topicName = "";
        if (subscription.getTenantDomain() != null && (!subscription.getTenantDomain()
                .equals(org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME))) {

            if (!subscription.getTopicName().startsWith("/")) {
                topicName = getTopicName(subscription.getTenantDomain() + "/" + subscription.getTopicName());
            } else {
                topicName = getTopicName(subscription.getTenantDomain() + subscription.getTopicName());
            }
        } else {
            topicName = getTopicName(subscription.getTopicName());
        }
        Topic topic = topicSession.createTopic(topicName);
        //Some times we are not getting the proper topic with the required syntax, if it is not
        //appropriate we need to check and add the BURL syntax to fix the issue https://wso2.org/jira/browse/MB-185
        if (!topic.toString().startsWith("topic://amq.topic")) {
            topic = topicSession.createTopic("BURL:" + topicName);
        }
        TopicSubscriber topicSubscriber = topicSession.createDurableSubscriber(topic, subscription.getId());
        topicSubscriber.setMessageListener(jmsMessageListener);

        this.subscriptionIDSessionDetailsMap.put(subscription.getId(),
                new JMSSubscriptionDetails(topicSubscriber, topicSession, topicConnection));
    } catch (JMSException e) {
        throw new EventBrokerException(
                "Can not subscribe to topic " + subscription.getTopicName() + " " + e.getMessage(), e);
    }
}

From source file:org.wso2.carbon.andes.event.core.internal.delivery.jms.JMSDeliveryManager.java

/**
 * {@inheritDoc}/*from w  w  w .  ja  va2 s  .co m*/
 */
public void subscribe(Subscription subscription) throws EventBrokerException {

    if (isDeactivated()) {
        return;
    }

    // in a multi tenant environment deployment synchronize may creates subscriptions before
    // the event observer get activated.
    if (this.subscriptionIDSessionDetailsMap.containsKey(subscription.getId())) {
        log.warn(
                "There is an subscription already exists for the subscription with id " + subscription.getId());
        return;
    }
    JMSMessageListener jmsMessageListener = new JMSMessageListener(this.notificationManager, subscription);
    try {
        TopicConnection topicConnection = getTopicConnection(subscription.getOwner());
        TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

        String topicName = "";
        if (subscription.getTenantDomain() != null && (!subscription.getTenantDomain()
                .equals(org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME))) {

            if (!subscription.getTopicName().startsWith("/")) {
                topicName = getTopicName(subscription.getTenantDomain() + "/" + subscription.getTopicName());
            } else {
                topicName = getTopicName(subscription.getTenantDomain() + subscription.getTopicName());
            }
        } else {
            topicName = getTopicName(subscription.getTopicName());
        }
        Topic topic = topicSession.createTopic(topicName);
        //Some times we are not getting the proper topic with the required syntax, if it is not
        //appropriate we need to check and add the BURL syntax to fix the issue https://wso2.org/jira/browse/MB-185
        if (!topic.toString().startsWith("topic://amq.topic")) {
            topic = topicSession.createTopic("BURL:" + topicName);
        }
        TopicSubscriber topicSubscriber = topicSession.createDurableSubscriber(topic, subscription.getId());
        topicSubscriber.setMessageListener(jmsMessageListener);

        this.subscriptionIDSessionDetailsMap.put(subscription.getId(),
                new JMSSubscriptionDetails(topicSubscriber, topicSession, topicConnection));
    } catch (JMSException e) {
        throw new EventBrokerException(
                "Can not subscribe to topic " + subscription.getTopicName() + " " + e.getMessage(), e);
    }
}