List of usage examples for javax.jms QueueBrowser close
void close() throws JMSException;
From source file:org.dawnsci.commandserver.mx.example.ActiveMQProducer.java
public static void main(String[] args) throws Exception { QueueConnectionFactory connectionFactory = ConnectionFactoryFacade .createConnectionFactory("tcp://ws097.diamond.ac.uk:61616"); Connection send = connectionFactory.createConnection(); Session session = send.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue("testQ"); final MessageProducer producer = session.createProducer(queue); producer.setDeliveryMode(DeliveryMode.PERSISTENT); Message message = session.createTextMessage("Hello World"); producer.send(message);// ww w . ja v a 2 s . c o m message = session.createTextMessage("...and another message"); producer.send(message); message = session.createObjectMessage(new TestObjectBean("this could be", "anything")); producer.send(message); // Test JSON SweepBean col = new SweepBean("fred", "d0000000001", 0, 100); ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.writeValueAsString(col); message = session.createTextMessage(jsonString); producer.send(message); producer.close(); session.close(); send.close(); // Now we peak at the queue // If the consumer is not going, the messages should still be there if (REQUIRE_PEAK) { QueueConnection qCon = connectionFactory.createQueueConnection(); QueueSession qSes = qCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); queue = qSes.createQueue("testQ"); qCon.start(); QueueBrowser qb = qSes.createBrowser(queue); Enumeration e = qb.getEnumeration(); if (e.hasMoreElements()) System.out.println("Peak at queue:"); while (e.hasMoreElements()) { Message m = (Message) e.nextElement(); if (m == null) continue; if (m instanceof TextMessage) { TextMessage t = (TextMessage) m; System.out.println(t.getText()); } else if (m instanceof ObjectMessage) { ObjectMessage o = (ObjectMessage) m; System.out.println(o.getObject()); } } qb.close(); qSes.close(); qCon.close(); } }
From source file:org.dawnsci.commandserver.core.producer.Broadcaster.java
/** * // w w w. j ava 2 s .c o m * @param bean * @throws Exception */ private void updateQueue(StatusBean bean) throws Exception { QueueConnection qCon = null; try { QueueBrowser qb = qSes.createBrowser(queue); @SuppressWarnings("rawtypes") Enumeration e = qb.getEnumeration(); ObjectMapper mapper = new ObjectMapper(); String jMSMessageID = null; while (e.hasMoreElements()) { Message m = (Message) e.nextElement(); if (m == null) continue; if (m instanceof TextMessage) { TextMessage t = (TextMessage) m; @SuppressWarnings("unchecked") final StatusBean qbean = mapper.readValue(t.getText(), bean.getClass()); if (qbean == null) continue; if (qbean.getUniqueId() == null) continue; // Definitely not our bean if (qbean.getUniqueId().equals(bean.getUniqueId())) { jMSMessageID = t.getJMSMessageID(); break; } } } qb.close(); if (jMSMessageID != null) { MessageConsumer consumer = qSes.createConsumer(queue, "JMSMessageID = '" + jMSMessageID + "'"); Message m = consumer.receive(1000); if (m != null && m instanceof TextMessage) { MessageProducer producer = qSes.createProducer(queue); producer.send(qSes.createTextMessage(mapper.writeValueAsString(bean))); } } } finally { if (qCon != null) qCon.close(); } }
From source file:net.timewalker.ffmq4.common.session.AbstractSession.java
/** * Close remaining browsers/* w w w .ja va 2 s . co m*/ */ private void closeRemainingBrowsers() { List<AbstractQueueBrowser> browsersToClose = new ArrayList<>(browsersMap.size()); synchronized (browsersMap) { browsersToClose.addAll(browsersMap.values()); } for (int n = 0; n < browsersToClose.size(); n++) { QueueBrowser browser = browsersToClose.get(n); log.debug("Auto-closing unclosed browser : " + browser); try { browser.close(); } catch (Exception e) { log.error("Could not close browser " + browser, e); } } }
From source file:nl.nn.adapterframework.extensions.tibco.GetTibcoQueues.java
private String getQueueMessage(Session jSession, TibjmsAdmin admin, String queueName, int queueItem, LdapSender ldapSender) throws TibjmsAdminException, JMSException { QueueInfo qInfo = admin.getQueue(queueName); if (qInfo == null) { throw new JMSException(" queue [" + queueName + "] does not exist"); }// ww w .j a v a 2 s . c om XmlBuilder qMessageXml = new XmlBuilder("qMessage"); ServerInfo serverInfo = admin.getInfo(); String url = serverInfo.getURL(); qMessageXml.addAttribute("url", url); String resolvedUrl = getResolvedUrl(url); if (resolvedUrl != null) { qMessageXml.addAttribute("resolvedUrl", resolvedUrl); } qMessageXml.addAttribute("timestamp", DateUtils.getIsoTimeStamp()); qMessageXml.addAttribute("startTime", DateUtils.format(serverInfo.getStartTime(), DateUtils.fullIsoFormat)); XmlBuilder qNameXml = new XmlBuilder("qName"); qNameXml.setCdataValue(queueName); Queue queue = jSession.createQueue(queueName); QueueBrowser queueBrowser = null; try { queueBrowser = jSession.createBrowser(queue); Enumeration enm = queueBrowser.getEnumeration(); int count = 0; boolean found = false; String chompCharSizeString = AppConstants.getInstance().getString("browseQueue.chompCharSize", null); int chompCharSize = (int) Misc.toFileSize(chompCharSizeString, -1); while (enm.hasMoreElements() && !found) { count++; if (count == queueItem) { qNameXml.addAttribute("item", count); Object o = enm.nextElement(); if (o instanceof Message) { Message msg = (Message) o; XmlBuilder qMessageId = new XmlBuilder("qMessageId"); qMessageId.setCdataValue(msg.getJMSMessageID()); qMessageXml.addSubElement(qMessageId); XmlBuilder qTimestamp = new XmlBuilder("qTimestamp"); qTimestamp.setCdataValue(DateUtils.format(msg.getJMSTimestamp(), DateUtils.fullIsoFormat)); qMessageXml.addSubElement(qTimestamp); StringBuffer sb = new StringBuffer(""); Enumeration propertyNames = msg.getPropertyNames(); while (propertyNames.hasMoreElements()) { String propertyName = (String) propertyNames.nextElement(); Object object = msg.getObjectProperty(propertyName); if (sb.length() > 0) { sb.append("; "); } sb.append(propertyName); sb.append("="); sb.append(object); } XmlBuilder qPropsXml = new XmlBuilder("qProps"); qPropsXml.setCdataValue(sb.toString()); qMessageXml.addSubElement(qPropsXml); XmlBuilder qTextXml = new XmlBuilder("qText"); String msgText; try { TextMessage textMessage = (TextMessage) msg; msgText = textMessage.getText(); } catch (ClassCastException e) { msgText = msg.toString(); qTextXml.addAttribute("text", false); } int msgSize = msgText.length(); if (isHideMessage()) { qTextXml.setCdataValue("***HIDDEN***"); } else { if (chompCharSize >= 0 && msgSize > chompCharSize) { qTextXml.setCdataValue(msgText.substring(0, chompCharSize) + "..."); qTextXml.addAttribute("chomped", true); } else { qTextXml.setCdataValue(msgText); } } qMessageXml.addSubElement(qTextXml); XmlBuilder qTextSizeXml = new XmlBuilder("qTextSize"); qTextSizeXml.setValue(Misc.toFileSize(msgSize)); qMessageXml.addSubElement(qTextSizeXml); } found = true; } else { enm.nextElement(); } } } finally { if (queueBrowser != null) { try { queueBrowser.close(); } catch (JMSException e) { log.warn(getLogPrefix(null) + "exception on closing queue browser", e); } } } qMessageXml.addSubElement(qNameXml); Map aclMap = getAclMap(admin, ldapSender); XmlBuilder aclXml = new XmlBuilder("acl"); XmlBuilder qInfoXml = qInfoToXml(qInfo); aclXml.setValue((String) aclMap.get(qInfo.getName())); qInfoXml.addSubElement(aclXml); qMessageXml.addSubElement(qInfoXml); Map consumersMap = getConnectedConsumersMap(admin); XmlBuilder consumerXml = new XmlBuilder("connectedConsumers"); if (consumersMap.containsKey(qInfo.getName())) { LinkedList<String> consumers = (LinkedList<String>) consumersMap.get(qInfo.getName()); String consumersString = listToString(consumers); if (consumersString != null) { consumerXml.setCdataValue(consumersString); } } qInfoXml.addSubElement(consumerXml); return qMessageXml.toXML(); }
From source file:nl.nn.adapterframework.extensions.tibco.TibcoUtils.java
protected static long getQueueFirstMessageAge(Session jSession, String queueName, String messageSelector, long currentTime, boolean warn) throws JMSException { QueueBrowser queueBrowser = null; try {//from w w w. jav a2s . c o m Queue queue = jSession.createQueue(queueName); if (messageSelector == null) { queueBrowser = jSession.createBrowser(queue); } else { queueBrowser = jSession.createBrowser(queue, messageSelector); } Enumeration enm = queueBrowser.getEnumeration(); if (enm.hasMoreElements()) { Object o = enm.nextElement(); if (o instanceof Message) { Message msg = (Message) o; long jmsTimestamp = msg.getJMSTimestamp(); return currentTime - jmsTimestamp; } else { if (warn) { log.warn("message was not of type Message, but [" + o.getClass().getName() + "]"); } return -2; } } else { return -1; } } finally { if (queueBrowser != null) { try { queueBrowser.close(); } catch (JMSException e) { log.warn("Exception on closing queueBrowser", e); } } } }
From source file:nl.nn.adapterframework.extensions.tibco.TibcoUtils.java
protected static long getQueueMessageCount(Session jSession, String queueName, String messageSelector) throws JMSException { QueueBrowser queueBrowser = null; try {/*from www.j a va 2s . c o m*/ Queue queue = jSession.createQueue(queueName); if (messageSelector == null) { queueBrowser = jSession.createBrowser(queue); } else { queueBrowser = jSession.createBrowser(queue, messageSelector); } int count = 0; for (Enumeration enm = queueBrowser.getEnumeration(); enm.hasMoreElements(); enm.nextElement()) { count++; } return count; } finally { if (queueBrowser != null) { try { queueBrowser.close(); } catch (JMSException e) { log.warn("Exception on closing queueBrowser", e); } } } }
From source file:nl.nn.adapterframework.jms.JmsMessageBrowser.java
public int getMessageCount() throws ListenerException { QueueBrowser queueBrowser = null; Session session = null;/*from w w w .ja v a2 s . co m*/ try { session = createSession(); if (StringUtils.isEmpty(getSelector())) { queueBrowser = session.createBrowser((Queue) getDestination()); } else { queueBrowser = session.createBrowser((Queue) getDestination(), getSelector()); } int count = 0; for (Enumeration enm = queueBrowser.getEnumeration(); enm.hasMoreElements(); enm.nextElement()) { count++; } return count; } catch (Exception e) { throw new ListenerException("cannot determin messagecount", e); } finally { try { if (queueBrowser != null) { queueBrowser.close(); } } catch (JMSException e) { throw new ListenerException("error closing queuebrowser", e); } closeSession(session); } }
From source file:nl.nn.adapterframework.jms.JmsMessageBrowser.java
protected Message doBrowse(Map selectors) throws ListenerException { QueueSession session = null;/* w ww .jav a 2 s. co m*/ Message msg = null; QueueBrowser queueBrowser = null; try { session = (QueueSession) createSession(); queueBrowser = session.createBrowser((Queue) getDestination(), getCombinedSelector(selectors)); Enumeration msgenum = queueBrowser.getEnumeration(); if (msgenum.hasMoreElements()) { msg = (Message) msgenum.nextElement(); } return msg; } catch (Exception e) { throw new ListenerException(e); } finally { try { if (queueBrowser != null) { queueBrowser.close(); } } catch (JMSException e1) { throw new ListenerException("exception closing queueBrowser", e1); } closeSession(session); } }
From source file:org.apache.activemq.apollo.JmsQueueBrowserTest.java
/** * Tests the queue browser. Browses the messages then the consumer tries to receive them. The messages should still * be in the queue even when it was browsed. * * Re-enable once https://issues.apache.org/jira/browse/APLO-226 is fixed. * * @throws Exception/*from w w w .j ava 2s.co m*/ */ public void testReceiveBrowseReceive() throws Exception { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); ActiveMQQueue destination = new ActiveMQQueue("TEST"); MessageProducer producer = session.createProducer(destination); MessageConsumer consumer = session.createConsumer(destination); connection.start(); Message[] outbound = new Message[] { session.createTextMessage("First Message"), session.createTextMessage("Second Message"), session.createTextMessage("Third Message") }; // lets consume any outstanding messages from previous test runs while (consumer.receive(1000) != null) { } producer.send(outbound[0]); producer.send(outbound[1]); producer.send(outbound[2]); // Get the first. assertEquals(outbound[0], consumer.receive(1000)); consumer.close(); //Thread.sleep(200); QueueBrowser browser = session.createBrowser((Queue) destination); Enumeration enumeration = browser.getEnumeration(); // browse the second assertTrue("should have received the second message", enumeration.hasMoreElements()); assertEquals(outbound[1], (Message) enumeration.nextElement()); // browse the third. assertTrue("Should have received the third message", enumeration.hasMoreElements()); assertEquals(outbound[2], (Message) enumeration.nextElement()); // There should be no more. boolean tooMany = false; while (enumeration.hasMoreElements()) { LOG.info("Got extra message: " + ((TextMessage) enumeration.nextElement()).getText()); tooMany = true; } assertFalse(tooMany); browser.close(); // Re-open the consumer. consumer = session.createConsumer(destination); // Receive the second. assertEquals(outbound[1], consumer.receive(1000)); // Receive the third. assertEquals(outbound[2], consumer.receive(1000)); consumer.close(); }
From source file:org.apache.activemq.apollo.JmsQueueBrowserTest.java
public void testBrowseReceive() throws Exception { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); ActiveMQQueue destination = new ActiveMQQueue("TEST"); connection.start();//from w w w. jav a 2 s .c om Message[] outbound = new Message[] { session.createTextMessage("First Message"), session.createTextMessage("Second Message"), session.createTextMessage("Third Message") }; MessageProducer producer = session.createProducer(destination); producer.send(outbound[0]); // create browser first QueueBrowser browser = session.createBrowser((Queue) destination); Enumeration enumeration = browser.getEnumeration(); // create consumer MessageConsumer consumer = session.createConsumer(destination); // browse the first message assertTrue("should have received the first message", enumeration.hasMoreElements()); assertEquals(outbound[0], (Message) enumeration.nextElement()); // Receive the first message. assertEquals(outbound[0], consumer.receive(1000)); consumer.close(); browser.close(); producer.close(); }