// 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 createJMSObject() throws Config4JMSException { Queue queue; Session session; session = creator.getSession(); try { // -------- // One of the following: // createBrowser(queue) // createBrowser(queue, messageSelector) // -------- queue = config4jms.getQueue(createQueue); if (createMessageSelector == null) { queueBrowser = (QueueBrowser) session.createBrowser(queue); } else { queueBrowser = (QueueBrowser) session.createBrowser(queue, createMessageSelector); } } catch (JMSException ex) { throw new Config4JMSException( ex, cfg.fileName() + ": error in " + "creating object for " + scope + "; Session.createConsumer() failed: " + ex.toString()); } }
/** * Returns top commands in queue. Does not remove anything from queue. This method can be used for * an admin tool to peek inside the queue. * * @param count number of commands to lookup. * @return top commands in queue. */ public List<Command> getTopCommands(int count, String queueName) { List<Command> res = new ArrayList<>(); Session session = null; try { session = consumerConnection.createSession(); Queue queue = (Queue) jmsServer.lookup("/queue/" + queueName); Enumeration messages = session.createBrowser(queue).getEnumeration(); for (int i = 0; i < count && messages.hasMoreElements(); i++) { TextMessage msg = (TextMessage) messages.nextElement(); res.add(message2Command(msg)); } return res; } catch (Exception e) { throw new HornetNestException("Could not lookup commands", e); } finally { try { if (session != null) session.close(); } catch (Throwable e) { } } }
@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()); }
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); } } }
/** Test that close() hierarchically closes all child objects */ public void testCloseHierarchy() throws Exception { Connection conn = cf.createConnection(); Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = sess.createConsumer(topic1); MessageProducer producer = sess.createProducer(topic1); sess.createBrowser(queue1); Message m = sess.createMessage(); conn.close(); // Session /* If the session is closed then any method invocation apart from close() * will throw an IllegalStateException */ try { sess.createMessage(); fail("Session is not closed"); } catch (javax.jms.IllegalStateException e) { } try { sess.getAcknowledgeMode(); fail("should throw IllegalStateException"); } catch (javax.jms.IllegalStateException e) { // OK } try { sess.getTransacted(); fail("should throw IllegalStateException"); } catch (javax.jms.IllegalStateException e) { // OK } try { sess.getMessageListener(); fail("should throw IllegalStateException"); } catch (javax.jms.IllegalStateException e) { // OK } try { sess.createProducer(queue1); fail("should throw IllegalStateException"); } catch (javax.jms.IllegalStateException e) { // OK } try { sess.createConsumer(queue1); fail("should throw IllegalStateException"); } catch (javax.jms.IllegalStateException e) { // OK } // Producer /* If the producer is closed then any method invocation apart from close() * will throw an IllegalStateException */ try { producer.send(m); fail("Producer is not closed"); } catch (javax.jms.IllegalStateException e) { } try { producer.getDisableMessageID(); fail("should throw IllegalStateException"); } catch (javax.jms.IllegalStateException e) { // OK } try { producer.getPriority(); fail("should throw IllegalStateException"); } catch (javax.jms.IllegalStateException e) { // OK } try { producer.getDestination(); fail("should throw IllegalStateException"); } catch (javax.jms.IllegalStateException e) { // OK } try { producer.getTimeToLive(); fail("should throw IllegalStateException"); } catch (javax.jms.IllegalStateException e) { // OK } // ClientConsumer try { consumer.getMessageSelector(); fail("should throw exception"); } catch (javax.jms.IllegalStateException e) { // OK } try { consumer.getMessageListener(); fail("should throw exception"); } catch (javax.jms.IllegalStateException e) { // OK } try { consumer.receive(); fail("should throw exception"); } catch (javax.jms.IllegalStateException e) { // OK } // Browser }