List of usage examples for javax.jms MessageConsumer receive
Message receive(long timeout) throws JMSException;
From source file:org.apache.activemq.demo.SimpleConsumer.java
/** * @param args the queue used by the example *//* w w w. java 2s . co m*/ public static void main(String[] args) { String destinationName = null; Context jndiContext = null; ConnectionFactory connectionFactory = null; Connection connection = null; Session session = null; Destination destination = null; MessageConsumer consumer = null; /* * Read destination name from command line and display it. */ if (args.length != 1) { LOG.info("Usage: java SimpleConsumer <destination-name>"); System.exit(1); } destinationName = args[0]; LOG.info("Destination name is " + destinationName); /* * Create a JNDI API InitialContext object */ try { jndiContext = new InitialContext(); } catch (NamingException e) { LOG.info("Could not create JNDI API " + "context: " + e.toString()); System.exit(1); } /* * Look up connection factory and destination. */ try { connectionFactory = (ConnectionFactory) jndiContext.lookup("ConnectionFactory"); destination = (Destination) jndiContext.lookup(destinationName); } catch (NamingException e) { LOG.info("JNDI API lookup failed: " + e.toString()); System.exit(1); } /* * Create connection. Create session from connection; false means * session is not transacted. Create receiver, then start message * delivery. Receive all text messages from destination until a non-text * message is received indicating end of message stream. Close * connection. */ try { connection = connectionFactory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); consumer = session.createConsumer(destination); connection.start(); while (true) { Message m = consumer.receive(1); if (m != null) { if (m instanceof TextMessage) { TextMessage message = (TextMessage) m; LOG.info("Reading message: " + message.getText()); } else { break; } } } } catch (JMSException e) { LOG.info("Exception occurred: " + e); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { } } } }
From source file:io.datalayer.activemq.consumer.SimpleConsumer.java
/** * @param args the queue used by the example *///from ww w .j a v a 2 s .c o m public static void main(String... args) { String destinationName = null; Context jndiContext = null; ConnectionFactory connectionFactory = null; Connection connection = null; Session session = null; Destination destination = null; MessageConsumer consumer = null; /* * Read destination name from command line and display it. */ if (args.length != 1) { LOG.info("Usage: java SimpleConsumer <destination-name>"); System.exit(1); } destinationName = args[0]; LOG.info("Destination name is " + destinationName); /* * Create a JNDI API InitialContext object */ try { jndiContext = new InitialContext(); } catch (NamingException e) { LOG.info("Could not create JNDI API " + "context: " + e.toString()); System.exit(1); } /* * Look up connection factory and destination. */ try { connectionFactory = (ConnectionFactory) jndiContext.lookup("ConnectionFactory"); destination = (Destination) jndiContext.lookup(destinationName); } catch (NamingException e) { LOG.info("JNDI API lookup failed: " + e.toString()); System.exit(1); } /* * Create connection. Create session from connection; false means * session is not transacted. Create receiver, then start message * delivery. Receive all text messages from destination until a non-text * message is received indicating end of message stream. Close * connection. */ try { connection = connectionFactory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); consumer = session.createConsumer(destination); connection.start(); while (true) { Message m = consumer.receive(1); if (m != null) { if (m instanceof TextMessage) { TextMessage message = (TextMessage) m; LOG.info("Reading message: " + message.getText()); } else { break; } } } } catch (JMSException e) { LOG.info("Exception occurred: " + e); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { } } } }
From source file:sk.seges.test.jms.activemq.SimpleActiveMQQueueReceiveFailTest.java
@Test public void testReceive() throws Exception { Connection connection = activeMQConnectionFactory.createConnection(); connection.start();/* w w w.j a v a 2s . co m*/ Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(activeMQTestQueueA); Message rawMessage = consumer.receive(1000); assertNull(rawMessage); connection.close(); }
From source file:sk.seges.test.jms.activemq.SimpleActiveMQQueueReceiveTest.java
@Test public void testReceive() throws Exception { Connection connection = activeMQConnectionFactory.createConnection(); connection.start();// w ww. ja va 2s . c o m Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(activeMQTestQueueA); Message rawMessage = consumer.receive(1000); assertTrue(rawMessage instanceof TextMessage); TextMessage message = (TextMessage) rawMessage; assertEquals("test text", message.getText()); connection.close(); }
From source file:com.nesscomputing.jms.activemq.ServiceDiscoveryTransportFactoryTest.java
private void consumeTestMessage() throws Exception { final Connection connection = factory.createConnection(); connection.start();//from www. ja v a 2s.c o m try { final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); final MessageConsumer consumer = session.createConsumer(session.createQueue(QNAME)); final Message message = consumer.receive(1000); LOG.info(ObjectUtils.toString(message, "<no message>")); Assert.assertEquals(uniqueId, ((TextMessage) message).getText()); } finally { connection.stop(); connection.close(); } }
From source file:ubic.gemma.job.grid.util.JMSBrokerMonitorImpl.java
private MapMessage sendTaskSubmissionQueueDiagnosticMessage() throws JMSException { MapMessage reply = jmsTemplate.execute(new SessionCallback<MapMessage>() { @Override//from w ww .j ava 2s .co m public MapMessage doInJms(Session session) throws JMSException { Queue replyTo = session.createTemporaryQueue(); Message message = session.createMessage(); message.setJMSReplyTo(replyTo); Queue queryQueue = session.createQueue("ActiveMQ.Statistics.Destination.tasks.submit"); MessageProducer producer = session.createProducer(queryQueue); MessageConsumer consumer = session.createConsumer(replyTo); producer.send(message); return (MapMessage) consumer.receive(5000); } }, true); return reply; }
From source file:org.apache.activemq.camel.JmsJdbcXARollbackTest.java
private boolean consumedFrom(String qName) throws Exception { ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://testXA"); factory.setWatchTopicAdvisories(false); Connection connection = factory.createConnection(); connection.start();/* w ww .j av a 2 s .com*/ Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(new ActiveMQQueue(qName)); Message message = consumer.receive(500); LOG.info("Got from queue:{} {}", qName, message); connection.close(); return message != null; }
From source file:org.wso2.carbon.sample.consumer.TopicConsumer.java
public void run() { // create topic connection TopicConnection topicConnection = null; try {/*from ww w. j a v a 2 s . c o m*/ topicConnection = topicConnectionFactory.createTopicConnection(); topicConnection.start(); } catch (JMSException e) { log.error("Can not create topic connection." + e.getMessage(), e); return; } Session session = null; try { session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createTopic(topicName); MessageConsumer consumer = session.createConsumer(destination); log.info("Listening for messages"); while (active) { Message message = consumer.receive(1000); if (message != null) { if (message instanceof MapMessage) { MapMessage mapMessage = (MapMessage) message; Map<String, Object> map = new HashMap<String, Object>(); Enumeration enumeration = mapMessage.getMapNames(); while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); map.put(key, mapMessage.getObject(key)); } log.info("Received Map Message : " + map); } else if (message instanceof TextMessage) { log.info("Received Text Message : " + ((TextMessage) message).getText()); } else { log.info("Received message : " + message.toString()); } } } log.info("Finished listening for messages."); session.close(); topicConnection.stop(); topicConnection.close(); } catch (JMSException e) { log.error("Can not subscribe." + e.getMessage(), e); } }
From source file:org.wso2.carbon.sample.consumer.QueueConsumer.java
public void run() { // create queue connection QueueConnection queueConnection = null; try {/* w w w . ja v a 2 s . c o m*/ queueConnection = queueConnectionFactory.createQueueConnection(); queueConnection.start(); } catch (JMSException e) { log.error("Can not create queue connection." + e.getMessage(), e); return; } Session session = null; try { session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(queueName); MessageConsumer consumer = session.createConsumer(destination); log.info("Listening for messages"); while (active) { Message message = consumer.receive(1000); if (message != null) { if (message instanceof MapMessage) { MapMessage mapMessage = (MapMessage) message; Map<String, Object> map = new HashMap<String, Object>(); Enumeration enumeration = mapMessage.getMapNames(); while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); map.put(key, mapMessage.getObject(key)); } log.info("Received Map Message : " + map); } else if (message instanceof TextMessage) { log.info("Received Text Message : " + ((TextMessage) message).getText()); } else { log.info("Received message : " + message.toString()); } } } log.info("Finished listening for messages."); session.close(); queueConnection.stop(); queueConnection.close(); } catch (JMSException e) { log.error("Can not subscribe." + e.getMessage(), e); } }
From source file:org.wso2.extension.siddhi.io.jms.sink.util.TopicConsumer.java
public void run() { // create topic connection TopicConnection topicConnection = null; try {//from w w w. j a va 2s . co m topicConnection = topicConnectionFactory.createTopicConnection(); topicConnection.start(); } catch (JMSException e) { log.error("Can not create topic connection." + e.getMessage(), e); return; } Session session = null; try { session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createTopic(topicName); MessageConsumer consumer = session.createConsumer(destination); log.info("Listening for messages"); while (active) { Message message = consumer.receive(1000); if (message != null) { resultContainer.eventReceived(message); if (message instanceof MapMessage) { MapMessage mapMessage = (MapMessage) message; Map<String, Object> map = new HashMap<String, Object>(); Enumeration enumeration = mapMessage.getMapNames(); while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); map.put(key, mapMessage.getObject(key)); } log.info("Received Map Message : " + map); } else if (message instanceof TextMessage) { log.info("Received Text Message : " + ((TextMessage) message).getText()); } else { log.info("Received message : " + message.toString()); } } } log.info("Finished listening for messages."); session.close(); topicConnection.stop(); topicConnection.close(); } catch (JMSException e) { log.error("Can not subscribe." + e.getMessage(), e); } }