// Implementation methods // ------------------------------------------------------------------------- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { WebClient client = WebClient.getWebClient(request); Session session = client.getSession(); Queue queue = getQueue(request, session); if (queue == null) { throw new ServletException("No queue URI specified"); } String msgId = request.getParameter("msgId"); if (msgId == null) { MessageRenderer renderer = getMessageRenderer(request); configureRenderer(request, renderer); String selector = getSelector(request); QueueBrowser browser = session.createBrowser(queue, selector); renderer.renderMessages(request, response, browser); } else { XmlMessageRenderer renderer = new XmlMessageRenderer(); QueueBrowser browser = session.createBrowser(queue, "JMSMessageID='" + msgId + "'"); if (!browser.getEnumeration().hasMoreElements()) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } Message message = (Message) browser.getEnumeration().nextElement(); PrintWriter writer = response.getWriter(); renderer.renderMessage(writer, request, response, browser, message); writer.flush(); } } catch (JMSException e) { throw new ServletException(e); } }
public void receiveAll() { System.out.println("--> Receiving redundant messages ..."); try { QueueBrowser browser = context.createBrowser(myQueue); while (browser.getEnumeration().hasMoreElements()) { System.out.println("--> here is one"); context.createConsumer(myQueue).receiveBody(String.class, 1000); } } catch (JMSException ex) { Logger.getLogger(MessageReceiverSync.class.getName()).log(Level.SEVERE, null, ex); } }
public int getQueueSize() { int count = 0; try { QueueBrowser browser = context.createBrowser(pointsQueue); Enumeration elems = browser.getEnumeration(); while (elems.hasMoreElements()) { elems.nextElement(); count++; } } catch (JMSException ex) { Logger.getLogger(ReceivePointsBean.class.getName()).log(Level.SEVERE, null, ex); } Logger.getLogger(ReceivePointsBean.class.getName()) .log(Level.INFO, "Getting queue size: " + count); return count; }
public Message getMessage() throws JMSException { if (message == null) { if (id != null) { QueueBrowser tempBrowser = getBrowser(); Enumeration iter = tempBrowser.getEnumeration(); while (iter.hasMoreElements()) { Message item = (Message) iter.nextElement(); if (id.equals(item.getJMSMessageID())) { message = item; break; } } tempBrowser.close(); } } return message; }
/** * Close the given JMS QueueBrowser and ignore any thrown exception. This is useful for typical * <code>finally</code> blocks in manual JMS code. * * @param browser the JMS QueueBrowser to close (may be <code>null</code>) */ public static void closeQueueBrowser(QueueBrowser browser) { if (browser != null) { try { browser.close(); } catch (JMSException ex) { logger.trace("Could not close JMS QueueBrowser", ex); } catch (Throwable ex) { // We don't trust the JMS provider: It might throw RuntimeException or Error. logger.trace("Unexpected exception on closing JMS QueueBrowser", ex); } } }
@Test public void testQueueBrowser() throws Exception { // Send a message to the broker. connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); ActiveMQDestination destination = createDestination(session, destinationType); MessageProducer producer = session.createProducer(destination); producer.setDeliveryMode(this.deliveryMode); sendMessages(session, producer, 5); producer.close(); QueueBrowser browser = session.createBrowser((Queue) destination); Enumeration<?> enumeration = browser.getEnumeration(); for (int i = 0; i < 5; i++) { Thread.sleep(100); assertTrue(enumeration.hasMoreElements()); Message m = (Message) enumeration.nextElement(); assertNotNull(m); assertEquals("" + i, ((TextMessage) m).getText()); } assertFalse(enumeration.hasMoreElements()); }
// Queue specific public Enumeration browse() throws JMSException { QueueBrowser b = ((QueueSession) session).createBrowser((Queue) destination); return b.getEnumeration(); }
protected void updateQueue(U bean) throws EventException { boolean resumeAfter = !awaitPaused; Session session = null; try { pause(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue(getSubmitQueueName()); QueueBrowser qb = session.createBrowser(queue); @SuppressWarnings("rawtypes") Enumeration e = qb.getEnumeration(); while (e.hasMoreElements()) { Message msg = (Message) e.nextElement(); TextMessage t = (TextMessage) msg; String json = t.getText(); final StatusBean b = service.unmarshal(json, getBeanClass()); MessageConsumer consumer = session.createConsumer(queue, "JMSMessageID = '" + msg.getJMSMessageID() + "'"); Message rem = consumer.receive(Constants.getReceiveFrequency()); consumer.close(); if (rem == null && b.getUniqueId().equals(bean.getUniqueId())) { // Something went wrong, not sure why it does this, TODO investigate if (overrideMap == null) overrideMap = new Hashtable<>(7); overrideMap.put(b.getUniqueId(), bean); continue; } MessageProducer producer = session.createProducer(queue); if (b.getUniqueId().equals(bean.getUniqueId())) { b.setStatus(bean.getStatus()); t = session.createTextMessage(service.marshal(b)); t.setJMSMessageID(rem.getJMSMessageID()); t.setJMSExpiration(rem.getJMSExpiration()); t.setJMSTimestamp(rem.getJMSTimestamp()); t.setJMSPriority(rem.getJMSPriority()); t.setJMSCorrelationID(rem.getJMSCorrelationID()); } producer.send(t); producer.close(); } } catch (Exception ne) { throw new EventException("Cannot reorder queue!", ne); } finally { // Only resume if it wasn't in a paused state before this update if (resumeAfter) { resume(); } try { if (session != null) session.close(); } catch (JMSException e) { throw new EventException("Cannot close session!", e); } } }