001package var.mom.jms.client;
002
003import java.io.BufferedReader;
004import java.io.IOException;
005import java.io.InputStreamReader;
006
007import javax.jms.Connection;
008import javax.jms.ConnectionFactory;
009import javax.jms.Destination;
010import javax.jms.JMSException;
011import javax.jms.Message;
012import javax.jms.MessageConsumer;
013import javax.jms.MessageListener;
014import javax.jms.MessageProducer;
015import javax.jms.Session;
016import javax.jms.TextMessage;
017import javax.naming.Context;
018import javax.naming.InitialContext;
019import javax.naming.NamingException;
020
021/**
022 * general JMS client with one producer and one consumer
023 *
024 * @author Sandro Leuchter
025 *
026 */
027public class JMSClient implements MessageListener {
028        private Connection connection;
029        private Session session;
030        private MessageProducer producer;
031        private MessageConsumer consumer;
032
033        /**
034         * constructor, establishes and starts connection to JMS provider specified in
035         * JNDI (via jndi.properties), afterwards producer and consumer are ready
036         *
037         * @param sendDest    Destination for producer
038         * @param receiveDest Destination for consumer
039         *
040         * @throws NamingException JNDI exceptions
041         * @throws JMSException    JMS exceptions
042         * @see javax.jms.Destination
043         */
044        public JMSClient(String sendDest, String receiveDest) throws NamingException, JMSException {
045                Context ctx = new InitialContext();
046                ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
047                this.connection = factory.createConnection();
048                this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
049                Destination destOut = (Destination) ctx.lookup(sendDest);
050                Destination destIn = (Destination) ctx.lookup(receiveDest);
051                this.producer = this.session.createProducer(destOut);
052                this.consumer = this.session.createConsumer(destIn);
053                this.consumer.setMessageListener(this);
054                this.connection.start();
055        }
056
057        /**
058         * asynchronous message consumption
059         *
060         * @see javax.jms.MessageListener
061         */
062        @Override
063        public void onMessage(Message message) {
064                if (message instanceof TextMessage) {
065                        TextMessage textMessage = (TextMessage) message;
066                        try {
067                                System.out.println(textMessage.getText());
068                        } catch (JMSException e) {
069                                System.err.println(e);
070                        }
071                }
072        }
073
074        /**
075         * main routine and starting point of program
076         *
077         * @param args not used
078         */
079        public static void main(String[] args) {
080                JMSClient node = null;
081                try {
082                        node = new JMSClient("var.mom.jms.client.queue1", "var.mom.jms.client.queue2");
083                        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
084                        String line;
085                        while (true) {
086                                line = input.readLine();
087                                node.producer.send(node.session.createTextMessage(line));
088                        }
089                } catch (NamingException | JMSException | IOException e) {
090                        System.err.println(e);
091                } finally {
092                        try {
093                                if ((node != null) && (node.producer != null)) {
094                                        node.producer.close();
095                                }
096                                if ((node != null) && (node.consumer != null)) {
097                                        node.consumer.close();
098                                }
099                                if ((node != null) && (node.session != null)) {
100                                        node.session.close();
101                                }
102                                if ((node != null) && (node.connection != null)) {
103                                        node.connection.close();
104                                }
105                        } catch (JMSException e) {
106                                System.err.println(e);
107                        }
108                }
109        }
110}