List of usage examples for javax.jms Session close
void close() throws JMSException;
From source file:org.wso2.carbon.sample.consumer.JMSQueueMessageConsumer.java
public void run() { // create queue connection QueueConnection queueConnection = null; try {// w w w. j a v a 2 s. com queueConnection = queueConnectionFactory.createQueueConnection(); queueConnection.start(); } catch (JMSException e) { log.info("Can not create queue connection." + e); return; } Session session = null; try { session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(queueName); MessageConsumer consumer = session.createConsumer(destination); int count = 0; long totalLatency = 0; long lastTimestamp = System.currentTimeMillis(); while (active) { Message message = consumer.receive(1000); if (message != null) { // if (message instanceof MapMessage) { MapMessage mapMessage = (MapMessage) message; long currentTime = System.currentTimeMillis(); long sentTimestamp = (Long) mapMessage.getObject("time"); totalLatency = totalLatency + (currentTime - sentTimestamp); int logCount = 1000; if ((count % logCount == 0) && (count > warmUpCount)) { double rate = (logCount * 1000.0d / (System.currentTimeMillis() - lastTimestamp)); log.info("Consumer: " + consumerId + " (" + logCount + " received) rate: " + rate + " Latency:" + (totalLatency / (logCount * 1.0d))); // log.info("total latency:" + totalLatency); log.info("Total rate: " + (int) (consumers * rate)); totalLatency = 0; lastTimestamp = System.currentTimeMillis(); } count++; } } log.info("Finished listening for messages."); } catch (JMSException e) { log.info("Can not subscribe." + e); } finally { if (session != null) { try { session.close(); } catch (JMSException e) { log.error(e); } } try { queueConnection.stop(); queueConnection.close(); } catch (JMSException e) { e.printStackTrace(); } } }
From source file:org.wso2.carbon.sample.consumer.QueueConsumer.java
public void run() { // create queue connection QueueConnection queueConnection = null; try {/*from w w w . ja v a 2 s.c o m*/ queueConnection = queueConnectionFactory.createQueueConnection(); queueConnection.start(); } catch (JMSException e) { log.error("Can not create queue connection." + e.getMessage(), e); return; } Session session = null; try { session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(queueName); 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(); queueConnection.stop(); queueConnection.close(); } catch (JMSException e) { log.error("Can not subscribe." + e.getMessage(), e); } }
From source file:org.wso2.carbon.sample.consumer.TopicConsumer.java
public void run() { // create topic connection TopicConnection topicConnection = null; try {// w w w . ja v a 2 s.com 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.carbon.sample.jmsclient.JMSClient.java
public static void publishMessages(String topicName, String broker) { try {//from ww w . j a v a2 s . c o m // filePath = JMSClientUtil.getEventFilePath(sampleNumber, format, topicName, filePath); TopicConnection topicConnection = null; Session session = null; Properties properties = new Properties(); if (broker.equalsIgnoreCase("activemq")) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("activemq.properties")); Context context = new InitialContext(properties); TopicConnectionFactory connFactory = (TopicConnectionFactory) context.lookup("ConnectionFactory"); topicConnection = connFactory.createTopicConnection(); topicConnection.start(); session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } else if (broker.equalsIgnoreCase("mb")) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("mb.properties")); Context context = new InitialContext(properties); TopicConnectionFactory connFactory = (TopicConnectionFactory) context .lookup("qpidConnectionFactory"); topicConnection = connFactory.createTopicConnection(); topicConnection.start(); session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } else if (broker.equalsIgnoreCase("qpid")) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("qpid.properties")); Context context = new InitialContext(properties); TopicConnectionFactory connFactory = (TopicConnectionFactory) context .lookup("qpidConnectionFactory"); topicConnection = connFactory.createTopicConnection(); topicConnection.start(); session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } else { log.info("Please enter a valid JMS message broker. (ex: activemq, mb, qpid"); } if (session != null) { Topic topic = session.createTopic(topicName); MessageProducer producer = session.createProducer(topic); try { List<Map<String, Object>> messageList = new ArrayList<Map<String, Object>>(); Random random = new Random(); for (int j = 0; j < sessionsPerThread; j++) { for (int i = 0; i < msgsPerSession; i++) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("id", random.nextInt() + ""); map.put("value", random.nextInt()); map.put("content", "sample content"); map.put("client", "jmsQueueClient"); // setting the timestamp later messageList.add(map); } publishMapMessage(producer, session, messageList); } } catch (JMSException e) { log.error("Can not subscribe." + e.getMessage(), e); } catch (IOException e) { log.error("Error when reading the data file." + e.getMessage(), e); } finally { producer.close(); session.close(); topicConnection.stop(); } } } catch (Exception e) { log.error("Error when publishing message" + e.getMessage(), e); } }
From source file:org.wso2.carbon.transport.jms.factory.PooledJMSConnectionFactory.java
/** * Validates a connection object and verifies it can be used further. * * @param key The connection key * @param pooledConnectionObject The wrapped connection object * @return True if the object is valid/*from ww w .jav a2 s .co m*/ */ @Override public boolean validateObject(PooledConnectionKey key, PooledObject<Connection> pooledConnectionObject) { boolean valid = false; try { Session session = pooledConnectionObject.getObject().createSession(false, Session.AUTO_ACKNOWLEDGE); session.close(); valid = true; } catch (JMSException e) { log.warn("Connection with key " + key.hashCode() + " is not valid anymore"); } return valid; }
From source file:org.wso2.extension.siddhi.io.jms.sink.util.QueueConsumer.java
public void run() { // create queue connection QueueConnection queueConnection = null; try {// w w w.java 2 s . com queueConnection = queueConnectionFactory.createQueueConnection(); queueConnection.start(); } catch (JMSException e) { log.error("Can not create queue connection." + e.getMessage(), e); return; } Session session; try { session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(queueName); MessageConsumer consumer = session.createConsumer(destination); log.info("Listening for messages at " + queueName); 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(); queueConnection.stop(); queueConnection.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 w ww . j a v a 2 s . c o 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); } }
From source file:org.wso2.extension.siddhi.io.jms.source.client.JMSClient.java
public void sendJMSEvents(String filePath, String topicName, String queueName, String format, String broker, String providerURL) {//from w w w.ja v a2 s.c o m if (format == null || "map".equals(format)) { format = "csv"; } if ("".equalsIgnoreCase(broker)) { broker = "activemq"; } Session session = null; Properties properties = new Properties(); if (!"activemq".equalsIgnoreCase(broker) && !"mb".equalsIgnoreCase(broker) && !"qpid".equalsIgnoreCase(broker)) { log.error("Please enter a valid JMS message broker. (ex: activemq, mb, qpid"); return; } try { if (topicName != null && !"".equalsIgnoreCase(topicName)) { TopicConnection topicConnection; TopicConnectionFactory connFactory = null; if ("activemq".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("activemq.properties")); // to provide custom provider urls if (providerURL != null) { properties.put(Context.PROVIDER_URL, providerURL); } Context context = new InitialContext(properties); connFactory = (TopicConnectionFactory) context.lookup("ConnectionFactory"); } else if ("mb".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("mb.properties")); Context context = new InitialContext(properties); connFactory = (TopicConnectionFactory) context.lookup("qpidConnectionFactory"); } else if ("qpid".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("qpid.properties")); Context context = new InitialContext(properties); connFactory = (TopicConnectionFactory) context.lookup("qpidConnectionFactory"); } if (connFactory != null) { topicConnection = connFactory.createTopicConnection(); topicConnection.start(); session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); if (session != null) { Topic topic = session.createTopic(topicName); MessageProducer producer = session.createProducer(topic); List<String> messagesList = JMSClientUtil.readFile(filePath); try { if ("csv".equalsIgnoreCase(format)) { log.info("Sending Map messages on '" + topicName + "' topic"); JMSClientUtil.publishMapMessage(producer, session, messagesList); } else { log.info("Sending " + format + " messages on '" + topicName + "' topic"); JMSClientUtil.publishTextMessage(producer, session, messagesList); } log.info("All Order Messages sent"); } catch (JMSException e) { log.error("Cannot subscribe." + e.getMessage(), e); } catch (IOException e) { log.error("Error when reading the data file." + e.getMessage(), e); } finally { producer.close(); session.close(); topicConnection.stop(); } } } else { log.error("Error when creating connection factory. Please check necessary jar files"); } } else if (queueName != null && !queueName.equalsIgnoreCase("")) { QueueConnection queueConnection; QueueConnectionFactory connFactory = null; if ("activemq".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("activemq.properties")); Context context = new InitialContext(properties); connFactory = (QueueConnectionFactory) context.lookup("ConnectionFactory"); } else if ("mb".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("mb.properties")); Context context = new InitialContext(properties); connFactory = (QueueConnectionFactory) context.lookup("qpidConnectionFactory"); } else if ("qpid".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("qpid.properties")); Context context = new InitialContext(properties); connFactory = (QueueConnectionFactory) context.lookup("qpidConnectionFactory"); } if (connFactory != null) { queueConnection = connFactory.createQueueConnection(); queueConnection.start(); session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); if (session != null) { Queue queue = session.createQueue(queueName); MessageProducer producer = session.createProducer(queue); List<String> messagesList = JMSClientUtil.readFile(filePath); try { if ("csv".equalsIgnoreCase(format)) { log.info("Sending Map messages on '" + queueName + "' queue"); JMSClientUtil.publishMapMessage(producer, session, messagesList); } else { log.info("Sending " + format + " messages on '" + queueName + "' queue"); JMSClientUtil.publishTextMessage(producer, session, messagesList); } } catch (JMSException e) { log.error("Cannot subscribe." + e.getMessage(), e); } catch (IOException e) { log.error("Error when reading the data file." + e.getMessage(), e); } finally { producer.close(); session.close(); queueConnection.stop(); } } } else { log.error("Error when creating connection factory. Please check necessary jar files"); } } else { log.error("Enter queue name or topic name to be published!"); } } catch (Exception e) { log.error("Error when publishing" + e.getMessage(), e); } }
From source file:org.wso2.extension.siddhi.io.jms.source.client.JMSClient.java
public void sendJMSEvents(List<String> messageList, String topicName, String queueName, String format, String broker, String providerURL) { if (format == null || "map".equals(format)) { format = "csv"; }/* w w w.java 2 s . co m*/ if ("".equalsIgnoreCase(broker)) { broker = "activemq"; } Session session = null; Properties properties = new Properties(); if (!"activemq".equalsIgnoreCase(broker) && !"mb".equalsIgnoreCase(broker) && !"qpid".equalsIgnoreCase(broker)) { log.error("Please enter a valid JMS message broker. (ex: activemq, mb, qpid"); return; } try { if (topicName != null && !"".equalsIgnoreCase(topicName)) { TopicConnection topicConnection; TopicConnectionFactory connFactory = null; if ("activemq".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("activemq.properties")); // to provide custom provider urls if (providerURL != null) { properties.put(Context.PROVIDER_URL, providerURL); } Context context = new InitialContext(properties); connFactory = (TopicConnectionFactory) context.lookup("ConnectionFactory"); } else if ("mb".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("mb.properties")); Context context = new InitialContext(properties); connFactory = (TopicConnectionFactory) context.lookup("qpidConnectionFactory"); } else if ("qpid".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("qpid.properties")); Context context = new InitialContext(properties); connFactory = (TopicConnectionFactory) context.lookup("qpidConnectionFactory"); } if (connFactory != null) { topicConnection = connFactory.createTopicConnection(); topicConnection.start(); session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); if (session != null) { Topic topic = session.createTopic(topicName); MessageProducer producer = session.createProducer(topic); //List<String> messagesList = JMSClientUtil.readFile(filePath); try { if ("csv".equalsIgnoreCase(format)) { log.info("Sending Map messages on '" + topicName + "' topic"); JMSClientUtil.publishMapMessage(producer, session, messageList); } else { log.info("Sending " + format + " messages on '" + topicName + "' topic"); JMSClientUtil.publishTextMessage(producer, session, messageList); } log.info("All Order Messages sent"); } catch (JMSException e) { log.error("Cannot subscribe." + e.getMessage(), e); } catch (IOException e) { log.error("Error when reading the data file." + e.getMessage(), e); } finally { producer.close(); session.close(); topicConnection.stop(); } } } else { log.error("Error when creating connection factory. Please check necessary jar files"); } } else if (queueName != null && !queueName.equalsIgnoreCase("")) { QueueConnection queueConnection; QueueConnectionFactory connFactory = null; if ("activemq".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("activemq.properties")); // to provide custom provider urls if (providerURL != null) { properties.put(Context.PROVIDER_URL, providerURL); } Context context = new InitialContext(properties); connFactory = (QueueConnectionFactory) context.lookup("ConnectionFactory"); } else if ("mb".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("mb.properties")); Context context = new InitialContext(properties); connFactory = (QueueConnectionFactory) context.lookup("qpidConnectionFactory"); } else if ("qpid".equalsIgnoreCase(broker)) { properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("qpid.properties")); Context context = new InitialContext(properties); connFactory = (QueueConnectionFactory) context.lookup("qpidConnectionFactory"); } if (connFactory != null) { queueConnection = connFactory.createQueueConnection(); queueConnection.start(); session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); if (session != null) { Queue queue = session.createQueue(queueName); MessageProducer producer = session.createProducer(queue); //List<String> messagesList = JMSClientUtil.readFile(filePath); try { if ("csv".equalsIgnoreCase(format)) { log.info("Sending Map messages on '" + queueName + "' queue"); JMSClientUtil.publishMapMessage(producer, session, messageList); } else { log.info("Sending " + format + " messages on '" + queueName + "' queue"); JMSClientUtil.publishTextMessage(producer, session, messageList); } } catch (JMSException e) { log.error("Cannot subscribe." + e.getMessage(), e); } catch (IOException e) { log.error("Error when reading the data file." + e.getMessage(), e); } finally { producer.close(); session.close(); queueConnection.stop(); } } } else { log.error("Error when creating connection factory. Please check necessary jar files"); } } else { log.error("Enter queue name or topic name to be published!"); } } catch (Exception e) { log.error("Error when publishing" + e.getMessage(), e); } }
From source file:org.wso2.siddhi.extension.output.transport.jms.util.QueueConsumer.java
public void run() { // create queue connection QueueConnection queueConnection = null; try {//www .j a va 2 s . c o m queueConnection = queueConnectionFactory.createQueueConnection(); queueConnection.start(); } catch (JMSException e) { log.error("Can not create queue connection." + e.getMessage(), e); return; } Session session; try { session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(queueName); MessageConsumer consumer = session.createConsumer(destination); log.info("Listening for messages at " + queueName); 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(); queueConnection.stop(); queueConnection.close(); } catch (JMSException e) { log.error("Can not subscribe." + e.getMessage(), e); } }