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.MessageProducer;
008import javax.jms.Session;
009import javax.jms.TextMessage;
010import javax.naming.Context;
011import javax.naming.InitialContext;
012import javax.naming.NamingException;
013
014/**
015 * client for the log service
016 *
017 * @author Sandro Leuchter
018 *
019 */
020public class ProducerNode {
021        private Connection connection;
022        private Session session;
023        private MessageProducer producer;
024
025        /**
026         * constructor, establishes and starts connection to JMS provider specified in
027         * JNDI (via jndi.properties), afterwards producer is ready
028         *
029         * @throws NamingException JNDI exceptions
030         * @throws JMSException    JMS exceptions
031         */
032        public ProducerNode() throws NamingException, JMSException {
033                Context ctx = new InitialContext();
034                ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
035                this.connection = factory.createConnection();
036                this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
037                Destination queue = (Destination) ctx.lookup(Conf.QUEUE);
038                this.producer = this.session.createProducer(queue);
039        }
040
041        /**
042         * creates TextMessage for JMS provider sets String property "Priority" to value
043         * of parameter priority and sends message to Destination of producer, which is
044         * configured in Conf.QUEUE
045         *
046         * @param text     payload of message
047         * @param priority value of String property "Priority"
048         * @throws JMSException JMS exceptions
049         */
050        public void sendMessage(String text, String priority) throws JMSException {
051                TextMessage message = this.session.createTextMessage();
052                message.setText(text);
053                message.setStringProperty("Priority", priority);
054                this.producer.send(message);
055        }
056
057        /**
058         * main routine and starting point of program
059         *
060         * @param args[0] time to wait in ms
061         * @param args[1] of message ("high" or anything else)
062         */
063        public static void main(String[] args) {
064                String text = args[0];
065                String priority = args[1];
066                ProducerNode node = null;
067                try {
068                        node = new ProducerNode();
069                        node.sendMessage(text, priority);
070                } catch (NamingException | JMSException e) {
071                        System.err.println(e);
072                } finally {
073                        try {
074                                if ((node != null) && (node.producer != null)) {
075                                        node.producer.close();
076                                }
077                                if ((node != null) && (node.session != null)) {
078                                        node.session.close();
079                                }
080                                if ((node != null) && (node.connection != null)) {
081                                        node.connection.close();
082                                }
083                        } catch (JMSException e) {
084                                System.err.println(e);
085                        }
086                }
087        }
088}