001package var.mom.jms.log;
002
003import javax.jms.Connection;
004import javax.jms.ConnectionFactory;
005import javax.jms.Destination;
006import javax.jms.JMSException;
007import javax.jms.Message;
008import javax.jms.MessageConsumer;
009import javax.jms.MessageListener;
010import javax.jms.Session;
011import javax.jms.TextMessage;
012import javax.naming.Context;
013import javax.naming.InitialContext;
014import javax.naming.NamingException;
015
016/**
017 * asynchronous provider for the log service
018 *
019 * @author Sandro Leuchter
020 *
021 */
022public class ConsumerCallbackNode implements MessageListener {
023        private Connection connection;
024        private Session session;
025        private MessageConsumer consumer;
026
027        /**
028         * constructor, establishes and starts connection to JMS provider specified in
029         * JNDI (via jndi.properties), afterwards consumer is ready
030         *
031         * @throws NamingException JNDI exceptions
032         * @throws JMSException    JMS exceptions
033         */
034        public ConsumerCallbackNode() throws NamingException, JMSException {
035                Context ctx = new InitialContext();
036                ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
037                Destination queue = (Destination) ctx.lookup(Conf.QUEUE);
038                this.connection = factory.createConnection();
039                this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
040                this.consumer = this.session.createConsumer(queue);
041                this.consumer.setMessageListener(this);
042                this.connection.start();
043        }
044
045        /**
046         * asynchronous message consumption
047         *
048         * @see javax.jms.MessageListener
049         */
050        @Override
051        public void onMessage(Message message) {
052                try {
053                        if (message instanceof TextMessage) {
054                                TextMessage textMessage = (TextMessage) message;
055                                String messageText = textMessage.getText();
056                                String priority = textMessage.getStringProperty("Priority");
057                                System.out.println(messageText + " [Priority=" + priority + "]");
058                        }
059                } catch (JMSException e) {
060                        System.err.println(e);
061                }
062
063        }
064
065        /**
066         * main routine and starting point of program
067         *
068         * @param args[0] time to wait in ms
069         */
070        public static void main(String[] args) {
071                long wait = Long.parseLong(args[0]);
072                ConsumerCallbackNode node = null;
073                try {
074                        node = new ConsumerCallbackNode();
075                        Thread.sleep(wait);
076                } catch (InterruptedException | NamingException | JMSException e) {
077                        System.err.println(e);
078                } finally {
079                        try {
080                                if ((node != null) && (node.consumer != null)) {
081                                        node.consumer.close();
082                                }
083                                if ((node != null) && (node.session != null)) {
084                                        node.session.close();
085                                }
086                                if ((node != null) && (node.connection != null)) {
087                                        node.connection.close();
088                                }
089                        } catch (JMSException e) {
090                                System.err.println(e);
091                        }
092                }
093        }
094}