Example usage for javax.jms TopicPublisher close

List of usage examples for javax.jms TopicPublisher close

Introduction

In this page you can find the example usage for javax.jms TopicPublisher close.

Prototype


void close() throws JMSException;

Source Link

Document

Closes the message producer.

Usage

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

protected String sendByTopic(TopicSession session, Topic destination, Message message)
        throws NamingException, JMSException {
    TopicPublisher tps = session.createPublisher(destination);
    tps.publish(message);//www.ja v  a 2 s  .  c  o  m
    tps.close();
    return message.getJMSMessageID();
}

From source file:org.grouter.common.jms.TopicListenerDestination.java

/**
 * <br>//from  ww w  . j av  a  2s.  c om
 */
public void sendReplyToTemporaryDestination(Message request) {
    TemporaryTopic replyTopic = null;
    TopicPublisher tempsender = null;
    String temporaryDestinationName = null;
    try {
        if (request.getJMSReplyTo() == null) {
            throw new IllegalStateException("The sender of this message has not entered a JMSReplyTo field - "
                    + "impossible to send reply on temporary destination!!");
        }
        temporaryDestinationName = request.getJMSReplyTo().toString();
        request.setJMSCorrelationID(request.getJMSMessageID());
        logger.debug("JMSCorrelationID was set!!!" + request.getJMSCorrelationID());
        replyTopic = (TemporaryTopic) request.getJMSReplyTo();
        tempsender = topicSession.createPublisher(replyTopic);
        logger.debug("Created a tempsender and sending reply to " + replyTopic);
        tempsender.send(request);
    } catch (JMSException ex) {
        //ignore
        logger.warn("Failed sending reply on temporary destination : " + temporaryDestinationName);
    } finally {
        try {
            if (tempsender != null) {
                tempsender.close();
            }
            if (replyTopic != null) {
                replyTopic.delete();
            }
        } catch (JMSException ex1) {
            //ignore
        }
    }
}

From source file:org.nuxeo.ecm.core.event.jms.JmsEventForwarder.java

protected void produceJMSMessage(SerializableEventBundle message) throws JMSBusNotActiveException {
    InitialContext ctx;/*from  w  w  w.  j  a v  a  2  s.  c  o  m*/
    Topic nuxeoTopic;
    try {
        ctx = new InitialContext();
        nuxeoTopic = (Topic) ctx.lookup(NUXEO_JMS_TOPIC);
    } catch (NamingException e) {
        jmsBusIsActive = false;
        throw new JMSBusNotActiveException(e);
    }

    TopicConnection nuxeoTopicConnection = null;
    TopicSession nuxeoTopicSession = null;
    TopicPublisher nuxeoMessagePublisher = null;
    try {
        TopicConnectionFactory factory = (TopicConnectionFactory) ctx.lookup("TopicConnectionFactory");
        nuxeoTopicConnection = factory.createTopicConnection();
        nuxeoTopicSession = nuxeoTopicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);

        ObjectMessage jmsMessage = nuxeoTopicSession.createObjectMessage(message);

        // add Headers for JMS message
        jmsMessage.setStringProperty("BundleEvent", message.getEventBundleName());

        nuxeoMessagePublisher = nuxeoTopicSession.createPublisher(nuxeoTopic);

        nuxeoMessagePublisher.send(jmsMessage);
        log.debug("Event bundle " + message.getEventBundleName() + " forwarded to JMS topic");

    } catch (Exception e) {
        log.error("Error during JMS forwarding", e);
    } finally {
        if (nuxeoTopicSession != null) {
            try {
                if (nuxeoMessagePublisher != null) {
                    nuxeoMessagePublisher.close();
                }
                nuxeoTopicConnection.close();
                nuxeoTopicSession.close();
            } catch (JMSException e) {
                log.error("Error during JMS cleanup", e);
            }
        }
    }
}

From source file:org.nuxeo.ecm.core.jms.CoreEventPublisher.java

public void publish(Object content, Topic topic, MessageFactory factory, String eventId) throws JMSException {
    TopicConnection connection = null;/*from  w w  w  .  j a v a  2s .  co  m*/
    TopicSession session = null;
    TopicPublisher publisher = null;
    try {
        // get a connection from topic connection pool
        connection = getTopicConnection();

        // create a not transacted session
        session = connection.createTopicSession(transacted, TopicSession.AUTO_ACKNOWLEDGE);

        // create the publisher
        publisher = session.createPublisher(topic);
        publisher.setDeliveryMode(isDeliveryPersistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT);
        publisher.setDisableMessageID(isDisableMessageID);
        publisher.setDisableMessageTimestamp(isDisableMessageTimestamp);
        // create the message using the given factory
        Message msg = factory.createMessage(session, content);
        if (eventId != null) {
            msg.setStringProperty("NuxeoEventId", eventId);
        }
        // publish the message
        publisher.publish(topic, msg);
    } finally {
        if (publisher != null) {
            publisher.close();
        }
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}

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

/**
 * {@inheritDoc}//from w w w.  jav  a2  s . c om
 */
public void publish(Message message, String topicName, int deliveryMode) throws EventBrokerException {

    if (isDeactivated()) {
        return;
    }

    try {
        String userName = getLoggedInUserName();
        if ((userName == null) || (userName.equals(""))) {
            // use the system user name
            userName = CarbonConstants.REGISTRY_SYSTEM_USERNAME;
        }

        TopicConnection topicConnection = getTopicConnection(userName);
        TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

        String tenantDomain = EventBrokerHolder.getInstance().getTenantDomain();
        if (tenantDomain != null
                && (!tenantDomain.equals(org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME))) {
            topicName = tenantDomain + "/" + getTopicName(topicName);
        } else {
            topicName = getTopicName(topicName);
        }

        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);
        }
        TopicPublisher topicPublisher = topicSession.createPublisher(topic);
        topicPublisher.setDeliveryMode(deliveryMode);
        TextMessage textMessage = topicSession.createTextMessage(message.getMessage());

        Map<String, String> properties = message.getProperties();
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            textMessage.setStringProperty(entry.getKey(), entry.getValue());
        }

        // saving the domain to be used send with the soap header
        if (CarbonContext.getThreadLocalCarbonContext().getTenantDomain() != null) {
            textMessage.setStringProperty(MultitenantConstants.TENANT_DOMAIN_HEADER_NAME,
                    CarbonContext.getThreadLocalCarbonContext().getTenantDomain());
        }

        topicPublisher.publish(textMessage);
        topicPublisher.close();
        topicSession.close();
        topicConnection.stop();
        topicConnection.close();
    } catch (JMSException e) {
        throw new EventBrokerException("Can not publish to topic " + topicName + " " + e.getMessage(), e);
    }
}

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

public void publish(Message message, String topicName, int deliveryMode) throws EventBrokerException {

    if (isDeactivated()) {
        return;//w w  w  .jav  a2s .co m
    }

    try {
        String userName = getLoggedInUserName();
        if ((userName == null) || (userName.equals(""))) {
            // use the system user name
            userName = CarbonConstants.REGISTRY_SYSTEM_USERNAME;
        }

        TopicConnection topicConnection = getTopicConnection(userName);
        TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

        String tenantDomain = EventBrokerHolder.getInstance().getTenantDomain();
        if (tenantDomain != null
                && (!tenantDomain.equals(org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME))) {
            if (!topicName.startsWith("/")) {
                topicName = getTopicName(tenantDomain + "/" + topicName);
            } else {
                topicName = getTopicName(tenantDomain + topicName);
            }
        } else {
            topicName = getTopicName(topicName);
        }

        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);
        }
        TopicPublisher topicPublisher = topicSession.createPublisher(topic);
        topicPublisher.setDeliveryMode(deliveryMode);
        TextMessage textMessage = topicSession.createTextMessage(message.getMessage().toString());

        Map<String, String> properties = message.getProperties();
        for (String key : properties.keySet()) {
            textMessage.setStringProperty(key, properties.get(key));
        }

        // saving the domain to be used send with the soap header
        if (CarbonContext.getThreadLocalCarbonContext().getTenantDomain() != null) {
            textMessage.setStringProperty(MultitenantConstants.TENANT_DOMAIN_HEADER_NAME,
                    CarbonContext.getThreadLocalCarbonContext().getTenantDomain());
        }

        topicPublisher.publish(textMessage);
        topicPublisher.close();
        topicSession.close();
        topicConnection.stop();
        topicConnection.close();
    } catch (JMSException e) {
        throw new EventBrokerException("Can not publish to topic " + topicName + " " + e.getMessage(), e);
    }
}