List of usage examples for javax.jms MessageConsumer setMessageListener
void setMessageListener(MessageListener listener) throws JMSException;
From source file:example.topic.Subscriber.java
public static void main(String[] args) { String url = BROKER_URL; if (args.length > 0) { url = args[0].trim();// ww w.ja v a2 s. c o m } System.out .println("\nWaiting to receive messages... Either waiting for END message or press Ctrl+C to exit"); ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url); Connection connection = null; final CountDownLatch latch = new CountDownLatch(1); try { connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createTopic("test-topic"); MessageConsumer consumer = session.createConsumer(destination); consumer.setMessageListener(new Subscriber(latch)); latch.await(); 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.ProducerRequestReply.java
public static void main(String[] args) { String url = BROKER_URL; if (args.length > 0) { url = args[0].trim();// w ww .j a v a 2s .c om } 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"); MessageProducer producer = session.createProducer(destination); Destination replyDest = session.createTemporaryQueue(); // set up the consumer to handle the reply MessageConsumer replyConsumer = session.createConsumer(replyDest); replyConsumer.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { System.out.println("*** REPLY *** "); System.out.println(message.toString()); } }); TextMessage message = session.createTextMessage("I need a response for this, please"); message.setJMSReplyTo(replyDest); producer.send(message); // wait for a response TimeUnit.SECONDS.sleep(2); producer.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.topic.durable.Subscriber.java
public static void main(String[] args) { String url = BROKER_URL; if (args.length > 0) { url = args[0].trim();//from ww w . j a va 2 s. co m } System.out .println("\nWaiting to receive messages... Either waiting for END message or press Ctrl+C to exit"); ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url); Connection connection = null; final CountDownLatch latch = new CountDownLatch(1); try { connection = connectionFactory.createConnection(); String clientId = System.getProperty("clientId"); connection.setClientID(clientId); connection.start(); Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); Topic destination = session.createTopic("test-topic"); MessageConsumer consumer = session.createDurableSubscriber(destination, clientId); consumer.setMessageListener(new Subscriber(latch)); latch.await(); 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.transaction.Client.java
public static void main(String[] args) { String url = BROKER_URL; if (args.length > 0) { url = args[0].trim();/*from w ww . j a va 2 s . com*/ } ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url); Connection connection = null; try { connection = connectionFactory.createConnection(); connection.start(); Topic destination = new ActiveMQTopic("transacted.client.example"); Session senderSession = connection.createSession(TRANSACTED, Session.AUTO_ACKNOWLEDGE); Session receiverSession = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); MessageConsumer receiver = receiverSession.createConsumer(destination); receiver.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { if (message instanceof TextMessage) { try { String value = ((TextMessage) message).getText(); System.out.println("We received a new message: " + value); } catch (JMSException e) { System.out.println("Could not read the receiver's topic because of a JMSException"); } } } }); MessageProducer sender = senderSession.createProducer(destination); connection.start(); acceptInputFromUser(senderSession, sender); senderSession.close(); receiverSession.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.wildcard.Client.java
public static void main(String[] args) { String url = BROKER_URL; if (args.length > 0) { url = args[0].trim();//from www .j a va 2 s .c o m } ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("admin", "password", url); Connection connection = null; try { Topic senderTopic = new ActiveMQTopic(System.getProperty("topicName")); connection = connectionFactory.createConnection("admin", "password"); Session senderSession = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); MessageProducer sender = senderSession.createProducer(senderTopic); Session receiverSession = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE); String policyType = System.getProperty("wildcard", ".*"); String receiverTopicName = senderTopic.getTopicName() + policyType; Topic receiverTopic = receiverSession.createTopic(receiverTopicName); MessageConsumer receiver = receiverSession.createConsumer(receiverTopic); receiver.setMessageListener(new MessageListener() { public void onMessage(Message message) { try { if (message instanceof TextMessage) { String text = ((TextMessage) message).getText(); System.out.println("We received a new message: " + text); } } catch (JMSException e) { System.out.println("Could not read the receiver's topic because of a JMSException"); } } }); connection.start(); System.out.println("Listening on '" + receiverTopicName + "'"); System.out.println("Enter a message to send: "); Scanner inputReader = new Scanner(System.in); while (true) { String line = inputReader.nextLine(); if (line == null) { System.out.println("Done!"); break; } else if (line.length() > 0) { try { TextMessage message = senderSession.createTextMessage(); message.setText(line); System.out.println("Sending a message: " + message.getText()); sender.send(message); } catch (JMSException e) { System.out.println("Exception during publishing a message: "); } } } receiver.close(); receiverSession.close(); sender.close(); senderSession.close(); } catch (Exception e) { System.out.println("Caught exception!"); } finally { if (connection != null) { try { connection.close(); } catch (JMSException e) { System.out.println("When trying to close connection: "); } } } }
From source file:org.firstopen.singularity.util.JMSUtil.java
public static Connection registerListenerOnQueue(MessageListener listener, String queueName) throws Exception { InitialContext jndiContext = JNDIUtil.getInitialContext(); Queue queue = (Queue) jndiContext.lookup("queue/" + queueName); ConnectionFactory qcf = (ConnectionFactory) jndiContext.lookup("ConnectionFactory"); Connection connection = qcf.createConnection(); Session m_session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer m_receiver = m_session.createConsumer(queue); m_receiver.setMessageListener(listener); return connection; }
From source file:Log4jJMSAppenderExample.java
public Log4jJMSAppenderExample() throws Exception { // create a logTopic topic consumer ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616"); Connection conn = factory.createConnection(); Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); conn.start();/*from ww w . ja v a 2 s . c o m*/ MessageConsumer consumer = sess.createConsumer(sess.createTopic("logTopic")); consumer.setMessageListener(this); // log a message Logger log = Logger.getLogger(Log4jJMSAppenderExample.class); log.info("Test log"); // clean up Thread.sleep(1000); consumer.close(); sess.close(); conn.close(); System.exit(1); }
From source file:com.fusesource.forge.jmstest.tests.simple.SimpleConsumer.java
protected void run() { Connection con = null;//from www . j av a 2 s . c o m Session session = null; final CountDownLatch latch = new CountDownLatch(Integer.MAX_VALUE); try { con = getConnection(); session = con.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination dest = getDestinationProvider().getDestination(session, "queue:TEST"); MessageConsumer consumer = session.createConsumer(dest); consumer.setMessageListener(new MessageListener() { public void onMessage(Message msg) { String grp = null; long nr = 0L; try { grp = msg.getStringProperty("JMSXGroupID"); nr = msg.getLongProperty("MsgNr"); } catch (JMSException jme) { } log().info("Received Message Group=(" + grp + ") MsgNr=" + nr); latch.countDown(); } }); con.start(); latch.await(); con.close(); } catch (Exception e) { } finally { if (con != null) { try { con.close(); } catch (Exception e) { } } } }
From source file:TopicListener.java
public void run() throws JMSException { ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url); connection = factory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); topic = session.createTopic("topictest.messages"); control = session.createTopic("topictest.control"); MessageConsumer consumer = session.createConsumer(topic); consumer.setMessageListener(this); connection.start();// www . j a v a2 s .c o m producer = session.createProducer(control); System.out.println("Waiting for messages..."); }
From source file:com.fusesource.forge.jmstest.tests.AsyncConsumer.java
public void doStart() { try {/* w w w .ja va 2s. co m*/ Destination dest = getDestination(getSession(), getDestinationName()); MessageConsumer consumer = session.createConsumer(dest); consumer.setMessageListener(this); getConnection().start(); } catch (Exception jme) { try { getConnection().close(); } catch (Exception e) { } } }