List of usage examples for javax.jms QueueBrowser getEnumeration
Enumeration getEnumeration() throws JMSException;
From source file:example.browser.Browser.java
public static void main(String[] args) { String url = BROKER_URL; if (args.length > 0) { url = args[0].trim();//from w ww . java2 s. c o m } ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url); Connection connection = null; try { connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); Queue destination = session.createQueue("test-queue"); QueueBrowser browser = session.createBrowser(destination); Enumeration enumeration = browser.getEnumeration(); while (enumeration.hasMoreElements()) { TextMessage message = (TextMessage) enumeration.nextElement(); System.out.println("Browsing: " + message); TimeUnit.MILLISECONDS.sleep(DELAY); } session.close(); } catch (Exception e) { System.out.println("Caught exception!"); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { System.out.println("Could not close an open connection..."); } } } }
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);//from w ww . j a v a2 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 a va 2 s. c om * @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:org.dawnsci.commandserver.core.producer.ProcessConsumer.java
/** * Parse the queue for stale jobs and things that should be rerun. * @param bean//from w w w.j a v a 2 s. c o m * @throws Exception */ private void processStatusQueue(URI uri, String statusQName) throws Exception { QueueConnection qCon = null; try { QueueConnectionFactory connectionFactory = ConnectionFactoryFacade.createConnectionFactory(uri); qCon = connectionFactory.createQueueConnection(); QueueSession qSes = qCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = qSes.createQueue(statusQName); qCon.start(); QueueBrowser qb = qSes.createBrowser(queue); @SuppressWarnings("rawtypes") Enumeration e = qb.getEnumeration(); ObjectMapper mapper = new ObjectMapper(); Map<String, StatusBean> failIds = new LinkedHashMap<String, StatusBean>(7); List<String> removeIds = new ArrayList<String>(7); while (e.hasMoreElements()) { Message m = (Message) e.nextElement(); if (m == null) continue; if (m instanceof TextMessage) { TextMessage t = (TextMessage) m; try { @SuppressWarnings("unchecked") final StatusBean qbean = mapper.readValue(t.getText(), getBeanClass()); if (qbean == null) continue; if (qbean.getStatus() == null) continue; if (!qbean.getStatus().isStarted()) { failIds.put(t.getJMSMessageID(), qbean); continue; } // If it has failed, we clear it up if (qbean.getStatus() == Status.FAILED) { removeIds.add(t.getJMSMessageID()); continue; } if (qbean.getStatus() == Status.NONE) { removeIds.add(t.getJMSMessageID()); continue; } // If it is running and older than a certain time, we clear it up if (qbean.getStatus() == Status.RUNNING) { final long submitted = qbean.getSubmissionTime(); final long current = System.currentTimeMillis(); if (current - submitted > getMaximumRunningAge()) { removeIds.add(t.getJMSMessageID()); continue; } } if (qbean.getStatus().isFinal()) { final long submitted = qbean.getSubmissionTime(); final long current = System.currentTimeMillis(); if (current - submitted > getMaximumCompleteAge()) { removeIds.add(t.getJMSMessageID()); } } } catch (Exception ne) { System.out.println("Message " + t.getText() + " is not legal and will be removed."); removeIds.add(t.getJMSMessageID()); } } } // We fail the non-started jobs now - otherwise we could // actually start them late. TODO check this final List<String> ids = new ArrayList<String>(); ids.addAll(failIds.keySet()); ids.addAll(removeIds); if (ids.size() > 0) { for (String jMSMessageID : ids) { MessageConsumer consumer = qSes.createConsumer(queue, "JMSMessageID = '" + jMSMessageID + "'"); Message m = consumer.receive(1000); if (removeIds.contains(jMSMessageID)) continue; // We are done if (m != null && m instanceof TextMessage) { MessageProducer producer = qSes.createProducer(queue); final StatusBean bean = failIds.get(jMSMessageID); bean.setStatus(Status.FAILED); producer.send(qSes.createTextMessage(mapper.writeValueAsString(bean))); System.out.println("Failed job " + bean.getName() + " messageid(" + jMSMessageID + ")"); } } } } finally { if (qCon != null) qCon.close(); } }
From source file:org.dawnsci.commandserver.core.consumer.RemoteSubmission.java
/** * Monitors a given bean in the status queue. * If the bean is not there throws exception. * If the bean is in a final state, returns the bean straight away. * //from w w w . j a va 2 s.c om * Polls the queue for the unique id of the bean we want until it * encounters a final state of that bean. * * Polling rate is less than 1s * * NOTE this class can poll forever if the job it is looking at never finishes. * * @param obean * @param string * @return the bean once it is in a final state. * @throws exception if broker or queue absent */ public StatusBean monitor(StatusBean obean) throws Exception { if (getQueueName() == null || "".equals(getQueueName())) throw new Exception("Please specify a queue name!"); QueueConnectionFactory connectionFactory = ConnectionFactoryFacade.createConnectionFactory(uri); QueueConnection qCon = connectionFactory.createQueueConnection(); // This times out when the server is not there. QueueSession qSes = qCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = qSes.createQueue(queueName); qCon.start(); QueueBrowser qb = qSes.createBrowser(queue); Class clazz = obean.getClass(); ObjectMapper mapper = new ObjectMapper(); try { POLL: while (true) { Thread.sleep(500); @SuppressWarnings("rawtypes") Enumeration e = qb.getEnumeration(); while (e.hasMoreElements()) { // We must final the bean somewhere. Message m = (Message) e.nextElement(); if (m == null) continue; if (m instanceof TextMessage) { TextMessage t = (TextMessage) m; final StatusBean bean = mapper.readValue(t.getText(), clazz); if (bean.getUniqueId().equals(obean.getUniqueId())) { if (bean.getStatus().isFinal()) return bean; System.out.println(bean.getPercentComplete()); continue POLL; } } } throw new Exception( "The bean with id " + obean.getUniqueId() + " does not exist in " + getQueueName() + "!"); } } finally { qCon.close(); } }
From source file:org.dawnsci.commandserver.ui.view.StatusQueueView.java
protected Collection<StatusBean> getStatusBeans(final URI uri, final String queueName, final IProgressMonitor monitor) throws Exception { QueueConnectionFactory connectionFactory = ConnectionFactoryFacade.createConnectionFactory(uri); monitor.worked(1);//from w w w .j a va 2 s . c o m QueueConnection qCon = connectionFactory.createQueueConnection(); // This times out when the server is not there. QueueSession qSes = qCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = qSes.createQueue(queueName); qCon.start(); QueueBrowser qb = qSes.createBrowser(queue); monitor.worked(1); @SuppressWarnings("rawtypes") Enumeration e = qb.getEnumeration(); Class clazz = getBeanClass(); ObjectMapper mapper = new ObjectMapper(); final Collection<StatusBean> list = new TreeSet<StatusBean>(new Comparator<StatusBean>() { @Override public int compare(StatusBean o1, StatusBean o2) { // Newest first! long t1 = o2.getSubmissionTime(); long t2 = o1.getSubmissionTime(); return (t1 < t2 ? -1 : (t1 == t2 ? 0 : 1)); } }); while (e.hasMoreElements()) { Message m = (Message) e.nextElement(); if (m == null) continue; if (m instanceof TextMessage) { TextMessage t = (TextMessage) m; final StatusBean bean = mapper.readValue(t.getText(), clazz); list.add(bean); } } return list; }
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"); }/*from w w w. ja va 2s . c o m*/ 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 {/* ww w .j a v a2s .co 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 {//ww w . j ava 2 s . c om 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 a 2s . 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); } }