List of usage examples for javax.jms MessageConsumer receive
Message receive(long timeout) throws JMSException;
From source file:org.codehaus.stomp.StompTest.java
public void testSendMessage() throws Exception { MessageConsumer consumer = session.createConsumer(queue); String frame = "CONNECT\n" + "login: brianm\n" + "passcode: wombats\n\n" + Stomp.NULL; sendFrame(frame);/*from w ww . j a va 2 s. co m*/ frame = receiveFrame(10000); Assert.assertTrue(frame.startsWith("CONNECTED")); frame = "SEND\n" + "destination:/queue/" + getQueueName() + "\n\n" + "Hello World" + Stomp.NULL; sendFrame(frame); TextMessage message = (TextMessage) consumer.receive(1000); Assert.assertNotNull(message); Assert.assertEquals("Hello World", message.getText()); // Make sure that the timestamp is valid - should // be very close to the current time. long tnow = System.currentTimeMillis(); long tmsg = message.getJMSTimestamp(); Assert.assertTrue(Math.abs(tnow - tmsg) < 1000); }
From source file:org.apache.axis2.transport.jms.JMSSender.java
/** * Create a Consumer for the reply destination and wait for the response JMS message * synchronously. If a message arrives within the specified time interval, process it * through Axis2//from w w w . j a va 2 s. co m * @param session the session to use to listen for the response * @param replyDestination the JMS reply Destination * @param msgCtx the outgoing message for which we are expecting the response * @param contentTypeProperty the message property used to determine the content type * of the response message * @throws AxisFault on error */ private void waitForResponseAndProcess(Session session, Destination replyDestination, MessageContext msgCtx, String correlationId, String contentTypeProperty) throws AxisFault { try { MessageConsumer consumer; consumer = JMSUtils.createConsumer(session, replyDestination, "JMSCorrelationID = '" + correlationId + "'"); // how long are we willing to wait for the sync response long timeout = JMSConstants.DEFAULT_JMS_TIMEOUT; String waitReply = (String) msgCtx.getProperty(JMSConstants.JMS_WAIT_REPLY); if (waitReply != null) { timeout = Long.valueOf(waitReply).longValue(); } if (log.isDebugEnabled()) { log.debug("Waiting for a maximum of " + timeout + "ms for a response message to destination : " + replyDestination + " with JMS correlation ID : " + correlationId); } Message reply = consumer.receive(timeout); if (reply != null) { // update transport level metrics metrics.incrementMessagesReceived(); try { metrics.incrementBytesReceived(JMSUtils.getMessageSize(reply)); } catch (JMSException e) { log.warn("Error reading JMS message size to update transport metrics", e); } try { processSyncResponse(msgCtx, reply, contentTypeProperty); metrics.incrementMessagesReceived(); } catch (AxisFault e) { metrics.incrementFaultsReceiving(); throw e; } } else { log.warn("Did not receive a JMS response within " + timeout + " ms to destination : " + replyDestination + " with JMS correlation ID : " + correlationId); metrics.incrementTimeoutsReceiving(); } } catch (JMSException e) { metrics.incrementFaultsReceiving(); handleException("Error creating a consumer, or receiving a synchronous reply " + "for outgoing MessageContext ID : " + msgCtx.getMessageID() + " and reply Destination : " + replyDestination, e); } }
From source file:com.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingWorker.java
@Override public String waitForMessage(Run<?, ?> build, String selector, String variable, Integer timeout) { String ip = null;/*from w ww. jav a2 s . com*/ try { ip = Inet4Address.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { log.severe("Unable to get localhost IP address."); } String ltopic = getTopic(); if (ip != null && provider.getAuthenticationMethod() != null && ltopic != null && provider.getBroker() != null) { log.info("Waiting for message with selector: " + selector); Connection connection = null; MessageConsumer consumer = null; try { ActiveMQConnectionFactory connectionFactory = provider.getConnectionFactory(); connection = connectionFactory.createConnection(); connection.setClientID(ip + "_" + UUID.randomUUID().toString()); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Topic destination = session.createTopic(ltopic); consumer = session.createConsumer(destination, selector); Message message = consumer.receive(timeout * 60 * 1000); if (message != null) { String value = getMessageBody(message); if (build != null) { if (StringUtils.isNotEmpty(variable)) { EnvVars vars = new EnvVars(); vars.put(variable, value); build.addAction(new CIEnvironmentContributingAction(vars)); } } log.info("Received message with selector: " + selector + "\n" + formatMessage(message)); return value; } log.info("Timed out waiting for message!"); } catch (Exception e) { log.log(Level.SEVERE, "Unhandled exception waiting for message.", e); } finally { if (consumer != null) { try { consumer.close(); } catch (Exception e) { } } if (connection != null) { try { connection.close(); } catch (Exception e) { } } } } else { log.severe("One or more of the following is invalid (null): ip, user, password, topic, broker."); } return null; }
From source file:org.apache.james.queue.activemq.ActiveMQMailQueue.java
/** * Try to use ActiveMQ StatisticsPlugin to get size and if that fails * fallback to {@link JMSMailQueue#getSize()} *//*from w w w .j a va2 s .c om*/ @Override public long getSize() throws MailQueueException { Connection connection = null; Session session = null; MessageConsumer consumer = null; MessageProducer producer = null; TemporaryQueue replyTo = null; long size = -1; try { connection = connectionFactory.createConnection(); connection.start(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); replyTo = session.createTemporaryQueue(); consumer = session.createConsumer(replyTo); Queue myQueue = session.createQueue(queuename); producer = session.createProducer(null); String queueName = "ActiveMQ.Statistics.Destination." + myQueue.getQueueName(); Queue query = session.createQueue(queueName); Message msg = session.createMessage(); msg.setJMSReplyTo(replyTo); producer.send(query, msg); MapMessage reply = (MapMessage) consumer.receive(2000); if (reply != null && reply.itemExists("size")) { try { size = reply.getLong("size"); return size; } catch (NumberFormatException e) { // if we hit this we can't calculate the size so just catch // it } } } catch (Exception e) { throw new MailQueueException("Unable to remove mails", e); } finally { if (consumer != null) { try { consumer.close(); } catch (JMSException e1) { e1.printStackTrace(); // ignore on rollback } } if (producer != null) { try { producer.close(); } catch (JMSException e1) { // ignore on rollback } } if (replyTo != null) { try { // we need to delete the temporary queue to be sure we will // free up memory if thats not done and a pool is used // its possible that we will register a new mbean in jmx for // every TemporaryQueue which will never get unregistered replyTo.delete(); } catch (JMSException e) { } } try { if (session != null) session.close(); } catch (JMSException e1) { // ignore here } try { if (connection != null) connection.close(); } catch (JMSException e1) { // ignore here } } // if we came to this point we should just fallback to super method return super.getSize(); }
From source file:org.codehaus.stomp.StompTest.java
public void testJMSXGroupIdCanBeSet() throws Exception { MessageConsumer consumer = session.createConsumer(queue); String frame = "CONNECT\n" + "login: brianm\n" + "passcode: wombats\n\n" + Stomp.NULL; sendFrame(frame);/*from www.j a va 2s .c o m*/ frame = receiveFrame(10000); Assert.assertTrue(frame.startsWith("CONNECTED")); frame = "SEND\n" + "destination:/queue/" + getQueueName() + "\n" + "JMSXGroupID: TEST\n\n" + "Hello World" + Stomp.NULL; sendFrame(frame); TextMessage message = (TextMessage) consumer.receive(1000); Assert.assertNotNull(message); Assert.assertEquals("TEST", ((ActiveMQTextMessage) message).getGroupID()); }
From source file:org.codehaus.stomp.StompTest.java
public void testSendMessageWithCustomHeadersAndSelector() throws Exception { MessageConsumer consumer = session.createConsumer(queue, "foo = 'abc'"); String frame = "CONNECT\n" + "login: brianm\n" + "passcode: wombats\n\n" + Stomp.NULL; sendFrame(frame);//from ww w. jav a2 s .c o m frame = receiveFrame(10000); Assert.assertTrue(frame.startsWith("CONNECTED")); frame = "SEND\n" + "foo:abc\n" + "bar:123\n" + "destination:/queue/" + getQueueName() + "\n\n" + "Hello World" + Stomp.NULL; sendFrame(frame); TextMessage message = (TextMessage) consumer.receive(1000); Assert.assertNotNull(message); Assert.assertEquals("Hello World", message.getText()); Assert.assertEquals("foo", "abc", message.getStringProperty("foo")); Assert.assertEquals("bar", "123", message.getStringProperty("bar")); }
From source file:org.codehaus.stomp.StompTest.java
public void testSendMessageWithStandardHeaders() throws Exception { MessageConsumer consumer = session.createConsumer(queue); String frame = "CONNECT\n" + "login: brianm\n" + "passcode: wombats\n\n" + Stomp.NULL; sendFrame(frame);/* w ww .ja v a 2s . com*/ frame = receiveFrame(10000); Assert.assertTrue(frame.startsWith("CONNECTED")); frame = "SEND\n" + "correlation-id:c123\n" + "priority:3\n" + "type:t345\n" + "JMSXGroupID:abc\n" + "foo:abc\n" + "bar:123\n" + "destination:/queue/" + getQueueName() + "\n\n" + "Hello World" + Stomp.NULL; sendFrame(frame); TextMessage message = (TextMessage) consumer.receive(1000); Assert.assertNotNull(message); Assert.assertEquals("Hello World", message.getText()); Assert.assertEquals("JMSCorrelationID", "c123", message.getJMSCorrelationID()); Assert.assertEquals("getJMSType", "t345", message.getJMSType()); Assert.assertEquals("getJMSPriority", 3, message.getJMSPriority()); Assert.assertEquals(DeliveryMode.PERSISTENT, message.getJMSDeliveryMode()); Assert.assertEquals("foo", "abc", message.getStringProperty("foo")); Assert.assertEquals("bar", "123", message.getStringProperty("bar")); Assert.assertEquals("JMSXGroupID", "abc", message.getStringProperty("JMSXGroupID")); ActiveMQTextMessage amqMessage = (ActiveMQTextMessage) message; Assert.assertEquals("GroupID", "abc", amqMessage.getGroupID()); }
From source file:org.codehaus.stomp.StompTest.java
public void testSendMessageWithReceipt() throws Exception { MessageConsumer consumer = session.createConsumer(queue); String frame = "CONNECT\n" + "login: brianm\n" + "passcode: wombats\n\n" + Stomp.NULL; sendFrame(frame);// w ww . j ava 2 s . c o m frame = receiveFrame(10000); Assert.assertTrue(frame.startsWith("CONNECTED")); frame = "SEND\n" + "destination:/queue/" + getQueueName() + "\n" + "receipt: 1234\n\n" + "Hello World" + Stomp.NULL; sendFrame(frame); String f = receiveFrame(10000); System.out.println(f); Assert.assertTrue(f.startsWith("RECEIPT")); Assert.assertTrue(f.indexOf("receipt-id:1234") >= 0); TextMessage message = (TextMessage) consumer.receive(1000); Assert.assertNotNull(message); Assert.assertEquals("Hello World", message.getText()); // Make sure that the timestamp is valid - should // be very close to the current time. long tnow = System.currentTimeMillis(); long tmsg = message.getJMSTimestamp(); Assert.assertTrue(Math.abs(tnow - tmsg) < 1000); }
From source file:org.apache.qpid.test.utils.QpidBrokerTestCase.java
/** * Consume all the messages in the specified queue. Helper to ensure * persistent tests don't leave data behind. * * @param queue the queue to purge//from w w w. j av a 2 s.c om * * @return the count of messages drained * * @throws Exception if a problem occurs */ protected int drainQueue(Queue queue) throws Exception { Connection connection = getConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(queue); connection.start(); int count = 0; while (consumer.receive(1000) != null) { count++; } connection.close(); return count; }
From source file:org.codehaus.stomp.StompTest.java
public void testSuccessiveTransactionsWithSameID() throws Exception { MessageConsumer consumer = session.createConsumer(queue); String frame = "CONNECT\n" + "login: brianm\n" + "passcode: wombats\n\n" + Stomp.NULL; sendFrame(frame);/* w ww . java2 s . c om*/ String f = receiveFrame(1000); Assert.assertTrue(f.startsWith("CONNECTED")); // first tx frame = "BEGIN\n" + "transaction: tx1\n" + "\n\n" + Stomp.NULL; sendFrame(frame); frame = "SEND\n" + "destination:/queue/" + getQueueName() + "\n" + "transaction: tx1\n\n" + "Hello World" + Stomp.NULL; sendFrame(frame); frame = "COMMIT\n" + "transaction: tx1\n" + "\n\n" + Stomp.NULL; sendFrame(frame); Message message = consumer.receive(1000); Assert.assertNotNull("Should have received a message", message); // 2nd tx with same tx ID frame = "BEGIN\n" + "transaction: tx1\n" + "\n\n" + Stomp.NULL; sendFrame(frame); frame = "SEND\n" + "destination:/queue/" + getQueueName() + "\n" + "transaction: tx1\n\n" + "Hello World" + Stomp.NULL; sendFrame(frame); frame = "COMMIT\n" + "transaction: tx1\n" + "\n\n" + Stomp.NULL; sendFrame(frame); message = consumer.receive(1000); Assert.assertNotNull("Should have received a message", message); }