List of usage examples for javax.jms TopicConnection close
void close() throws JMSException;
From source file:org.wso2.carbon.andes.event.core.internal.delivery.jms.JMSDeliveryManager.java
/** * {@inheritDoc}/*from ww w . j a v a 2 s .c o m*/ */ 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.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 {/* w w w. j a va 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.appfactory.s4.integration.TenantStratosSubscriptionMessagePublisher.java
/** * Publish message to MB/ActiveMQ Queue// ww w . j av a2s . co m * @param runtimeJson runtimebeans as a json * @param tenantInfoJson tenantInfoBean as a json * @param restServiceProperties propertyMap * @param stage current stage * @throws AppFactoryEventException */ public void publishMessage(String runtimeJson, String tenantInfoJson, Map<String, String> restServiceProperties, String stage) 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(AppFactoryConstants.TOPIC, topicName); TopicConnection topicConnection = null; TopicSession topicSession = null; try { ctx = new InitialContext(properties); connFactory = (TopicConnectionFactory) ctx.lookup(AppFactoryConstants.CF_NAME); topicConnection = connFactory.createTopicConnection(); topicConnection.start(); topicSession = topicConnection.createTopicSession(false, TopicSession.CLIENT_ACKNOWLEDGE); Topic topic = topicSession.createTopic(topicName); MapMessage mapMessage = topicSession.createMapMessage(); mapMessage.setString(AppFactoryConstants.STAGE, stage); mapMessage.setString(AppFactoryConstants.RUNTIMES_INFO, runtimeJson); mapMessage.setString(AppFactoryConstants.TENANT_INFO, tenantInfoJson); javax.jms.TopicPublisher topicPublisher = topicSession.createPublisher(topic); topicPublisher.publish(mapMessage); //TODO remove this log log.info("Message with Id:" + mapMessage.getJMSMessageID() + " was successfully published to the" + " topic " + topicName); if (log.isDebugEnabled()) { log.debug("Message with Id:" + mapMessage.getJMSMessageID() + " was successfully published to the" + " topic " + topicName); } } catch (NamingException e) { String msg = "Failed to initialize InitialContext"; throw new AppFactoryEventException(msg, e); } catch (JMSException e) { String msg = "Failed to publish message due to " + e.getMessage(); throw new AppFactoryEventException(msg, e); } finally { 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 publish(Message message, String topicName, int deliveryMode) throws EventBrokerException { if (isDeactivated()) { return;/*from w w w . j av a2 s .com*/ } 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); } }
From source file:org.wso2.carbon.integration.test.client.JMSConsumerClient.java
public void run() { // create topic connection TopicConnection topicConnection = null; try {/*from w w w .j a v a2 s .co m*/ topicConnection = topicConnectionFactory.createTopicConnection(); topicConnection.start(); } catch (JMSException e) { log.error("Can not create topic connection." + e.getMessage(), e); return; } Session session = null; try { session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createTopic(topicName); MessageConsumer consumer = session.createConsumer(destination); log.info("Listening for messages"); while (active) { Message message = consumer.receive(1000); if (message != null) { messageCount++; if (message instanceof MapMessage) { MapMessage mapMessage = (MapMessage) message; Map<String, Object> map = new HashMap<String, Object>(); Enumeration enumeration = mapMessage.getMapNames(); while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); map.put(key, mapMessage.getObject(key)); } preservedEventList.add(map); log.info("Received Map Message : \n" + map + "\n"); } else if (message instanceof TextMessage) { String textMessage = ((TextMessage) message).getText(); preservedEventList.add(textMessage); log.info("Received Text Message : \n" + textMessage + "\n"); } else { preservedEventList.add(message.toString()); log.info("Received message : \n" + message.toString() + "\n"); } } } log.info("Finished listening for messages."); session.close(); topicConnection.stop(); topicConnection.close(); } catch (JMSException e) { log.error("Can not subscribe." + e.getMessage(), e); } }
From source file:org.wso2.carbon.integration.test.client.JMSPublisherClient.java
/** * This method will publish the data in the test data file to the given topic via ActiveMQ message broker * * @param topicName the topic which the messages should be published under * @param format format of the test data file (csv or text) * @param testCaseFolderName Testcase folder name which is in the test artifacts folder * @param dataFileName data file name with the extension to be read * *//*from w ww. j a va 2s.c o m*/ public static void publish(String topicName, String format, String testCaseFolderName, String dataFileName) { if (format == null || "map".equals(format)) { format = "csv"; } try { Properties properties = new Properties(); String filePath = getTestDataFileLocation(testCaseFolderName, dataFileName); properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("activemq.properties")); Context context = new InitialContext(properties); TopicConnectionFactory connFactory = (TopicConnectionFactory) context.lookup("ConnectionFactory"); TopicConnection topicConnection = connFactory.createTopicConnection(); topicConnection.start(); Session session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic(topicName); MessageProducer producer = session.createProducer(topic); List<String> messagesList = readFile(filePath); try { if (format.equalsIgnoreCase("csv")) { log.info("Sending Map messages on '" + topicName + "' topic"); publishMapMessages(producer, session, messagesList); } else { log.info("Sending " + format + " messages on '" + topicName + "' topic"); publishTextMessage(producer, session, messagesList); } } catch (JMSException e) { log.error("Can not subscribe." + e.getMessage(), e); } finally { producer.close(); session.close(); topicConnection.stop(); topicConnection.close(); } } catch (Exception e) { log.error("Error when publishing messages" + e.getMessage(), e); } log.info("All Order Messages sent"); }
From source file:org.wso2.carbon.sample.consumer.TopicConsumer.java
public void run() { // create topic connection TopicConnection topicConnection = null; try {//from w w w . j a v a 2 s .co m topicConnection = topicConnectionFactory.createTopicConnection(); topicConnection.start(); } catch (JMSException e) { log.error("Can not create topic connection." + e.getMessage(), e); return; } Session session = null; try { session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createTopic(topicName); MessageConsumer consumer = session.createConsumer(destination); log.info("Listening for messages"); while (active) { Message message = consumer.receive(1000); if (message != null) { if (message instanceof MapMessage) { MapMessage mapMessage = (MapMessage) message; Map<String, Object> map = new HashMap<String, Object>(); Enumeration enumeration = mapMessage.getMapNames(); while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); map.put(key, mapMessage.getObject(key)); } log.info("Received Map Message : " + map); } else if (message instanceof TextMessage) { log.info("Received Text Message : " + ((TextMessage) message).getText()); } else { log.info("Received message : " + message.toString()); } } } log.info("Finished listening for messages."); session.close(); topicConnection.stop(); topicConnection.close(); } catch (JMSException e) { log.error("Can not subscribe." + e.getMessage(), e); } }
From source file:org.wso2.extension.siddhi.io.jms.sink.util.TopicConsumer.java
public void run() { // create topic connection TopicConnection topicConnection = null; try {//from www . ja va 2 s . co m topicConnection = topicConnectionFactory.createTopicConnection(); topicConnection.start(); } catch (JMSException e) { log.error("Can not create topic connection." + e.getMessage(), e); return; } Session session = null; try { session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createTopic(topicName); MessageConsumer consumer = session.createConsumer(destination); log.info("Listening for messages"); while (active) { Message message = consumer.receive(1000); if (message != null) { resultContainer.eventReceived(message); if (message instanceof MapMessage) { MapMessage mapMessage = (MapMessage) message; Map<String, Object> map = new HashMap<String, Object>(); Enumeration enumeration = mapMessage.getMapNames(); while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); map.put(key, mapMessage.getObject(key)); } log.info("Received Map Message : " + map); } else if (message instanceof TextMessage) { log.info("Received Text Message : " + ((TextMessage) message).getText()); } else { log.info("Received message : " + message.toString()); } } } log.info("Finished listening for messages."); session.close(); topicConnection.stop(); topicConnection.close(); } catch (JMSException e) { log.error("Can not subscribe." + e.getMessage(), e); } }