Search in sources :

Example 1 with BrokerException

use of org.wso2.carbon.apimgt.core.exception.BrokerException in project carbon-apimgt by wso2.

the class BrokerUtil method publishToTopic.

/**
 * Publish to broker topic
 *
 * @param topicName     publishing topic name
 * @param gatewayEvent    topic message data object
 */
public static void publishToTopic(String topicName, GatewayEvent gatewayEvent) throws GatewayException {
    TopicSession topicSession = null;
    Topic topic = null;
    TopicPublisher topicPublisher = null;
    TopicConnection topicConnection = null;
    try {
        topicConnection = getTopicConnection();
        topicConnection.start();
        topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        topic = topicSession.createTopic(topicName);
        topicPublisher = topicSession.createPublisher(topic);
        TextMessage textMessage = topicSession.createTextMessage(new Gson().toJson(gatewayEvent));
        topicPublisher.publish(textMessage);
    } catch (JMSException e) {
        String errorMessage = "Error occurred while publishing " + gatewayEvent.getEventType() + " event to JMS " + "topic :" + topicName;
        log.error(errorMessage, e);
        throw new GatewayException(errorMessage, ExceptionCodes.GATEWAY_EXCEPTION);
    } catch (BrokerException e) {
        String errorMessage = "Error occurred while obtaining broker topic connection for topic : " + topicName;
        log.error(errorMessage, e);
        throw new GatewayException(errorMessage, ExceptionCodes.GATEWAY_EXCEPTION);
    } finally {
        if (topicPublisher != null) {
            try {
                topicPublisher.close();
            } catch (JMSException e) {
                log.error("Error occurred while closing topic publisher for topic : " + topicName);
            }
        }
        if (topicSession != null) {
            try {
                topicSession.close();
            } catch (JMSException e) {
                log.error("Error occurred while closing topic session for topic : " + topicName);
            }
        }
        if (topicConnection != null) {
            try {
                topicConnection.close();
            } catch (JMSException e) {
                log.error("Error occurred while closing topic connection for topic : " + topicName);
            }
        }
    }
}
Also used : TopicSession(javax.jms.TopicSession) BrokerException(org.wso2.carbon.apimgt.core.exception.BrokerException) TopicPublisher(javax.jms.TopicPublisher) GatewayException(org.wso2.carbon.apimgt.core.exception.GatewayException) Gson(com.google.gson.Gson) JMSException(javax.jms.JMSException) Topic(javax.jms.Topic) TopicConnection(javax.jms.TopicConnection) TextMessage(javax.jms.TextMessage)

Aggregations

Gson (com.google.gson.Gson)1 JMSException (javax.jms.JMSException)1 TextMessage (javax.jms.TextMessage)1 Topic (javax.jms.Topic)1 TopicConnection (javax.jms.TopicConnection)1 TopicPublisher (javax.jms.TopicPublisher)1 TopicSession (javax.jms.TopicSession)1 BrokerException (org.wso2.carbon.apimgt.core.exception.BrokerException)1 GatewayException (org.wso2.carbon.apimgt.core.exception.GatewayException)1