List of usage examples for javax.jms MessageConsumer receive
Message receive(long timeout) throws JMSException;
From source file:example.queue.Consumer.java
public static void main(String[] args) { String url = BROKER_URL; if (args.length > 0) { url = args[0].trim();/* w ww . j ava2s .c o m*/ } System.out.println("\nWaiting to receive messages... will timeout after " + TIMEOUT / 1000 + "s"); 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); Destination destination = session.createQueue("test-queue"); MessageConsumer consumer = session.createConsumer(destination); int i = 0; while (true) { Message message = consumer.receive(TIMEOUT); if (message != null) { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); System.out.println("Got " + i++ + ". message: " + text); } } else { break; } } consumer.close(); 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:example.queue.exclusive.Consumer.java
public static void main(String[] args) { String url = BROKER_URL; if (args.length > 0) { url = args[0].trim();//from w w w . j av a 2 s .c om } System.out.println("\nWaiting to receive messages... will timeout after " + TIMEOUT / 1000 + "s"); 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?consumer.exclusive=true"); MessageConsumer consumer = session.createConsumer(destination); int i = 0; while (true) { Message message = consumer.receive(TIMEOUT); if (message != null) { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); System.out.println("Got " + i++ + ". message: " + text); } } else { break; } } consumer.close(); 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:example.tempdest.Consumer.java
public static void main(String[] args) { String url = BROKER_URL; if (args.length > 0) { url = args[0].trim();//from w w w .j a va2s . c om } System.out.println("\nWaiting to receive messages... will timeout after " + TIMEOUT / 1000 + "s"); ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url); Connection connection = null; try { connection = connectionFactory.createConnection(); connection.start(); final Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue("test-queue"); MessageConsumer consumer = session.createConsumer(destination); int i = 0; while (true) { Message message = consumer.receive(TIMEOUT); if (message != null) { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); System.out.println("Got " + i++ + ". message: " + text); Destination replyTo = message.getJMSReplyTo(); MessageProducer producer = session.createProducer(replyTo); producer.send( session.createTextMessage("You made it to the consumer, here is your response")); producer.close(); } } else { break; } } consumer.close(); 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.ActiveMQConsumer.java
public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory = ConnectionFactoryFacade .createConnectionFactory("tcp://sci-serv5.diamond.ac.uk:61616"); Connection connection = connectionFactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue("testQ"); final MessageConsumer consumer = session.createConsumer(queue); connection.start();/*from w ww . ja v a 2s .com*/ while (true) { // You have to kill it to stop it! Message m = consumer.receive(1000); if (m != null) { if (m instanceof TextMessage) { TextMessage t = (TextMessage) m; System.out.println(t.getText()); try { ObjectMapper mapper = new ObjectMapper(); final SweepBean colBack = mapper.readValue(t.getText(), SweepBean.class); System.out.println("Data collection found: " + colBack.getDataCollectionId()); } catch (Exception ne) { System.out.println(m + " is not a data collection."); } } else if (m instanceof ObjectMessage) { ObjectMessage o = (ObjectMessage) m; System.out.println(o.getObject()); } } } }
From source file:example.queue.selector.Consumer.java
public static void main(String[] args) { String url = BROKER_URL; if (args.length > 0) { url = args[0].trim();// w w w .j av a2s .c o m } System.out.println("\nWaiting to receive messages... will timeout after " + TIMEOUT / 1000 + "s"); 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); Destination destination = session.createQueue("test-queue"); MessageConsumer consumer = session.createConsumer(destination, "intended = 'me'"); int i = 0; while (true) { Message message = consumer.receive(TIMEOUT); if (message != null) { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); System.out.println("Got " + i++ + ". message: " + text); } } else { break; } } consumer.close(); 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:com.fusesource.examples.activemq.VirtualConsumer.java
public static void main(String args[]) { Connection connection = null; try {//from w w w .j a v a2s .c o m // JNDI lookup of JMS Connection Factory and JMS Destination Context context = new InitialContext(); ConnectionFactory factory = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_NAME); connection = factory.createConnection(); connection.setClientID(System.getProperty("clientID")); connection.start(); Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue(System.getProperty("queue")); MessageConsumer consumer = session.createConsumer(queue); LOG.info("Start consuming messages from " + queue + " with " + MESSAGE_TIMEOUT_MILLISECONDS + "ms timeout"); // Synchronous message consumer int i = 1; while (true) { Message message = consumer.receive(MESSAGE_TIMEOUT_MILLISECONDS); if (message != null) { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); LOG.info("Got " + (i++) + ". message: " + text); } } else { break; } } consumer.close(); session.close(); } catch (Throwable t) { LOG.error(t); } finally { // Cleanup code // In general, you should always close producers, consumers, // sessions, and connections in reverse order of creation. // For this simple example, a JMS connection.close will // clean up all other resources. if (connection != null) { try { connection.close(); } catch (JMSException e) { LOG.error(e); } } } }
From source file:com.fusesource.examples.activemq.SimpleSubscriber.java
public static void main(String args[]) { Connection connection = null; try {/* w w w . j av a 2s.c o m*/ // JNDI lookup of JMS Connection Factory and JMS Destination Context context = new InitialContext(); ConnectionFactory factory = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_NAME); connection = factory.createConnection(); connection.setClientID("ApplicationA"); connection.start(); Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); String topicName = System.getProperty("topic"); Topic topic = session.createTopic(topicName); MessageConsumer consumer = session.createConsumer(topic); LOG.info("Start consuming messages from " + topicName + " with " + MESSAGE_TIMEOUT_MILLISECONDS + "ms timeout"); // Synchronous message consumer int i = 1; while (true) { Message message = consumer.receive(MESSAGE_TIMEOUT_MILLISECONDS); if (message != null) { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); LOG.info("Got " + (i++) + ". message: " + text); } } else { break; } } consumer.close(); session.close(); } catch (Throwable t) { LOG.error(t); } finally { // Cleanup code // In general, you should always close producers, consumers, // sessions, and connections in reverse order of creation. // For this simple example, a JMS connection.close will // clean up all other resources. if (connection != null) { try { connection.close(); } catch (JMSException e) { LOG.error(e); } } } }
From source file:com.fusesource.examples.activemq.SimpleConsumer.java
public static void main(String args[]) { Connection connection = null; try {//from www . ja v a 2 s .c om // JNDI lookup of JMS Connection Factory and JMS Destination Context context = new InitialContext(); ConnectionFactory factory = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_NAME); Destination destination = (Destination) context.lookup(DESTINATION_NAME); connection = factory.createConnection(); connection.start(); Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(destination); LOG.info("Start consuming messages from " + destination.toString() + " with " + MESSAGE_TIMEOUT_MILLISECONDS + "ms timeout"); // Synchronous message consumer int i = 1; while (true) { Message message = consumer.receive(MESSAGE_TIMEOUT_MILLISECONDS); if (message != null) { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); LOG.info("Got " + (i++) + ". message: " + text); } } else { break; } } consumer.close(); session.close(); } catch (Throwable t) { LOG.error(t); } finally { // Cleanup code // In general, you should always close producers, consumers, // sessions, and connections in reverse order of creation. // For this simple example, a JMS connection.close will // clean up all other resources. if (connection != null) { try { connection.close(); } catch (JMSException e) { LOG.error(e); } } } }
From source file:com.fusesource.examples.activemq.DurableSubscriber.java
public static void main(String args[]) { Connection connection = null; try {//from ww w . ja v a2 s . c o m // JNDI lookup of JMS Connection Factory and JMS Destination Context context = new InitialContext(); ConnectionFactory factory = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_NAME); connection = factory.createConnection(); connection.setClientID(System.getProperty("clientID")); connection.start(); Session session = connection.createSession(NON_TRANSACTED, Session.CLIENT_ACKNOWLEDGE); String topicName = System.getProperty("topic"); Topic topic = session.createTopic(topicName); MessageConsumer consumer = session.createDurableSubscriber(topic, "Test_Durable_Subscriber"); LOG.info("Start consuming messages from " + topicName + " with " + MESSAGE_TIMEOUT_MILLISECONDS + "ms timeout"); // Synchronous message consumer int i = 1; while (true) { Message message = consumer.receive(MESSAGE_TIMEOUT_MILLISECONDS); if (message != null) { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); LOG.info("Got " + (i++) + ". message: " + text); } } else { break; } } consumer.close(); session.close(); } catch (Throwable t) { LOG.error(t); } finally { // Cleanup code // In general, you should always close producers, consumers, // sessions, and connections in reverse order of creation. // For this simple example, a JMS connection.close will // clean up all other resources. if (connection != null) { try { connection.close(); } catch (JMSException e) { LOG.error(e); } } } }
From source file:example.composite.dest.Consumer.java
public static void main(String[] args) { String url = BROKER_URL; if (args.length > 0) { url = args[0].trim();//from w w w .j a v a 2 s .co m } System.out.println("\nWaiting to receive messages... will timeout after " + TIMEOUT / 1000 + "s"); 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); Destination destination = session.createQueue("test-queue"); Destination destinationFoo = session.createQueue("test-queue-foo"); Destination destinationBar = session.createQueue("test-queue-bar"); Destination destinationTopicFoo = session.createTopic("test-topic-foo"); MessageConsumer consumer = session.createConsumer(destination); MessageConsumer consumerFoo = session.createConsumer(destinationFoo); MessageConsumer consumerBar = session.createConsumer(destinationBar); MessageConsumer consumerTopicFoo = session.createConsumer(destinationTopicFoo); int i = 0; while (true) { Message message = consumer.receive(TIMEOUT); if (message != null) { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); System.out.println("Got " + i++ + ". message on test-queue: " + text); } } else { break; } message = consumerFoo.receive(TIMEOUT); if (message != null) { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); System.out.println("Got " + i++ + ". message on test-queue-foo: " + text); } } else { break; } message = consumerBar.receive(TIMEOUT); if (message != null) { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); System.out.println("Got " + i++ + ". message on test-queue-bar: " + text); } } else { break; } message = consumerTopicFoo.receive(TIMEOUT); if (message != null) { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); System.out.println("Got " + i++ + ". message on test-topic-bar: " + text); } } else { break; } } consumer.close(); 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..."); } } } }