Example usage for javax.jms MessageConsumer close

List of usage examples for javax.jms MessageConsumer close

Introduction

In this page you can find the example usage for javax.jms MessageConsumer close.

Prototype


void close() throws JMSException;

Source Link

Document

Closes the message consumer.

Usage

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   w w  w.j  av  a2  s.  com*/
    }
    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:example.composite.dest.Consumer.java

public static void main(String[] args) {
    String url = BROKER_URL;
    if (args.length > 0) {
        url = args[0].trim();/*from   ww w  .  jav 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);
        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...");
            }
        }
    }
}

From source file:com.mirth.connect.connectors.jms.JmsUtils.java

public static void close(MessageConsumer consumer) throws JMSException {
    if (consumer != null) {
        consumer.close();
    }//www .  j ava2 s  . c  om
}

From source file:com.mirth.connect.connectors.jms.JmsUtils.java

public static void closeQuietly(MessageConsumer consumer) {
    if (consumer != null) {
        try {/*  ww  w . j a  v a  2s  .  c om*/
            consumer.close();
        } catch (JMSException e) {
            logger.error("Failed to close jms message consumer", e);
        }
    }
}

From source file:org.fusesource.stompjms.JmsTestSupport.java

protected void safeClose(MessageConsumer c) {
    try {
        c.close();
    } catch (Throwable e) {
    }
}

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();// ww  w  . j a  v a 2s. co  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:hermes.ext.imq.ImqAdmin.java

private void clearDestMetricTopicSubscribers() throws JMSException {
    for (MessageConsumer subscriber : destMetricTopicSubscribers.values()) {
        LOG.debug("Closing subscriber: " + subscriber);
        subscriber.close();
    }//from   w  w  w.jav  a2s. c o  m
    destMetricTopicSubscribers.clear();
}

From source file:com.facetime.communication.activemq.AmqConsumer.java

public synchronized void closeConsumers() {
    for (Iterator<MessageConsumer> it = consumers.values().iterator(); it.hasNext();) {
        MessageConsumer consumer = it.next();
        it.remove();/*from  www  .j  a va 2s. com*/
        try {
            consumer.setMessageListener(null);
            if (consumer instanceof MessageAvailableConsumer) {
                ((MessageAvailableConsumer) consumer).setAvailableListener(null);
            }
            consumer.close();
        } catch (JMSException e) {
            LOG.debug("caught exception closing consumer", e);
        }
    }
}

From source file:com.facetime.communication.activemq.AmqConsumer.java

public synchronized void closeConsumer(Destination destination) throws JMSException {
    MessageConsumer consumer = consumers.get(destination);
    if (consumer != null) {
        consumers.remove(destination);//w ww .  j  a v a  2 s. c o m
        consumer.setMessageListener(null);
        if (consumer instanceof MessageAvailableConsumer) {
            ((MessageAvailableConsumer) consumer).setAvailableListener(null);
        }
        consumer.close();
    }
}

From source file:com.chinamobile.bcbsp.comm.ConsumerTool.java

/** Comsume messages. */
protected void consumeMessages(Connection connection, Session session, MessageConsumer consumer, long timeout)
        throws JMSException, IOException {
    Message message;//from   www .  jav a 2 s  . c o  m
    while (!this.receiver.getNoMoreMessagesFlag()) {
        while ((message = consumer.receive(timeout)) != null) {
            onMessageOptimistic(message);
        }
    }
    consumer.close();
    session.close();
    connection.close();
}